WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术进阶教程高可靠性移动应用程序-移动数据库和J2ME工具(2)

高可靠性移动应用程序-移动数据库和J2ME工具(2)
作者:翻译作者:jungleguo  来源:转载  发布时间:2005-8-10 11:39:36

原文: http://www.javaworld.com/javaworld/jw-06-2003/jw-0606-wireless-p2.html

一个应用程序例子


现在通过一个简单的例子,我们检测一下移动数据库应用程序的典型用法和关键组件。
移动联系管理器

这是一个由PointBase提供的移动联系管理器的例子。联系管理器 contact manager包括在PointBase 4.x中。为了读者方便,我已经把源代码打包成zip文件放在Resource中。如果你想编译和运行例子,你必须先从PointBase处下载适当的jar文件。
这个应用程序本身比较简单。它主要沿用了高级地址本应用程序的通用特性。例如,它允许用户存储联系人名字,地址和电话号码;提供自觉浏览和搜索接口;和后台数据库服务器同步。图1和图2分别显示了该应用程序在标准模式和同步模式下的操作。这些屏幕快照来自一个由Insignia’s Jeode PersonalJava VM驱动的Pocket PC 和一个由J2SE驱动的Mac OS X 膝上型电脑。相同字节代码的应用程序没有经过修改运行在许多平台上,证明了Java的威力。


图1 在袖珍PC Jeode PersonalJava上的标准联系管理器


图2 在Mac OS X上的两个同步的联系管理器spoke

客户端应用程序UI(用户界面)是用AWT写的。这是被PersonalJava或J2ME/FP/PP设备所支持的唯一标准UI库。除了这些UI驱动,我们还有另一个代码层,它提供访问一般的设备上JDBC数据库。这个数据库访问层也提供了与后台服务器同步移动数据的逻辑,它是通过PointBase专有UniSync同步服务器来实现的。现在我们来看看数据访问层的代码,它包括在一个单独的类:DBManager.

设备上的数据访问

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

例1 连接移动数据库和初始化访问对象
  1. class DBManager {
  2. // DBManager is a singleton class.
  3. private static DBManager instance;
  4. private String driver;
  5. private String url;
  6. private String user;
  7. private String password;
  8. private boolean delay;
  9. private Connection connection;
  10. private Statement statement;
  11. private PreparedStatement insert;
  12. private PreparedStatement find;
  13. private PreparedStatement delete;
  14. private PreparedStatement update;
  15. private PreparedStatement all;
  16. static DBManager getInstance() {
  17. if (instance == null) {
  18. instance = new DBManager();
  19. }
  20. return instance;
  21. }
  22. private DBManager() {
  23. // Get parameters from runtime properties.
  24. // This allows us to switch to different JDBC databases
  25. // without changing the application code.
  26. Properties properties = ContactManager.getProperties();
  27. driver =
  28. properties.getProperty("driver", "com.pointbase.me.jdbc.jdbcDriver");
  29. url =
  30. properties.getProperty("url", "jdbc:pointbase:micrpbdemo");
  31. user =
  32. properties.getProperty("user", "PBPUBLIC");
  33. password =
  34. properties.getProperty("password", "PBPUBLIC");
  35. delay =
  36. properties.getProperty("delayread","true").equals("true");
  37. connect();
  38. }
  39. private void connect() {
  40. try {
  41. // Load the driver class.
  42. Class.forName(driver);
  43. // If the database doesn't exist, create a new database.
  44. connection = DriverManager.getConnection(url, user, password);
  45. // Create template statement objects.
  46. statement = connection.createStatement();
  47. createStatement();
  48. // If the database is newly created, load the schema.
  49. boolean newdb=initDatabase();
  50. // Load sample data for the new tables.
  51. if(newdb) {
  52. SampleDataCreator.insert(connection);
  53. }
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. System.exit(1);
  57. }
  58. }
  59. void disconnect() {
  60. try {
  61. connection.commit();
  62. statement.close();
  63. insert.close();
  64. find.close();
  65. delete.close();
  66. update.close();
  67. all.close();
  68. connection.close();
  69. System.exit(0);
  70. } catch (Exception e) {
  71. e.printStackTrace();
  72. System.exit(1);
  73. }
  74. }
  75. // Create the table and load the schema.
  76. private boolean initDatabase() {
  77. try {
  78. String sql = "CREATE TABLE NameCard (ID INT PRIMARY KEY, "+
  79. "Name VARCHAR(254), Company VARCHAR(254), Title VARCHAR(254), "+
  80. "Address1 VARCHAR(254), Address2 VARCHAR(254), "+
  81. "Phone VARCHAR(254), Email VARCHAR(254), "+
  82. "Picture Binary(1000000))";
  83. // If the table already exists, this will throw an exception.
  84. statement.executeUpdate(sql);
  85. // This means the database already exists.
  86. return true;
  87. } catch (SQLException e) {
  88. // Ignore the error - the table already exists, which is good
  89. // so we don't need to add demo data later on.
  90. return false;
  91. }
  92. }
  93. // Create statement templates.
  94. private void createStatement() {
  95. try {
  96. insert = connection.prepareStatement(
  97. "INSERT INTO NameCard (ID, Name, Company, Title, Address1, "+
  98. "Address2, Phone, Email, Picture) "+
  99. "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
  100. find = connection.prepareStatement(
  101. "SELECT * FROM NameCard WHERE (Name LIKE ?) "+
  102. "AND (Company LIKE ?) AND (Title LIKE ?) "+
  103. "AND ((Address1 LIKE ?) OR (Address2 LIKE ?)) "+
  104. "AND (Phone LIKE ?) AND (Email LIKE ?)");
  105. delete = connection.prepareStatement(
  106. "DELETE FROM NameCard WHERE ID = ?");
  107. update = connection.prepareStatement(
  108. "UPDATE NameCard SET ID=?, Name=?, Company=?, Title=?, "+
  109. "Address1=?, Address2=?, Phone=?, Email=?, Picture=? "+
  110. "WHERE ID = ?");
  111. all = connection.prepareStatement(
  112. "SELECT ID, Name, Company, Title, Address1, Address2, "+
  113. "Phone, Email FROM NameCard");
  114. } catch (SQLException e) {
  115. e.printStackTrace();
  116. }
  117. }
  118. // Other methods.
  119. }

[1] [2] [3] [4]  下一页

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码