How to Connect Java (JDBC) with MS Access Database

In this tutorial I will teach you how to connect java (jdbc) with ms access database.

MS Access is a part of Microsoft Office and used as database management system (dbms).

For making a new database go to MS Access > Blank Database. Give a name to database and click on Create button to create the database.

Also Read: How to Connect Java (JDBC) with MySQL or Oracle Database

How to Connect Java (JDBC) with MS Access Database

Below example shows jdbc ms access database connectivity.

How to Connect Java (JDBC) with MS Access Database

package com;

import java.sql.Connection;
import java.sql.DriverManager;

public class JDBCMSAccess {
	public static void main(String args[]){
		//location where database is present in the system
		String database = "D:\\Users\\TCP\\Documents\\Database1.accdb";
		
		try {
				//loading the driver class
				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	
				//creating connection string
				String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + database;
	
				//establishing connection
				Connection con = DriverManager.getConnection(url);
	
				if(con!=null){
					System.out.println("Connection Successful!");
					con.close();
				}
				
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

JDK 1.8

If you are using jdk 1.8 then you will get ClassNotFoundException. Just read below article to solve the error.

Read: Solve java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver Error

 

Above program is really easy to understand. You can ask your queries in comment section.

3 thoughts on “How to Connect Java (JDBC) with MS Access Database”

  1. getting following error pls fix that for me!!!!

    java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
    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 com.JDBCMSAccess.main(JDBCMSAccess.java:13)

  2. java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:340)
    at GUI.(GUI.java:71)
    at GUI.main(GUI.java:108)

    I got this error. i am using JDK 1.14.0

Leave a Comment

Your email address will not be published. Required fields are marked *