To read data from an Access database using Java, you can use the JDBC-ODBC Bridge driver. However, please note that the JDBC-ODBC Bridge driver has been deprecated since Java 8 and is not recommended for use in production environments. It is recommended to use a dedicated JDBC driver provided by the database vendor for better performance and stability.
That being said, here’s an example using the JDBC-ODBC Bridge driver to read data from an Access database:
import java.sql.*; public class AccessDBDataReader { public static void main(String[] args) { // Database URL String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=/path/to/your/access_database.accdb"; // SQL query String query = "SELECT * FROM your_table"; try { // Register JDBC driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Open a connection Connection conn = DriverManager.getConnection(url); // Create a statement Statement stmt = conn.createStatement(); // Execute the query ResultSet rs = stmt.executeQuery(query); // Process the result set while (rs.next()) { // Retrieve the data by column name int id = rs.getInt("id"); String name = rs.getString("name"); String email = rs.getString("email"); // Do something with the data System.out.println("ID: " + id); System.out.println("Name: " + name); System.out.println("Email: " + email); System.out.println("--------------------------"); } // Close the resources rs.close(); stmt.close(); conn.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } }
Replace /path/to/your/access_database.accdb
with the actual path to your Access database file.
Keep in mind that the JDBC-ODBC Bridge driver might not be available in all Java environments, and its usage is discouraged in favor of native drivers provided by the database vendors.