Beanshell jdbc连接

时间:2010-09-29 11:30:20

标签: java jdbc beanshell

我想使用beanshell脚本连接到我的数据库。我的代码适用于java,但beanshell不能使用相同的代码。将抛出未找到类的异常。它有其他用途还是不能用beanshell连接到数据库?

谢谢, 比拉尔


@PlínioPantaleão 代码就像这样

addClassPath("C:\\mysql-connector-java-5.1.13\\mysql-connector-java-5.1.13-bin.jar");
import com.mysql.jdbc.Driver;
import java.sql.Connection;

Connection conn = null;

Class.forName("com.mysql.jdbc.Driver").newInstance();

,例外是

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.gjt.sp.jedit.bsh.Reflect.invokeMethod(Reflect.java:134)
at org.gjt.sp.jedit.bsh.Reflect.invokeStaticMethod(Reflect.java:98)
at org.gjt.sp.jedit.bsh.Name.invokeMethod(Name.java:871)
at org.gjt.sp.jedit.bsh.BSHMethodInvocation.eval(BSHMethodInvocation.java:75)
at org.gjt.sp.jedit.bsh.BSHPrimarySuffix.doSuffix(BSHPrimarySuffix.java:102)
at org.gjt.sp.jedit.bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:80)
at org.gjt.sp.jedit.bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:47)
at org.gjt.sp.jedit.bsh.Interpreter.eval(Interpreter.java:644)
at org.gjt.sp.jedit.bsh.Interpreter.eval(Interpreter.java:738)
at org.gjt.sp.jedit.bsh.Interpreter.eval(Interpreter.java:727)
at org.gjt.sp.jedit.BeanShellFacade._eval(BeanShellFacade.java:148)
at org.gjt.sp.jedit.BeanShellFacade.eval(BeanShellFacade.java:113)
at org.gjt.sp.jedit.BeanShellFacade.evalSelection(BeanShellFacade.java:85)
at org.gjt.sp.jedit.BeanShell.evalSelection(BeanShell.java:71)
at sun.reflect.GeneratedMethodAccessor22.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.gjt.sp.jedit.bsh.Reflect.invokeMethod(Reflect.java:134)
at org.gjt.sp.jedit.bsh.Reflect.invokeStaticMethod(Reflect.java:98)
at org.gjt.sp.jedit.bsh.Name.invokeMethod(Name.java:871)
at org.gjt.sp.jedit.bsh.BSHMethodInvocation.eval(BSHMethodInvocation.java:75)
at org.gjt.sp.jedit.bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:102)
at org.gjt.sp.jedit.bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:47)
at org.gjt.sp.jedit.bsh.BSHBlock.evalBlock(BSHBlock.java:130)
at org.gjt.sp.jedit.bsh.BSHBlock.eval(BSHBlock.java:80)
at org.gjt.sp.jedit.bsh.BshMethod.invokeImpl(BshMethod.java:362)
at org.gjt.sp.jedit.bsh.BshMethod.invoke(BshMethod.java:258)
at org.gjt.sp.jedit.bsh.BshMethod.invoke(BshMethod.java:186)
at org.gjt.sp.jedit.BeanShellFacade.runCachedBlock(BeanShellFacade.java:225)
at org.gjt.sp.jedit.BeanShell.runCachedBlock(BeanShell.java:423)
at org.gjt.sp.jedit.BeanShellAction.invoke(BeanShellAction.java:73)
at org.gjt.sp.jedit.gui.InputHandler.invokeAction(InputHandler.java:352)
at org.gjt.sp.jedit.jEdit$4.invokeAction(jEdit.java:3255)
at org.gjt.sp.jedit.jEdit$4.invokeAction(jEdit.java:3237)
at org.gjt.sp.jedit.EditAction$Wrapper.actionPerformed(EditAction.java:221)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

1 个答案:

答案 0 :(得分:1)

ClassNotFoundException只是意味着在运行时类路径中缺少消息中提到的类。只需add JDBC驱动程序JAR文件到beanshell脚本的运行时类路径。以下是文档摘录:

  

Changing the Class Path

     

addClassPath(URL | path)

     

将指定的目录或存档添加到类路径中。档案可以通过URL定位,允许它们通过网络加载。

     

示例:

addClassPath( "/home/pat/java/classes" );
addClassPath( "/home/pat/java/mystuff.jar" );
addClassPath( new URL("http://myserver/~pat/somebeans.jar") );

然后,要加载驱动程序类,您need使用Beanshell提供的getClass()方法而不是标准Class#forName()。以下是文档摘录:

  

Loading Classes Explicitly

     

为了在考虑任何BeanShell类路径修改的同时按名称执行显式类查找,您必须使用标准Class.forName()方法的替换。

     

getClass()命令将使用BeanShell类路径按名称加载类。或者,您可以明确地咨询班级经理:

name="foo.bar.MyClass";
c = getClass( name );
c = BshClassManager.classForName( name );  // equivalent