Saturday, November 6, 2021

How to solve java.lang.classnotfoundexception sun.jdbc.odbc.jdbcodbcdriver in Java 8

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. This class is required to connect any database using Object database connectivity driver e.g. Microsoft Access, but unfortunately, you cannot use it from JDK 8 onward. In order to solve this error, just use the Jackcess library or a commercial driver like HXTT. Normally, in pre-Java 8 world, java.lang.classnotfoundexception sun.jdbc.odbc.jdbcodbcdriver error comes when you try to connect to the Microsoft Access database from Java using JDBC and JDBC ODBC bridge driver is not available in the classpath.

If you remember, In order to open SQL connection to the database, the first step is to load and register the driver. In order to load driver, we use Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  and this method throws java.lang.classnotfoundexception sun.jdbc.odbc.jdbcodbcdriver, if ClassLoader is not able to find the requested class (sun.jdbc.odbc.JdbcOdbcDriver) in CLASSPATH.

In order to connect to MS Access, we need type 1 JDBC driver, also known as JDBC ODBC bridge driver and the class in question, JdbcOdbcDriver is driver implementation to connect to Open database connectivity driver installed in the machine.

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.

And, if you are new to JDBC then you can also take a look at the Complete JDBC Programming course on Udemy. This course is a comprehensive guide on how to use JDBC in Java to connect to different databases. You will learn the right ways of doing things with respect to Java and the database.

Many newbies in Java programming prefer to connect to MS Access database from Java then bigger databases like Oracle, SQL Server or MySQL. Even I have done the same when I first started learning JDBC, the main reason for that was MS ACCESS comes as part of Microsoft Office and was available in my machine.

I didn't have to go with pain involved in installing Oracle, SQL Server, or MySQL database, sometimes even you don't even have administrator access in your machine to do that. BTW, there are several disadvantages of using JDBC ODBC Bridge driver, first, it requires an ODBC driver to be installed in your machine, second, it makes your code platform dependent.

Using pure Java type 4 JDBC driver is the best way to connect commercial databases from Java programs. Almost all major database vendor provides type 4 JDBC driver nowadays, so its just matter of finding the right version and dropping that JAR in classpath.

java.lang.classnotfoundexception sun.jdbc.odbc.jdbcodbcdriver in Java 8




java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver in Java 8

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.  Here is the stacktrace of the error you get :

   java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:30
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:259)


That's all about java.lang.classnotfoundexception sun.jdbc.odbc.jdbcodbcdriver exception in Java 8. JDBC-ODBC bridge driver is buggy, slow and unsupported on many platforms, it may not even be available even in 64-bit Windows versions. You will get this exception if you try to connect to the Microsoft Access database from Java 8 JRE or JVM because the JDBC-ODBC bridge driver is now removed.

Consider using type 4 JDBC, a pure Java driver with a real database, or if you still want to connect to MS access, use a Jackcess library or a commercial driver like HXTT.

Related JDBC Tutorials and troubleshooting guide :

  • General Guide to solve java.lang.ClassNotFoundException in Java [guide]
  • How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Java MySQL? [solution]
  • How to fix java.lang.ClassNotFoundException: org.postgresql.Driver error in Java? [solution]
  • How to fix java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver Error in Java? [solution]
  • java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory? [solution]
  • How to solve java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver in Java? [solution]
  • How to connect Oracle database from Java Program? (example)
  • How to solve java.lang.unsatisfiedlinkerror no ocijdbc11 in java.library.path? (solution)

Thanks for reading this article so far. If you find the solution useful and it solves your problem then please share this article with your friends and colleagues on Twitter and Facebook. I would really appreciate that. 

P. S. - If you want to learn more about JDBC and looking for free courses to learn JDBC then you can also check out this list of free JDBC courses for Java developers to start with. This list contains the best free JDBC courses for Java developers from Udemy and Coursera. 


No comments :

Post a Comment