java jdbc example connect to microsoft access database


To connect to a Microsoft Access database using JDBC in Java, you can follow these steps:

  1. Download and install the Microsoft Access Database Engine. You can download it from the official Microsoft website.

  2. Create a new Java project in your IDE and add the JDBC driver for Microsoft Access to the project's classpath. The JDBC driver is typically a JAR file that you can download from the internet.

  3. Import the necessary JDBC classes in your Java code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
  1. Create a connection to the Microsoft Access database using the following code:
String dbFile = "path/to/your/database.accdb";
String url = "jdbc:ucanaccess://" + dbFile;
Connection connection = DriverManager.getConnection(url);

Replace "path/to/your/database.accdb" with the path to your Microsoft Access database file.

  1. Execute SQL queries on the database using the Connection object. Here is an example of how to execute a simple SELECT query:
try {
    String sql = "SELECT * FROM mytable";
    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery(sql);
    while ( {
        String name = resultSet.getString("name");
        int age = resultSet.getInt("age");
        System.out.println(name + " is " + age + " years old.");
} catch (SQLException e) {

Replace "mytable" with the name of your table.

  1. Close the connection. Once you are finished using the connection, you should close it to free up system resources:

That's it! With these steps, you can connect to a Microsoft Access database using JDBC in Java and execute SQL queries on the database.