在OSGi项目中使用SqLite JDBC Jar

时间:2015-03-24 13:38:37

标签: java jdbc osgi bndtools

我正在尝试学习OSGi,我做了一个简单的项目,它只是在Sqlite DB中创建一个表。我在构建路径中添加了“sqlite-jdbc-3.7.2.jar”。但是当我运行该项目时,它会显示“ClassNotFoundException”。

我将Eclipse IDE与BndTools一起使用。

我搜索了其他帖子,有些人说将jar添加为另一个包,然后将其导出。我尝试过这个。在工作区中,我选择了Import->插件和片段 - >选择目录。但它没有列出Jar(适用于其他Jars,但未检测到sqlite-jdbc-3.7.2.jar)。

我在下面做过,

se.sample.sqlitedbconn.ConnActivator.java

  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.Statement;
  import org.osgi.framework.BundleActivator;
  import org.osgi.framework.BundleContext;
  import se.sample.connproperties.CreateTable;
  import aQute.bnd.annotation.component.*;

 @Component
 public class ConnActivator implements BundleActivator {

Connection connection;
Statement statement;

@Activate
public void start(BundleContext context) throws Exception {
    System.out.println("Starting");


    try {
        Class.forName("org.sqlite.JDBC");
        connection = DriverManager.getConnection("jdbc:sqlite://home/raj/Desktop/EclipseProjectsWorkspace/ZWave.db");
    } catch (Exception ex) {
        System.err.println(ex.getClass().getName()+": "+ex.getMessage());
    }

    System.out.println("Connection To Database Successful");

    CreateTable createTable = new CreateTable();
    statement = connection.createStatement();
    boolean isTableCreated = createTable.createANewTable(connection,statement);

        if(isTableCreated){
             System.out.println("Table Created Successfully!");
        }else{
             System.out.println("Table Creation Failed!");
        }

        statement.close();
    }

     @Deactivate
     public void stop(BundleContext context) throws Exception {
         System.out.println("Stopping");

        if(connection != null){
            connection.close();
        }

         System.out.println("Database Connection Closed!");
    }

 }

se.sample.connproperties.CreateTable.java

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class CreateTable {


     public boolean createANewTable(Connection connection, Statement statement) {
    String sql =    "CREATE TABLE Product " +
                    "(ProductId INT PRIMARY KEY  NOT NULL,"+
                    "ProductName TEXT NOT NULL,"+
                    "ProductPrice REAL);";
    try {
        statement.executeUpdate(sql);
    } catch (SQLException e) {
        System.err.println(e.getClass().getName()+": "+e.getMessage());
        return false;
    }
    return true;
   }

 }

当我运行此操作时,我收到以下错误:

Starting
Connection To Database Successful
java.lang.ClassNotFoundException: org.sqlite.JDBC not found by se.sample.sqlitedbconn [8]
! Failed to start bundle se.sample.sqlitedbconn-0.0.0.201503241305, exception activator error null from: se.sample.sqlitedbconn.ConnActivator:start#34
 ____________________________
 Welcome to Apache Felix Gogo

 g! 

请帮助我:)

2 个答案:

答案 0 :(得分:1)

请查看一个很棒的OSGi教程here,它非常好地描述了数据库连接和OSGi。

祝你好运

答案 1 :(得分:0)

DriverManager使用OSGI类加载器(因为调用者是从OSGI容器加载的)。因为从OSGI的类加载器中看不到org.sqlite.JDBC,所以你有一个ClassNotFoundException。

SQLite JDBC的3.8.11.2版本现在是一个OSGI包,因此如果您将SQLite JDBC jar部署为OSGI包而不是捆绑包的嵌入式jar,它应该可以工作。