1

我想在 beanshell 脚本中连接到 hsqldb。

我在加载课程时遇到问题,我之前写过它并得到了回复。

代码如下所示:

    Connection conn = null;

    try {
            getClass("org.hsqldb.jdbcDriver").newInstance();
            conn =  DriverManager.getConnection("jdbc:hsqldb:file:C:/testdata/tdb","SA","");
            System.out.println("Connection established");
    }

我收到了这个错误:

java.sql.SQLException: No suitable driver found for jdbc:hsqldb:file:C:/testdata/tdb

我也尝试注册驱动程序但我没有工作

DriverManager.register(getClass("org.hsqldb.jdbcDriver").newInstance())

这段代码已经在 java 中工作(而不是使用 Class.forName() 的 getClass())beanshell 需要什么来处理这段代码?

谢谢, 比拉尔

4

1 回答 1

1

I gave up on this , there was no way to dynamically load JDBC classes as opposed to other types of objects, and instead, I now use Runtime exec to call the command line program for the DB . Also , beanshell has a "exec()" method.

Luckily, all I needed was to run scripts against the DB rather than actually interact with the DB , and so this works for me.

Also, the reason JDBC wont load for you is because Beanshell sometimes will load its own classloader (instead of the default classloader). This happens especially if you try to dynamically load jars inside the Beanshell script. If you put your jdbc.jar into Javasoft/ext directory, in that case I believe it will get into the default classloader. Also, if your Beanshell script is careful enough not to trigger the new classloader, that may work as well. In other words, dont call "addClassPath" in your script, among other things.

于 2011-08-26T07:29:09.357 回答