对我来说算是挑战性的一个J2ME数据存储的问题,高手请进!

老猫的TOM 2010-12-31 11:13:09
一直在做J2EE的开发,最近公司拉了一个J2ME手持机<PDA>(其实和手机差不多)开发项目,本人算是第一次做这样的项目,如果问题问的有些弱还希望高手不要见笑。
下面开始说问题:
PAD使用的是WTK平台,要实现的功能是:对数据进行保存、修改、删除、查询,由于环境的问题,程序要求在无网络的情况下能够正常运行,所以运行环境算是单机版的。
数据格式很简单包含:档案ID、卡号、用户名、金额。
数据量最少在一万条以上,由于只能存储在本机环境内,这对于手持机来说不是件容易的事情。起先准备使用操作本地文本文件的方式存储(txt文件,每一行为一条数据)但是j2me对文本文件的操作真是太低级。对于查询和修改数据不易操作,而且数据过大会导致内存溢出。
所以本人准备尝试使用嵌入式数据库来完成,目前选用的数据库位JMESQL,网上对于这个数据库的操作知识并不是很多,只能在官方找到所谓的例子,可是操作中创建表一直不成功。后台打印异常为:
开始创建表----
..............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)

看字面意思是提示数据表已存在,但是我明明是第一次创建。
附上源码希望高手能帮忙看看:


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();
}

}

}

}


package com.sqlplus;

import br.com.dreamsource.mobile.jmesql.io.Properties;

public class UserPOJO {


private String userName;
private String passwd;
private String dbName;
private String connectionURL;

public UserPOJO() {
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPasswd() {
return passwd;
}

public void setPasswd(String passwd) {
this.passwd = passwd;
}

public String getDbName() {
return dbName;
}

public void setDbName(String dbName) {
this.dbName = dbName;
}

public String getConnectionURL() {
return connectionURL;
}

public void setConnectionURL(String connectionURL) {
this.connectionURL = connectionURL;
}

public Properties getAsProperties() {
Properties p = new Properties();
p.setProperty("user", this.userName);
p.setProperty("password", this.passwd);
p.setProperty("database", this.dbName);
return p;
}

}
...全文
261 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Manfeel 2011-07-06
  • 打赏
  • 举报
回复
兄弟,我最近也在研究jMeSQL,现在碰到一个更头痛的问题,用insert插入的记录,退出程序后,记录消失。
我查了一下jMeSQL的文档(如果那也算是文档的话!),可能是创建的table默认都在memeory中,于是用
Create cached table,结果,insert数据直接抛异常,我发现是在Cache.java代码中,里面估计是存在某些bug,找了很久也没有结果,我的QQ:179813134,希望交流
老猫的TOM 2011-01-12
  • 打赏
  • 举报
回复
本人循环插入数据,貌似达到七千五百行左右报内存溢出。
老猫的TOM 2011-01-12
  • 打赏
  • 举报
回复
问题已解决,不是代码问题,本人也不知为何数据表创建成功返回的boolea值却是false,会不会是javamesql的一个bug?
kazeik 2011-01-11
  • 打赏
  • 举报
回复
对数据库不了解.
老猫的TOM 2011-01-04
  • 打赏
  • 举报
回复
谢谢上面的两位,但本人感觉很孤独,有没有其他高手进来看看
老猫的TOM 2010-12-31
  • 打赏
  • 举报
回复

package com.sqlplus;

public class Consts {

public static final int UPDATE_TYPE_INSERT = 1;
public static final int UPDATE_TYPE_UPDATE = 2;
public static final int UPDATE_TYPE_DELETE = 3;

public static final String DATABASE_NAME = "worders";
public static final String DATABASE_CONNETION_STRING = "jdbc:mesqldb:worders";

public static final String DROP_USER = "DROP USER ?";
public static final String CREATE_USER = "CREATE USER ? PASSWORD ? admin";

public static final String DROP_PRICTABLE_TBL = "DROP TABLE priceTable";
public static final String CREATE_PRICETABLE_TBL =
"CREATE TABLE priceTable (" +
" id int identity, " +
" name varchar(20) not null, " +
" percent float not null," +
" primary key(id)" +
")";

public static final String PRICETABLE_ID = "id";
public static final String PRICETABLE_NAME = "name";
public static final String PRICETABLE_PERCENT = "percent";

public static final String PRICETABLE_SELECT_BY_EDIT = "SELECT * FROM pricetable";

public static final String DROP_CUSTOMER_TBL = "DROP TABLE customer";
public static final String CREATE_CUSTOMER_TBL =
"CREATE TABLE customer (" +
" id int identity," +
" remoteId int," +
" priceTableId int," +
" dtRecord date," +
" name varchar(80) not null," +
" cpf varchar(11)," +
" cnpj varchar(14)," +
" phone varchar(10) not null," +
" fax varchar(10)," +
" contact varchar(80) not null," +
" mail varchar(80)," +
" zipCode varchar(8) not null," +
" credit float," +
" recordState int not null," +
" primary key(id)," +
" foreign key(priceTableId) references priceTable(id)" +
")";

public static final String CUSTOMER_ID = "id";
public static final String CUSTOMER_REMOTEID = "remoteid";
public static final String CUSTOMER_PRICETABLEID = "pricetableid";
public static final String CUSTOMER_DTRECORD = "dtrecord";
public static final String CUSTOMER_NAME = "name";
public static final String CUSTOMER_CPF = "cpf";
public static final String CUSTOMER_CNPJ = "cnpj";
public static final String CUSTOMER_PHONE = "phone";
public static final String CUSTOMER_FAX = "fax";
public static final String CUSTOMER_CONTACT = "contact";
public static final String CUSTOMER_MAIL = "mail";
public static final String CUSTOMER_ZIPCODE = "zipcode";
public static final String CUSTOMER_CREDIT = "credit";
public static final String CUSTOMER_RECORDSTATE = "recordstate";

public static final String CUSTOMER_LOCATE_BY_FIELD = "SELECT id, name FROM customer";
public static final String CUSTOMER_SELECT_BY_EDIT =
"SELECT priceTableId, name, cpf, cnpj, phone, fax, contact, mail, " +
"zipCode, credit FROM customer";

public static final String CUSTOMER_INSERT =
"INSERT INTO customer (remoteId, priceTableId, dtRecord, name, cpf, cnpj, phone, fax, " +
"contact, mail, zipCode, credit, recordState) VALUES (0, ?, CURDATE(), ?, ?, ?, ?, ?, ?, ?, ?, ?, 0)";

public static final String CUSTOMER_UPDATE =
"UPDATE customer set priceTableId = ?, name = ?, cpf = ?, cnpj = ?, phone = ?, fax = ?, " +
"contact = ?, mail = ?, zipCode = ?, credit = ? where id = ?";

public static final String CUSTOMER_DELETE =
"DELETE FROM customer where id = ?";


}



以上是我项目中的所有源码,如果大家对于存储还有其他什么好的方法或建议,麻烦能写出来参考一下,谢谢了。
softice_ 2010-12-31
  • 打赏
  • 举报
回复
没搞过嵌入式数据库,帮顶
xiang1115 2010-12-31
  • 打赏
  • 举报
回复


我也正在研究jmesql,就是不知道怎么能创建表格。

13,097

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧