开发者

JDBC Driver in OSGI (Eclipse IDE)

开发者 https://www.devze.com 2023-02-06 23:00 出处:网络
I am having some problems with getting my OSGI programs to recognzie/utilize the mysql jdbc driver. I have a bundle that is speficcally for entering data into a mysql database. I have copied over all

I am having some problems with getting my OSGI programs to recognzie/utilize the mysql jdbc driver.

I have a bundle that is speficcally for entering data into a mysql database. I have copied over all the same methods as in a test program (non-OSGI). I am not able to create a connection suing DriverManager.getConnection().

I have added the driver to the class path andhave tried all the solutions on this site such as using Class.forName(). Possibly I am inputting the wrong string arg into forName().

public void createConn(String URL, String DBName, String username, String password){

 try {
   Class.forName("my开发者_StackOverflow中文版sql-connector-java-5.1.14-bin.jar");
  } catch (ClassNotFoundException e1) {
   e1.printStackTrace();
  }
  try {
   conn = DriverManager.getConnection(URL + DBName,username,password);
   System.out.println("Connection Created");
   stmt = conn.createStatement();
   System.out.println("Statement Created");
   //data = new ApplianceData();

   //flag = true;
   //this.writeThread = new Thread();
   //writeThread.start();

  } catch (SQLException e) {
   System.err.println(e.getMessage());
  }
}
  • Can someone tell me the argument they used in Class.forName();

  • Does anybody have a solution to this problem or encountered this?


Thanks, that took care of the classNotFound Exception. Now I have an error stating that the driver is not suitable. I know OSGI has some issues with drivers etc. Can someone recommend a way to circumvent this?

I have placed the jdbc jar in the java installation bin folders, and in the bin folder of the bundle.

ClassLoader DBHCL = ClassLoader.getSystemClassLoader();
DBHCL.loadClass("com.mysql.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver", true, DBHCL).newInstance();
System.out.println("Class Loaded");
//DriverManager.getDriver("jdbc:mysql://localhost/timedb");
//System.out.println("Driver Gotten");
conn = DriverManager.getConnection(URL + DBName,username,password);
System.out.println("Connection Created");
stmt = conn.createStatement();
System.out.println("Statement Created");
connFlag = true;

Console Output, Error: osgi> start 7 Data Base Service (MYSQL) Starting Class Loaded No suitable driver found for jdbc:mysql://localhost/timedb Exception in thread "Thread-1" INSERT INTO appliance1...

Does anybody have any insight into this problem?

Thanks


Class.forName(String) takes a fully qualified class name, not a jar file. You should use something like

Class.forName("com.mysql.jdbc.Driver");


i know this has 3 years but i'm answering just in case someone googles it. so the problem is that the bundle does not see the jar from the classloade, so you need to import its packages in the manifest. the way i did, is i created a separate bundle containing mysql connector jar. newProject> plugin from existing jar; then i added all of its packages to "exported packages" in its manifest file. Then in my first bundle i added all the packages of the connector to "imported packages" and it worked.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号