Beanshell 不会加载我动态添加的 JDBC 驱动程序类?

发布于 2024-11-30 11:52:44 字数 1370 浏览 5 评论 0原文

使用 JDK1.6.0_16,我有一个简单的程序,我试图让 beanshell 2.0b4 动态加载 .jar (如 文档表明它可以)但我没有运气。文档说,如果我使用 beanshells 的 getClass() 方法,那么它将加载之前由“addClassPath()”方法加载的 jar。它不起作用。我需要这方面的帮助...

//debug();
addClassPath("mysql-connector-java-5.1.15.jar"); 
import com.mysql.jdbc.Driver; 
import java.sql.Connection;  
import java.sql.DriverManager; 
import java.util.Arrays;

System.out.println("MySQL Connect Example.");
System.out.println("Classpath: " + Arrays.toString( getClassPath() ) + "\n");

Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
String driver = "com.mysql.jdbc.Driver";
String userName = "root"; 
String password = "password";
try {
  Class driverClass = getClass( driver );
  if(driverClass != null) {
    Driver driver = driverClass.newInstance();
    if(driver != null) {
      DriverManager.registerDriver(driver);
    }
  }
  conn = DriverManager.getConnection(url+dbName,userName,password);
  System.out.println("Connected to the database");
  conn.close();
  System.out.println("Disconnected from database");
} catch (Exception e) {
  e.printStackTrace();
}

我遇到的这个问题强烈表明(beanshell 的) getClass() 方法无法看到它自己的动态更改的类路径。

注意:只有当我将 mysql.jar 文件放入 jre/lib/ext 目录(这是旧版 jre 类加载器可以加载它的位置;而不是 beanshell 类加载器)时,此代码才有效。

Using JDK1.6.0_16, I have this simple program where I am trying to get beanshell 2.0b4 to load a .jar dynamically (as the documentation suggests it will do) and I am having no luck. The documentation says that if I use beanshells' getClass() method then it will load jars that were previously loaded by the "addClassPath()" method. It is not working. I need help on this...

//debug();
addClassPath("mysql-connector-java-5.1.15.jar"); 
import com.mysql.jdbc.Driver; 
import java.sql.Connection;  
import java.sql.DriverManager; 
import java.util.Arrays;

System.out.println("MySQL Connect Example.");
System.out.println("Classpath: " + Arrays.toString( getClassPath() ) + "\n");

Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
String driver = "com.mysql.jdbc.Driver";
String userName = "root"; 
String password = "password";
try {
  Class driverClass = getClass( driver );
  if(driverClass != null) {
    Driver driver = driverClass.newInstance();
    if(driver != null) {
      DriverManager.registerDriver(driver);
    }
  }
  conn = DriverManager.getConnection(url+dbName,userName,password);
  System.out.println("Connected to the database");
  conn.close();
  System.out.println("Disconnected from database");
} catch (Exception e) {
  e.printStackTrace();
}

This problem I am having strongly suggests that the getClass() method (of beanshell) is unable to see its own dynamically changed classpath.

NOTE: this code works only when I put the mysql.jar file into the jre/lib/ext directory (which is where the legacy jre classloader can load it; not the beanshell classloader)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断桥再见 2024-12-07 11:52:44

这可能不是 BeanShell 的事情,jdbc 在跨类加载器加载驱动程序时出现问题(请查看类 javadoc 的 ClassLoader,并查看类似 这个)。

this is probably not a beanshell thing, jdbc has issues loading drivers across classloaders (check out the class javadoc for ClassLoader, and see hacks like this).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文