Apache Felix无法访问Postgres JDBC [英] Apache Felix not able to access Postgres JDBC

查看:97
本文介绍了Apache Felix无法访问Postgres JDBC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载了Postgresql-9.2-1003.jdbc3.jar并将其放在felix \ bundle目录中.

I downloaded Postgresql-9.2-1003.jdbc3.jar and put it in felix\bundle directory.

我的程序访问Postgres表EMP并进行打印.我正在尝试在Felix OSGi服务器中执行此操作.我的程序分为两个部分:

My program accesses Postgres table EMP and prints it. I am trying to do it in Felix OSGi server. There are two parts of my program:

  1. Part-1程序,该程序仅连接到Postgres JDBC驱动程序并打开数据库:

  1. Part-1 program which simply connects to Postgres JDBC driver and opens the database:

package com.myprogram.myemp;

package com.myprogram.myemp;

import java.sql.Connection; 导入java.sql.DriverManager; 导入java.sql.ResultSet; 导入java.sql.Statement; 导入org.postgresql.Driver;

import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import org.postgresql.Driver;

公共类ConnectPostgres {

public class 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部分程序更像是将捆绑软件作为服务提供商启动:

Part-2 program is more like launches a bundle as a service provider:

package com.myprogram.myemp;

package com.myprogram.myemp;

导入org.osgi.framework.BundleContext; 导入org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext; import org.osgi.framework.BundleActivator;

公共类Activator实现BundleActivator {

public class Activator implements 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这样的流行数据库的数据库连接,我应该能够将EMP表作为服务发布到OSGi兼容服务器Felix Equinox上.

The requirement is: Using database connection of a popular database like Postgres or SQLite, I should be able to publish EMP table as a Service on a OSGi compliant server Felix, Equinox.

**我在Felix 3.0中遇到的错误是:

**The error which I am getting in Felix 3.0 is:

(&(package = org.postgresql))**

(&(package=org.postgresql))**

驱动程序在那里,我将其放置在bundle目录下.

The driver is there, I placed it under bundle directory.

在我看来,这个问题是

  1. 在OSGi中无法使用JDBC进行数据库连接. OSGi可以连接到数据库吗?规范,Wiki,示例似乎都没有提及.没有它们,所有示例看起来都像是摄氏温度到华氏温度的转换程序,对商业没有真正的价值.如果我对OSGi的理解是错误的,请纠正我.

  1. Database connections using JDBC are not possible in OSGi. Can OSGi connect to Databases? The specification, wikis, examples all seem to be silent about. Without which all examples look like Celsius to Fahrenheit temperature conversion programs, of no real value to business. Please correct me if my understanding about OSGi is wrong.

我可能做错了什么?我应该尝试连接数据库的另一种方式是什么.

What could be I am doing wrong? What is the other way I should try to connect to database.

预先感谢

推荐答案

Postgresql-9.2-1003.jdbc3.jar JAR可能不是OSGI捆绑包,因此您不能以自己的方式安装它.

The Postgresql-9.2-1003.jdbc3.jar JAR is probably not an OSGI bundle, so you can't just install it the way you have.

在这里检查:

https://ops4j1.jira.com/wiki/display/PAXJDBC/PostgreSQL + Driver +适配器

Maven官方工件postgresql:postgresql是一个没有OSGi清单标头的普通旧JAR.您将不得不使用Pax URL wrap:处理程序动态地对此进行包装,或者构建自己的捆绑软件,并添加OSGi清单. Pax Tipi项目将填补这一空白.

The official Maven artifact postgresql:postgresql is a plain old JAR without OSGi manifest headers. You will have to wrap this on the fly using the Pax URL wrap: handler, or build your own bundle, adding an OSGi manifest. This gap is to be filled by the Pax Tipi project.

这篇关于Apache Felix无法访问Postgres JDBC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆