与 Beanshell 的 JDBC 连接
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 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 )
orBshClassManager.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. ThegetClass()
should work just fine.感谢 BalusC,这就是答案:
Thanks to BalusC, this was the answer: