简体   繁体   中英

java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver doesn't work anymore in java. How to fix this?

It worked two days ago. Now it doesn't. I'm 100% sure the coding is correct. This is the way I used to connect the database. Couldn't find an answer yet to my problem. Please give me an answer as i'm a beginner for java language. (Using 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);}
}

This is the stack trace

 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

Based on the comments, your system is now running on the Java 8 JRE. The ODBC Bridge has been removed from this, and all future versions. You need to find a native JDBC driver for the database you want to connect to, and put that on your classpath.

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

Alternatively, you could explicitly use the JDK 7 runtime, but I'd recommend against this. You've uncovered a compatibility problem, and it's better to solve it than to try to work around it.

Please add your jar to the class path like below.

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

Note:Please replace msql connector with your jar file as per requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM