一直在做J2EE的开发,最近公司拉了一个J2ME手持机<PDA>(其实和手机差不多)开发项目,本人算是第一次做这样的项目,如果问题问的有些弱还希望高手不要见笑。
下面开始说问题:
PAD使用的是WTK平台,要实现的功能是:对数据进行保存、修改、删除、查询,由于环境的问题,程序要求在无网络的情况下能够正常运行,所以运行环境算是单机版的。
数据格式很简单包含:档案ID、卡号、用户名、金额。
数据量最少在一万条以上,由于只能存储在本机环境内,这对于手持机来说不是件容易的事情。起先准备使用操作本地文本文件的方式存储(txt文件,每一行为一条数据)但是j2me对文本文件的操作真是太低级。对于查询和修改数据不易操作,而且数据过大会导致内存溢出。
所以本人准备尝试使用嵌入式数据库来完成,目前选用的数据库位JMESQL,网上对于这个数据库的操作知识并不是很多,只能在官方找到所谓的例子,可是操作中创建表一直不成功。后台打印异常为:
- Java code
开始创建表----..............false开始创建表----br.com.dreamsource.mobile.jmesql.exceptions.SQLException: already exists: PRICETABLE -- S0001 in statement [CREATE TABLE priceTable ( id int identity, name varchar(20) not null, percent float not null, primary key(id))] -- Table at br.com.dreamsource.mobile.jmesql.jdbcResultSet.<init>(+37) at br.com.dreamsource.mobile.jmesql.jdbcConnection.executeStandalone(+19) at br.com.dreamsource.mobile.jmesql.jdbcConnection.execute(+21) at br.com.dreamsource.mobile.jmesql.jdbcStatement.fetchResult(+36) at br.com.dreamsource.mobile.jmesql.jdbcStatement.execute(+5) at com.sqlplus.FrameMainMIDlet.createDatabase(+48) at com.sqlplus.FrameMainMIDlet.commandAction(+33) at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282) at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10) at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68) at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47) at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.handleVmEvent(+186) at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+57)
看字面意思是提示数据表已存在,但是我明明是第一次创建。
附上源码希望高手能帮忙看看:
- Java code
package com.sqlplus;import java.util.Vector;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Form;import javax.microedition.lcdui.TextField;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;import br.com.dreamsource.mobile.jmesql.jdbcConnection;import br.com.dreamsource.mobile.jmesql.jdbcDriver;import br.com.dreamsource.mobile.jmesql.jdbcPreparedStatement;import br.com.dreamsource.mobile.jmesql.jdbcResultSet;import br.com.dreamsource.mobile.jmesql.jdbcStatement;import br.com.dreamsource.mobile.jmesql.exceptions.SQLException;public class FrameMainMIDlet extends MIDlet implements CommandListener { private Form mainForm; private static DatabaseManager myDatabaseManager; private UserPOJO userPOJO; TextField tf = new TextField("要写入的数据", "", 100, TextField.ANY); TextField cx = new TextField("查询条件", "", 100, TextField.ANY); TextField cxjg = new TextField("查询结果", "", 100, TextField.ANY); Command a_cmdSet = new Command("添加", Command.SCREEN, 0); Command w_cmdSet = new Command("修改", Command.SCREEN, 0); Command r_cmdSet = new Command("查询", Command.SCREEN, 0); Command c_cmdSet = new Command("创建表", Command.SCREEN, 0); Command exitCommand = new Command("退出", Command.EXIT, 0); public FrameMainMIDlet() { mainForm = new Form("嵌入式数据库"); mainForm.append(tf); mainForm.append(cx); mainForm.append(cxjg); mainForm.addCommand(exitCommand); mainForm.addCommand(c_cmdSet); mainForm.addCommand(r_cmdSet); mainForm.addCommand(w_cmdSet); mainForm.addCommand(a_cmdSet); mainForm.setCommandListener(this); }/* *//** * 检查数据库如果没有开始创建 * * @return * @throws Exception */ public void createDatabase(UserPOJO userPOJO) { jdbcDriver driver = new jdbcDriver(); // String userName = userPOJO.getUserName(); // String userPassword = userPOJO.getPasswd(); userPOJO.setUserName("SA"); userPOJO.setPasswd(""); userPOJO.setDbName(Consts.DATABASE_NAME); jdbcConnection connection; try { connection = driver.connect(userPOJO.getAsProperties()); jdbcStatement statement = connection.createStatement(); boolean l=statement.execute(Consts.CREATE_PRICETABLE_TBL); System.out.println(".............."+l); if(l==true){ tf.setString("创建表成功........."); boolean k=statement.execute("insert into priceTable values (null, 'tabela1', 10)"); if(k==true){ cx.setString("插入数据成功......."); try { jdbcPreparedStatement prepared =connection.prepareStatement("select name from priceTable"); jdbcResultSet res = prepared.executeQuery(); int i; StringBuffer Results = new StringBuffer(); this.cxjg.setString(""); System.out.println("数据查询"+Results); if (!(res == null)) { while (res.next()) { for (i = 1; i <= res.getColumnCount(); i++) { this.cxjg.setString(this.cxjg.getString() + res.getColumnName(i) + " -- " + res.getString(i) + "\n"); } this.cxjg.setString(this.cxjg.getString()); } } } catch (Exception e) { this.cxjg.setString(e.getMessage()); } }else{ cx.setString("插入数据失败......."); } }else{ tf.setString("创建表失败....."); } // boolean i=statement.execute(Consts.CREATE_CUSTOMER_TBL); } catch (SQLException e1) { // TODO 自动生成 catch 块 e1.printStackTrace(); } } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { // TODO 自动生成方法存根 } protected void pauseApp() { // TODO 自动生成方法存根 } protected void startApp() throws MIDletStateChangeException { // TODO 自动生成方法存根 this.userPOJO = new UserPOJO(); this.userPOJO.setConnectionURL(Consts.DATABASE_CONNETION_STRING); this.userPOJO.setDbName(Consts.DATABASE_NAME); if (DatabaseManager.databaseExists(Consts.DATABASE_NAME)) { Display.getDisplay(this).setCurrent(mainForm); } else { Display.getDisplay(this).setCurrent(mainForm); } } public void commandAction(Command command, Displayable displayable) { if (command == r_cmdSet) { }else if(command==c_cmdSet){ System.out.println("开始创建表----"); try { createDatabase( userPOJO); } catch (Exception e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } }}