In this post, we will see how to resolve java.lang.classnotfoundexception: sun.jdbc.odbc.jdbcodbcdriver exception in java.
Table of Contents
Problem: java.lang.classnotfoundexception: sun.jdbc.odbc.jdbcodbcdriver
This exeception comes in Java 8 because sun.jdbc.odbc.jdbcodbcdriver
has been removed from JDK and JRE. This class is used to connect database using Object database connectivity driver such as Microsoft access.
Since Java 8 does not support jdbc odbc bridge, you need to use some other alterantive solutions to resolve this issue.
Solution 1 : Using UCanAccess jars
You can use UCanAccess
jars to resolve this issue.
Here is the dependencies you can put in your pom.xml.
1 2 3 4 5 6 7 |
<dependency> <groupId>net.sf.ucanaccess</groupId> <artifactId>ucanaccess</artifactId> <version>5.0.0</version> </dependency> |
Maven will add all other required dependencies for you.
Here is example code to connect using UCanAccess libraries.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package org.arpit.java2blog; import java.sql.Connection; import java.sql.DriverManager; public class MSAccessUsingUCanAccess { public static void main(String args[]){ String conUrl = "jdbc:ucanaccess://C:\\Users\\Arpit\\Databases\\Employee.accdb"; try { //Getting connection Connection con = DriverManager.getConnection(conUrl); if(con!=null){ System.out.println("Connectioned successfully"); con.close(); } } catch (Exception e) { e.printStackTrace(); } } } |
Please note that if you are working with large databse and using defualt memory setting, it is recommended to use xmx and xms parameter to allocate sufficient memory, otherwise you have to set drive’s memory property to false as below
1 2 3 |
String conUrl = "jdbc:ucanaccess://C:\\Users\\Arpit\\Databases\\Employee.accdb;memory=false"; |
Solution 2 : Revert Java version to 7 or before
If you are not using features of Java 8, then you can revert your java version to 7 and it will work fine.
That’s all about how to resolve java.lang.classnotfoundexception: sun.jdbc.odbc.jdbcodbcdriver exception in java.