⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 updatedemo.java

📁 图书中的经典例子 好好看看 会有帮助的。java2me版本的
💻 JAVA
字号:
//updateDemo.java file
//试验在自动更新的结果集被改变时发生的情况
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;

public class updateDemo extends MIDlet implements CommandListener
{
    //命令对象
    private Command exitCommand, addCmd, delCmd, resetCmd, ckCmd,ckCmd2;
    private TextBox tb; //通过TextBox来显示信息
    private RecordStore rc=null; //数据操作对象
    private RecordEnumeration re=null;
    private String sDbName="udemo";
    private int iCounter=0;
    public updateDemo()
    {
        tb =new TextBox("OutputWindow","",120,TextField.ANY);
        exitCommand =new Command("Exit",Command.EXIT,1);
        addCmd =new Command("add",Command.SCREEN,1);
        ckCmd =new Command("check",Command.SCREEN,1);
        ckCmd2 =new Command("check2",Command.SCREEN,1);
        delCmd =new Command("DelDb",Command.SCREEN,1);
        resetCmd =new Command("resetCmd",Command.SCREEN,1);
        tb.addCommand(exitCommand);
        tb.addCommand(addCmd);
        tb.addCommand(delCmd);
        tb.addCommand(resetCmd);
        tb.addCommand(ckCmd);
        tb.addCommand(ckCmd2);
        tb.setCommandListener(this);
    }
    protected void startApp(  ) throws MIDletStateChangeException
    {
        Display.getDisplay(this).setCurrent(tb);
        InitDb();
    }

	protected void InitDb( )
	{
		try
		{
			RecordStore.deleteRecordStore(sDbName);
		}
		catch(Exception e)
		{
			System.out.println("db error " + e.toString());
		}
		try
		{
			rc = RecordStore.openRecordStore(sDbName, true);
			UserFindFilter f = new UserFindFilter(1,"a");
			UserSorter sort = new UserSorter(UserSorter.SORTBYNAME);
			re = rc.enumerateRecords(f, sort, true);
		}
		catch(Exception e)
		{
			System.out.println("db error " + e.toString());
		}
	}
	
    protected void pauseApp(  )
    {
    }

    protected void destroyApp( boolean p1 )
    {
    	if(rc != null)
    	{
			try
			{
				rc.closeRecordStore( );
			}
			catch(Exception e)
			{
				System.out.println("db error " + e.toString());
			}
    	}
    	if(re != null)
    	{
			try
			{
				re.destroy( );
			}
			catch(Exception e)
			{
				System.out.println("db error " + e.toString());
			}
    	}
    	
    }
	public void doEnum( )
	{
//		re.reset();
		System.out.println("recNum ="+ re.numRecords());
		try
		{
			while(re.hasNextElement() )
			{
				System.out.println("next id = "+ re.nextRecordId());
			}
		}
		catch(Exception e)
		{
			System.out.println("db error " + e.toString());
		}
	}
	public void doEnum2( )
	{
//		re.reset();
		System.out.println("recNum ="+ re.numRecords());
		try
		{
			while(re.hasPreviousElement() )
			{
				System.out.println("next id = "+ re.previousRecordId());
			}
		}
		catch(Exception e)
		{
			System.out.println("db error " + e.toString());
		}
	}
    public void commandAction(Command c,Displayable d)
    {
    	String sAdd = "abcd"+ iCounter ;
    	sAdd +="&aaa&aaa&aaa";
    	iCounter++;
    	byte []b=sAdd.getBytes();
    	try
    	{
	    	
	        if (c ==exitCommand)
	        {
	            destroyApp(false);
	            notifyDestroyed();
	        }
	        else if(c ==addCmd)
	        {
	        	rc.addRecord(b,0,sAdd.length());
	        }
	        else if(c ==delCmd)
	        {
	        }
	        else if(c ==resetCmd)
	        {
	        	re.reset();
	        }
	        else if(c ==ckCmd)
	        {
	        	doEnum();
	        }
	        else if(c ==ckCmd2)
	        {
	        	doEnum2();
	        }
	    }
		catch(Exception e)
		{
			System.out.println("db error " + e.toString());
		}
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -