一個應用程序例子
現在通過一個簡單的例子,我們檢測一下移動數據庫應用程序的典型用法和關鍵組件。
移動聯系管理器
這是一個由PointBase提供的移動聯系管理器的例子。聯系管理器 contact manager包括在PointBase 4.x中。為了讀者方便,我已經把源代碼打包成zip文件放在Resource中。如果你想編譯和運行例子,你必須先從PointBase處下載適當的jar文件。
這個應用程序本身比較簡單。它主要沿用了高級地址本應用程序的通用特性。例如,它允許用戶存儲聯系人名字,地址和電話號碼;提供自覺瀏覽和搜索接口;和后臺數據庫服務器同步。圖1和圖2分別顯示了該應用程序在標準模式和同步模式下的操作。這些屏幕快照來自一個由Insignia’s Jeode PersonalJava VM驅動的Pocket PC 和一個由J2SE驅動的Mac OS X 膝上型電腦。相同字節代碼的應用程序沒有經過修改運行在許多平臺上,證明了Java的威力。
可靠性移動應用程序---J2ME工具(2)(圖一)" />
客戶端應用程序UI(用戶界面)是用AWT寫的。這是被PersonalJava或J2ME/FP/PP設備所支持的唯一標準UI庫。除了這些UI驅動,我們還有另一個代碼層,它提供訪問一般的設備上JDBC數據庫。這個數據庫訪問層也提供了與后臺服務器同步移動數據的邏輯,它是通過PointBase專有UniSync同步服務器來實現的?,F在我們來看看數據訪問層的代碼,它包括在一個單獨的類:DBManager.
設備上的數據訪問
類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)
loadPic