移动联系治理器 这是一个由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(); } }