与 Beanshell 的 JDBC 连接

发布于 2024-11-30 10:54:08 字数 1488 浏览 0 评论 0原文

StackOverflow 上的其他类似问题没有回答我在这方面的问题。我有这个不起作用的脚本,我想知道如何让它工作:

// beanshell script script.bsh
import com.mysql.jdbc.Driver; 
import java.sql.Connection;  
name="com.mysql.jdbc.Driver"; 
c = getClass( name ); 
c = BshClassManager.classForName( name );  // equivalent 

我得到的错误是:

// Debug: getResolvedMethod cache MISS: class bsh.BshClassManager - classForName
// Debug: Searching for method: classForName( java.lang.String ) in 'bsh.BshClassManager'
// Debug: Looking for most specific method: classForName
bsh.UtilEvalError: Cannot reach instance method: classForName( java.lang.String ) from static context: bsh.BshClassManager
        at bsh.Reflect.checkFoundStaticMethod(Unknown Source)
        at bsh.Reflect.resolveJavaMethod(Unknown Source)
        at bsh.Reflect.resolveExpectedJavaMethod(Unknown Source)
        at bsh.Reflect.invokeStaticMethod(Unknown Source)
        at bsh.Name.invokeMethod(Unknown Source)
        at bsh.BSHMethodInvocation.eval(Unknown Source)
        at bsh.BSHPrimaryExpression.eval(Unknown Source)
        at bsh.BSHPrimaryExpression.eval(Unknown Source)
        at bsh.BSHAssignment.eval(Unknown Source)
        at bsh.Interpreter.eval(Unknown Source)
        at bsh.Interpreter.source(Unknown Source)
        at bsh.Interpreter.main(Unknown Source)

文档说它应该存在。

Other similar questions on StackOverflow didn't answer my question in this area. I have this script that doesn't work, and I am wondering how to get it to work:

// beanshell script script.bsh
import com.mysql.jdbc.Driver; 
import java.sql.Connection;  
name="com.mysql.jdbc.Driver"; 
c = getClass( name ); 
c = BshClassManager.classForName( name );  // equivalent 

And the error I get is:

// Debug: getResolvedMethod cache MISS: class bsh.BshClassManager - classForName
// Debug: Searching for method: classForName( java.lang.String ) in 'bsh.BshClassManager'
// Debug: Looking for most specific method: classForName
bsh.UtilEvalError: Cannot reach instance method: classForName( java.lang.String ) from static context: bsh.BshClassManager
        at bsh.Reflect.checkFoundStaticMethod(Unknown Source)
        at bsh.Reflect.resolveJavaMethod(Unknown Source)
        at bsh.Reflect.resolveExpectedJavaMethod(Unknown Source)
        at bsh.Reflect.invokeStaticMethod(Unknown Source)
        at bsh.Name.invokeMethod(Unknown Source)
        at bsh.BSHMethodInvocation.eval(Unknown Source)
        at bsh.BSHPrimaryExpression.eval(Unknown Source)
        at bsh.BSHPrimaryExpression.eval(Unknown Source)
        at bsh.BSHAssignment.eval(Unknown Source)
        at bsh.Interpreter.eval(Unknown Source)
        at bsh.Interpreter.source(Unknown Source)
        at bsh.Interpreter.main(Unknown Source)

The documentation says it should exist.

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

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

发布评论

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

评论(2

意中人 2024-12-07 10:54:08

根据 http://beanshell.org/manual/classpath.html#Loading_Classes_Explicitly 文档您可以自由选择 getClass( name )BshClassManager.classForName( name ) 来加载驱动程序。另请注意 // equal 注释。

也许文档只是给出了如何正确使用 BshClassManager 的错误示例。但由于它只是一个“等效项”,因此您可以将其完全删除。 getClass() 应该可以正常工作。

According to the http://beanshell.org/manual/classpath.html#Loading_Classes_Explicitly documentation you're free to choose either getClass( name ) or BshClassManager.classForName( name ) to load the driver. Note also the // equivalent comment.

Perhaps the documentation just gave a wrong example of how to use the BshClassManager properly. But as it's just an "equivalent" you could remove that altogether. The getClass() should work just fine.

一抹微笑 2024-12-07 10:54:08

感谢 BalusC,这就是答案:

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

System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "mydb";
String driver = "com.mysql.jdbc.Driver";
String userName = "root"; 
String password = "password";
try {
    c = getClass( 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();
 }

Thanks to BalusC, this was the answer:

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

System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "mydb";
String driver = "com.mysql.jdbc.Driver";
String userName = "root"; 
String password = "password";
try {
    c = getClass( 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();
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文