java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver在java中不再起作用。如何解决这个问题?

时间:2014-11-19 16:51:13

标签: java jdbc

两天前工作了。现在它没有。我100%确定编码是正确的。这是我用来连接数据库的方式。无法找到我的问题的答案。请给我一个答案,因为我是java语言的初学者。 (使用jdk 7u72)

public void set()
{
    try
    {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String su=new String("jdbc:odbc:MOVIESURF"); //This is the database
        con=DriverManager.getConnection(su,"","");
        st=con.createStatement();
    }
    catch(Exception e2){System.out.println(e2);}
}

这是堆栈跟踪

 java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
         at java.net.URLClassLoader$1.run(Unknown Source)
         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 A.set(jptest7.java:410)
         at A.actionPerformed(jptest7.java:560)
         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.plaf.basic.BasicButtonListener.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.dispatchEventImpl(Unknown Source)
         at java.awt.EventQueue.access$400(Unknown Source)
         at java.awt.EventQueue$3.run(Unknown Source)
         at java.awt.EventQueue$3.run(Unknown Source)
         at java.security.AccessController.doPrivileged(Native Method)
         at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
         at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
         at java.awt.EventQueue$4.run(Unknown Source)
         at java.awt.EventQueue$4.run(Unknown Source)
         at java.security.AccessController.doPrivileged(Native Method)
         at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)

Connection failed select PASSWORD from USERS where USERNAME='ashane' ERROR:
 java.lang.NullPointerException

2 个答案:

答案 0 :(得分:2)

根据评论,您的系统现在在Java 8 JRE上运行。 ODBC桥已从此以及所有未来版本中删除。您需要为要连接的数据库找到本机JDBC驱动程序,并将其放在类路径中。

java -classpath "[/path/to/your/driver.jar];." [yourclassname]

或者,您可以显式使用JDK 7运行时,但我建议不要这样做。你已经发现了一个兼容性问题,最好解决它而不是试图解决它。

答案 1 :(得分:-3)

请将您的jar添加到类路径中,如下所示。

java -cp .; C:\ Program Files \ MySQL \ mysql-connector-java-5.1.15 \ mysql-connector-java-5.1.15.jar MysqlConnect

注意:请根据要求将msql连接器替换为您的jar文件。