bundle开发----数据库访问(基于MySQL)

来源:互联网 发布:听不懂程序员说的话 编辑:程序博客网 时间:2024/05/20 08:24

  看完基于串口和Socket的bundle实现,本文中我们来看看bundle中如何实现数据库的访问。

先来看看DatabaseBundle.java(数据库使用的是MySQL,并且已经创建了一个名为demo_db的数据库和一个名为user_table的表用来保存用户名和密码)

[java] view plaincopy
  1. package demo.database.bundle;  
  2.   
  3. import java.sql.*;  
  4.   
  5.   
  6. public class DatabaseBundle {  
  7.       
  8.     protected static Connection con  = null;  
  9.       
  10.     static String driver = "com.mysql.jdbc.Driver";  
  11.     static String PGUrl    = "jdbc:mysql://192.168.0.1/demo_db";  
  12.     static String PGUser   = "demo";  
  13.     static String PGPasswd = "demo";  
  14.       
  15.       
  16.     public static Statement stmt = null;  
  17.   
  18.     public static void openConnection() {  
  19.         try {  
  20.             Class.forName(driver);  
  21.             con = DriverManager.getConnection(PGUrl, PGUser, PGPasswd);  
  22.         } catch (Exception ex) {  
  23.             System.err.println("connection failed!");  
  24.             ex.printStackTrace();  
  25.             return;  
  26.         }  
  27.           
  28.           
  29.     }  
  30.       
  31.     public static void closeConnection()   
  32.     {  
  33.         if(con != null) {  
  34.             try {  
  35.                 con.close();  
  36.             } catch (Exception ex) {  
  37.                 return;  
  38.             }  
  39.         }  
  40.           
  41.     }  
  42.       
  43.       
  44.   
  45.     public static void updateUserTable(String id, String pass, String fullname)  
  46.     {  
  47.           
  48.         int idnumber   = 1;  
  49.           
  50.         try {  
  51.             String sql = "INSERT INTO user_table (id_number, id, pass, fullname) VALUES (" +  
  52.                             idnumber + "," +  
  53.                             id + "," +  
  54.                             fullname + ")";  
  55.   
  56.             //System.out.println(sql);  
  57.   
  58.             Statement stmt = con.createStatement();  
  59.   
  60.             stmt.executeUpdate(sql);  
  61.         } catch (Exception ex) {  
  62.             System.out.println("SQL Error!)");  
  63.             return;  
  64.         }  
  65.     }  
  66.       
  67.   
  68. }  

再来看看Activator.java

[java] view plaincopy
  1. package demo.database.bundle;  
  2.   
  3. import org.osgi.framework.BundleActivator;  
  4. import org.osgi.framework.BundleContext;  
  5.   
  6. public class Activator implements BundleActivator {  
  7.     
  8.   
  9.   public void start(BundleContext context) throws Exception {   
  10.       System.out.println("Database Bundle started!");  
  11.           DatabaseBundle.openConnection();  
  12.           DatabaseBundle.updateUserTable("0""123456""test_name");  
  13.  }  
  14.   
  15.   public void stop(BundleContext context) throws Exception {  
  16.       DatabaseBundle.closeConnection();  
  17.       System.out.println("Database Bundle stoped!");  
  18.   }  
  19. }  

运行该bundle, 即可发现user_table已经有记录被更新了,说明该bundle能访问数据库。

值得注意的一点是:只是把mysql-connector-java-5.1.22-bin.jar加进工程的lib之后编译和生成bundle没有问题,但启动bundle时会出错。必须在bundle.manifest文件中,把下面红色部分(com.mysql.jdbc)import进来才可以(如下所示)。而使用postgresql-jdbc-8.4.701.jar来访问基于PostgreSQL数据库时则无需Import此选项也没有问题。

Import-Package: org.osgi.framework,com.mysql.jdbc

0 0
原创粉丝点击