I downloaded Postgresql-9.2-1003.jdbc3.jar and put it in felix\bundle directory.
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:
Part-1 program which simply connects to Postgres JDBC driver and opens the database:
package com.myprogram.myemp;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import org.postgresql.Driver;
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(); } } }
}
Part-2 program is more like launches a bundle as a service provider:
package com.myprogram.myemp;
import org.osgi.framework.BundleContext; import org.osgi.framework.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 { }
}
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.
**The error which I am getting in Felix 3.0 is:
Unsatisfied requirement(s):
(&(package=org.postgresql))**
The driver is there, I placed it under bundle directory.
The problem as it seems to me:
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.
Thanks in advance