Angsuman's Java Blog

News and views from my perspective as a Chief Software Architect and CEO. Focused on Java and Web Technologies.


Sunday, April 13, 2003

How to access MS Access database from JDBC?



private static final String accessDBURLPrefix
= "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
private static final String accessDBURLSuffix = ";DriverID=22;READONLY=false}";

// Initialize the JdbcOdbc Bridge Driver
static {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(ClassNotFoundException e) {
System.err.println("JdbcOdbc Bridge Driver not found!");
}
}

/** Creates a Connection to a Access Database */
public static Connection getAccessDBConnection(String filename) throws SQLException {
filename = filename.replace('\\', '/').trim();
String databaseURL = accessDBURLPrefix + filename + accessDBURLSuffix;
// System.err.println("Datebase URL: " + databaseURL);
return DriverManager.getConnection(databaseURL, "", "");
}