Java - JDBC - Connecting with Access without DSN
The steps for Java Connectivity with Access without DSN:
Download the JDBC-ODBC Bridge driver from the internet and add it to your classpath.
Create a new Java project in your IDE.
In your project, create a new Java class.
Import the necessary libraries:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
In the main method of your class, create a connection string to your Access database file (.mdb or .accdb).
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\path\\to\\your\\database\\file.accdb";
Create a connection object using the connection string and your database login credentials.
Connection con = DriverManager.getConnection(url, "username", "password");
Create a statement object to execute SQL queries.
Statement stmt = con.createStatement();
Write your SQL query and execute it using the statement object.
String sql = "SELECT * FROM Customers";
stmt.executeQuery(sql);
Process the results of your query and close the statement and connection objects.
while (rs.next()) {
String name = rs.getString("Name");
int age = rs.getInt("Age");
System.out.println(name + " " + age);
}
stmt.close();
con.close();