Here is an example about how to create a database connection to MS Access database. To allow the database access to be authenticated the security user account can be added from Tools->Security->User and Group Accounts menu in the Microsoft Access 2003.
On the example below we can either connect through the DSN created previously on the Windows system, or we can create it in our program as the long URL below.
The sun.jdbc.odbc.JdbcOdbcDriver
driver only available prior to JDK 8, so you can use this if you are on JDK 5, 6, or 7.
package org.kodejava.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MSAccessConnect {
private static final String USERNAME = "admin";
private static final String PASSWORD = "admin";
private static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
// If you want to use you ODBC DSN
//private static final String URL = "jdbc:odbc:TestDB";
private static final String URL =
"jdbc:odbc:Driver={MICROSOFT ACCESS DRIVER (*.mdb, *.accdb)};" +
"DBQ=F:/Wayan/Kodejava/kodejava-example/kodejava-jdbc/src/main/resources/kodejava.mdb;";
public static void main(String[] args) throws Exception {
Class.forName(DRIVER);
try (Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
// Do something with the connection here!
System.out.println("connection = " + connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
The connection object printed from the code above:
connection = sun.jdbc.odbc.JdbcOdbcConnection@4ec4f498
Latest posts by Wayan (see all)
- How do I create a servlet filter to make secure cookies? - April 18, 2024
- How do I configure secure cookies using web.xml? - April 18, 2024
- How do I create a table with multiple header in iText 8? - March 16, 2024
It is actually possible to transfer the jdbcodbc bridge from jdk7 to jdk8. The recipe is out there, google, google. Ucanaccess is also a great alternative, but only for access to the file by one process.