1

I'm trying to connect to MySQL using ODBC, JDBC, and I have trouble with ClassNotFoundException error.

Exception in thread "main" 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 dbconnect.jdbcconnect.main(jdbcconnect.java:16)

I added jars to JavaBuildPath like this enter image description here

my code

public static void main(String[] args) throws SQLException, ClassNotFoundException {
        // TODO Auto-generated method stub
        //Connection conn = null;

        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String url = "jdbc:odbc:Gtable";
        Class.forName(driver); 
        Connection conn = DriverManager.getConnection(url, "root", "")    
    //  Statement s=conn.createStatement();

        //s.executeQuery("Select")
    }

}

How can i resolve this?

JTFouquier
  • 271
  • 2
  • 14
Empi
  • 141
  • 16

2 Answers2

0

java.lang.classnotfoundexception sun.jdbc.odbc.jdbcodbcdriver exception comes in Java 8 because it has removed the JDBC ODBC bridge driver class "sun.jdbc.odbc.jdbcodbcdriver" from JDK and JRE

0

Reading this post on Javarevisited How to solve java.lang.classnotfoundexception sun.jdbc.odbc.jdbcodbcdriver in Java 8 may fully and correctly address your problem.

JDBC-ODBC bridge driver has been marked as "don't use" for the last decade or so. It was present in Java 7 release but removed from Java 8 release onward. It might also not be available on non-Windows JVMs, as it makes no sense to have an ODBC-JDBC bridge driver on platforms on which ODBC does not exist e.g. Linux or Mac OS

Since this driver has been removed from Java 8 you have to use different strategies to access as reported Manipulating an Access database from Java without ODBC such as UCanAccess if you happen to use an Access DB.

Just remember that sun.jdbc.odbc.JdbcOdbcDriver is a standard class from JDK API and it doesn't come with any external JAR like other vendor database's JDBC drivers e.g. JDBC driver to connect Oracle database comes on ojdbc6.jar and MySQL driver comes in mysql-connector-java-5.1.23-bin.jar. JdbcOdbcDriver class is present in rt.jar, which is always included in Classpath, as this JAR file is part of the JRE.

Community
  • 1
  • 1
abarisone
  • 3,429
  • 11
  • 27
  • 49