OSGI (Eclipse IDE) 中的 JDBC 驱动程序
我在让 OSGI 程序识别/利用 mysql jdbc 驱动程序时遇到一些问题。
我有一个专门用于将数据输入 mysql 数据库的包。我已经复制了与测试程序(非 OSGI)中相同的所有方法。我无法使用 DriverManager.getConnection() 创建连接。
我已将驱动程序添加到类路径中,并尝试了此站点上的所有解决方案,例如使用 Class.forName()。可能我在 forName() 中输入了错误的字符串参数。
public void createConn(String URL, String DBName, String username, String password){
try {
Class.forName("mysql-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());
}
}
有人可以告诉我他们在 Class.forName() 中使用的参数吗;
有人能解决这个问题或遇到过这个问题吗?
谢谢,这解决了 classNotFound 异常。现在我有一个错误,指出驱动程序不合适。我知道 OSGI 在驱动程序等方面存在一些问题。有人可以推荐一种方法来规避这个问题吗?
我已将 jdbc jar 放在 java 安装 bin 文件夹中,以及捆绑包的 bin 文件夹中。
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;
控制台输出,错误: osgi>开始 7 数据库服务(MYSQL)启动 类加载 没有找到适合 jdbc 的驱动程序:mysql://localhost/timedb 线程“Thread-1”中出现异常 INSERT INTO Appliance1...
有人对这个问题有任何见解吗?
谢谢
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("mysql-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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Class.forName(String)
采用完全限定的类名,而不是 jar 文件。你应该使用类似的东西Class.forName(String)
takes a fully qualified class name, not a jar file. You should use something like我知道这已经有 3 年了,但我回答只是为了以防万一有人用谷歌搜索它。
所以问题是bundle看不到来自类加载的jar,所以你需要在清单中导入它的包。
我所做的就是创建一个包含 mysql 连接器 jar 的单独包。
新项目>来自现有 jar 的插件;然后我将其所有包添加到其清单文件中的“导出包”中。然后在我的第一个包中,我将连接器的所有包添加到“导入包”中,并且它起作用了。
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.