13,097
社区成员




开始创建表----
..............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;
}
}
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 = ?";
}