移動聯系治理器 這是一個由PointBase提供的移動聯系治理器的例子。聯系治理器 contact manager包括在PointBase 4.x中。為了讀者方便,我已經把源代碼打包成zip文件放在Resource中。假如你想編譯和運行例子,你必須先從PointBase處下載適當的jar文件。 這個應用程序本身比較簡單。它主要沿用了高級地址本應用程序的通用特性。例如,它答應用戶存儲聯系人名字,地址和電話號碼;提供自覺瀏覽和搜索接口;和后臺數據庫服務器同步。圖1和圖2分別顯示了該應用程序在標準模式和同步模式下的操作。這些屏幕快照來自一個由Insignia’s Jeode Personaljava VM驅動的Pocket PC 和一個由J2SE驅動的Mac OS X 膝上型電腦。相同字節代碼的應用程序沒有經過修改運行在許多平臺上,證實了Java的威力。
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(); }
// If the database is newly created, load the schema. boolean newdb=initDatabase(); // Load sample data for the new tables. if(newdb) { SampleDataCreator.insert(connection); }
// 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(); } }