【发布时间】:2013-07-14 13:42:51
【问题描述】:
我下载了Postgresql-9.2-1003.jdbc3.jar,放到felix\bundle目录下。
我的程序访问 Postgres 表 EMP 并打印它。我正在尝试在 Felix OSGi 服务器中执行此操作。我的程序有两个部分:
-
第 1 部分程序,它简单地连接到 Postgres JDBC 驱动程序并打开数据库:
包 com.myprogram.myemp;
导入 java.sql.Connection; 导入 java.sql.DriverManager; 导入java.sql.ResultSet; 导入 java.sql.Statement; 导入 org.postgresql.Driver;
公共类 ConnectPostgres {
static final String DB_URL = "jdbc:postgresql://localhost:5432/scott"; static final String UNAME = "postgres"; static final String PWORD = "password"; public void myMain() { Connection conn = null; ResultSet rs = null; Statement st = null; String JDBC_DRIVER = Driver.class.getName(); try { Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, UNAME, PWORD); st = conn.createStatement(); rs = st.executeQuery("select * from EMP"); while (rs.next()) { System.out.println ("EMP Name:" + rs.getLong("EMPNO") + " " + rs.getString("ENAME") ); } } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); st.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } }}
-
第 2 部分计划更像是作为服务提供者启动捆绑包:
包 com.myprogram.myemp;
导入 org.osgi.framework.BundleContext; 导入 org.osgi.framework.BundleActivator;
公共类激活器实现BundleActivator {
@Override public void start(BundleContext arg0) throws Exception { ConnectPostgres app = new ConnectPostgres(); app.myMain(); } @Override public void stop(BundleContext arg0) throws Exception { }}
要求是: 使用 Postgres 或 SQLite 等流行数据库的数据库连接,我应该能够在符合 OSGi 的服务器 Felix、Equinox 上将 EMP 表作为服务发布。
**我在 Felix 3.0 中遇到的错误是:
不满足的要求:
(&(package=org.postgresql))**
驱动在那里,我放在bundle目录下。
我认为的问题:
在 OSGi 中无法使用 JDBC 进行数据库连接。 OSGi 可以连接到数据库吗?规范、维基、示例似乎都没有提及。没有它,所有示例看起来都像摄氏到华氏温度转换程序,对业务没有实际价值。如果我对 OSGi 的理解有误,请纠正我。
我做错了什么?我应该尝试连接到数据库的其他方式是什么。
提前致谢
【问题讨论】:
-
为什么您认为只需将驱动程序 JAR 放在
bundle目录中就足以将该驱动程序安装到 OSGi 框架中?? -
关于课程的第一个问题... OSGi 可以连接到数据库。这没有明确指出,因为它不需要。相反,如果 OSGi 无法连接到数据库,您不认为会非常明确地指出吗?
-
+1 for Neil,尽管将 JAR 放到 bundle 目录中并期望它成为一个 bundle 似乎很直观。不是 100% 确定他们的设置,但如果不是 postgresql 驱动程序没有“准备好 OSGI”,情况可能就是这样。
-
另一点...问题说您使用的是 Felix 3.0。那太旧了,如果可能的话,你应该升级到 Felix 4.2(尽管它不能解决这个特定的问题)。
标签: database postgresql jdbc osgi apache-felix