鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 手机系统开发 > J2ME > >

移动数据库和J2ME工具探讨

来源:互联网 作者:佚名 时间:2015-08-02 08:07
欢迎进入IT技术社区论坛,与200万技术人员互动交流 >>进入 设备上的数据访问 类DBManager是一个单独的类,它提供从应用程序单点访问数据。这个单独模式避免了嵌入式数据库的线程复杂性。下面的代码片断显示了DBManager的构造器和初始化的代码。它连接数据库
欢迎进入IT技术社区论坛,与200万技术人员互动交流 >>进入
设备上的数据访问

  类DBManager是一个单独的类,它提供从应用程序单点访问数据。这个单独模式避免了嵌入式数据库的线程复杂性。下面的代码片断显示了DBManager的构造器和初始化的代码。它连接数据库,定义表,将测试数据导入表中,创建为以后时候的SQL状态模版(PreparedStatement)。正如我们所看到的,这里用到的都是标准JDBC。对于企业Java 开发者下面的代码应该很容易明白:

  例1 连接移动数据库和初始化访问对象

class DBManager {
// DBManager is a singleton class.
private static DBManager instance;
private String driver;
private String url;
private String user;
private String password;
private boolean delay;
private Connection connection;
private Statement statement;
private PreparedStatement insert;
private PreparedStatement find;
private PreparedStatement delete;
private PreparedStatement update;
private PreparedStatement all;

static DBManager getInstance() {
if (instance == null) {
instance = new DBManager();
}
return instance;
}

private DBManager() {
// Get parameters from runtime properties.
// This allows us to switch to different JDBC databases
// without changing the application code.
Properties properties = ContactManager.getProperties();
driver =
properties.getProperty("driver", "com.pointbase.me.jdbc.jdbcDriver");
url =
properties.getProperty("url", "jdbc:pointbase:micro:pbdemo");
user =
properties.getProperty("user", "PBPUBLIC");
password =
properties.getProperty("password", "PBPUBLIC");
delay =
properties.getProperty("delayread","true").equals("true");
connect();
}

private void connect() {
try {
// Load the driver class.
Class.forName(driver);

// If the database doesn't exist, create a new database.
connection = DriverManager.getConnection(url, user, password);

// Create template statement objects.
statement = connection.createStatement();
createStatement();

// If the database is newly created, load the schema.
boolean newdb=initDatabase();
// Load sample data for the new tables.
if(newdb) {
SampleDataCreator.insert(connection);
}

} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}

void disconnect() {
try {
connection.commit();
statement.close();
insert.close();
find.close();
delete.close();
update.close();
all.close();
connection.close();
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}

// Create the table and load the schema.
private boolean initDatabase() {
try {
String sql = "CREATE TABLE NameCard (ID INT PRIMARY KEY, "+
"Name VARCHAR(254), Company VARCHAR(254), Title VARCHAR(254), "+
"Address1 VARCHAR(254), Address2 VARCHAR(254), "+
"Phone VARCHAR(254), Email VARCHAR(254), "+
"Picture Binary(1000000))";
// If the table already exists, this will throw an exception.
statement.executeUpdate(sql);
// This means the database already exists.
return true;
} catch (SQLException e) {
// Ignore the error - the table already exists, which is good
// so we don't need to add demo data later on.
return false;
}
}

// Create statement templates.
private void createStatement() {
try {
insert = connection.prepareStatement(
"INSERT INTO NameCard (ID, Name, Company, Title, Address1, "+
"Address2, Phone, Email, Picture) "+
"valueS (?, ?, ?, ?, ?, ?, ?, ?, ?)");
find = connection.prepareStatement(
"SELECT * FROM NameCard WHERE (Name LIKE ?) "+
"AND (Company LIKE ?) AND (Title LIKE ?) "+
"AND ((Address1 LIKE ?) OR (Address2 LIKE ?)) "+
"AND (Phone LIKE ?) AND (Email LIKE ?)");
delete = connection.prepareStatement(
"DELETE FROM NameCard WHERE ID = ?");
update = connection.prepareStatement(
"UPDATE NameCard SET ID=?, Name=?, Company=?, Title=?, "+
"Address1=?, Address2=?, Phone=?, Email=?, Picture=? "+
"WHERE ID = ?");
all = connection.prepareStatement(
"SELECT ID, Name, Company, Title, Address1, Address2, "+
"Phone, Email FROM NameCard");
} catch (SQLException e) {
e.printStackTrace();
}
}

// Other methods.

}

  在DBManager中的其他方法通过简单JDBC API调用进行访问数据库。如下的代码片断展示了搜索和操纵名称卡片记录的方法。这些方法使用了我们之前定义的SQL模版。

  例2 数据访问方法

Vector findNameCardsByKeyword(String name, String company,
String title, String address1, String address2,
String phone, String email) {
Vector NameCards = new Vector();
String[] keywords = {name, company, title, address1, address2,
phone, email};
try {
for (int i = 0; i < keywords.length; i++) {
String criteria = (keywords[i].equals("")) ? "%" :
"%" + keywords[i] + "%";
find.setString(i + 1, criteria);
}
ResultSet resultSet = find.executeQuery();
while (resultSet.next()) {
NameCard nameCard = new NameCard(resultSet.getInt(1),
resultSet.getString(2), resultSet.getString(3),
resultSet.getString(4), resultSet.getString(5),
resultSet.getString(6),
resultSet.getString(7), resultSet.getString(8));
if (!delay)
loadPicture(nameCard);
NameCards.addElement(nameCard);
}
} catch (SQLException e) {
e.printStackTrace();
}
return NameCards;
}

void addNameCard(NameCard nameCard) {
nameCard.setID(getNewID());
try {
insert.setInt(1, nameCard.getID());
insert.setString(2, nameCard.getName());
insert.setString(3, nameCard.getCompany());
insert.setString(4, nameCard.getTitle());
insert.setString(5, nameCard.getAddress1());
insert.setString(6, nameCard.getAddress2());
insert.setString(7, nameCard.getPhone());
insert.setString(8, nameCard.getEmail());
insert.setBytes(9, nameCard.getPicture().getBytes());
insert.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}

void updateNameCard(NameCard nameCard) {
try {
update.setInt(1, nameCard.getID());
update.setString(2, nameCard.getName());
update.setString(3, nameCard.getCompany());
update.setString(4, nameCard.getTitle());
update.setString(5, nameCard.getAddress1());
update.setString(6, nameCard.getAddress2());
update.setString(7, nameCard.getPhone());
update.setString(8, nameCard.getEmail());
update.setBytes(9, nameCard.getPicture().getBytes());
update.setInt(10, nameCard.getID());
update.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}

void deleteNameCard(NameCard nameCard) {
try {
delete.setInt(1, nameCard.getID());
delete.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}

void loadPicture(NameCard nameCard) {
try {
ResultSet resultSet =
statement.executeQuery(
"SELECT Picture FROM NameCard WHERE ID = " +
nameCard.getID());
resultSet.next();
Picture picture = new Picture();
picture.setBytes(resultSet.getBytes(1));
nameCard.setPicture(picture);
} catch (SQLException e) {
e.printStackTrace();
}
}

private int getNewID() {
try {
ResultSet resultSet = statement.executeQuery(
"SELECT MAX(ID)+1 FROM NameCard");
if (resultSet.next()) {
return resultSet.getInt(1);
} else {
return 0;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}

  [1] [2] [3] 

网友评论
<