How to connect java to Ms Access 2010?
44,433
Solution 1
According to msdn it should be sun.jdbc.odbc.JdbcOdbcDriver
. So replace this line of code:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Solution 2
Spelling error? Perhaps this line:
con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Acess Driver (*.mdb, *.accdb)}; DBQ="+ f.getPath() + "//db//JavaAccess.accd","","");
should be
con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ="+ f.getPath() + "//db//JavaAccess.accd","","");
Access has 2 C's
Solution 3
Create connection
public static Connection getConnection() {
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:anime"; //anime is the database
String username = "ipieluser"; //leave blank if none
String password = "ipielpassword"; //leave blank if none
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
How to call:
public static void main(String args[]) {
try {
Connection conn = getConnection();
Statement st = conn.createStatement();
st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM localTable");
//get and displays the number of columns
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
System.out.println("resultSet MetaData column Count=" + numberOfColumns);
st.close();
conn.close();
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
Solution 4
Use UCanAccess JDBC Driver :
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); // can be omitted in most cases
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://<mdb or accdb file path>",user, password);
e.g.:
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://c:/pippo.mdb");
So for your example it will be
con = DriverManager.getConnection("jdbc:ucanaccess://"+f.getPath()+"/db/JavaAccess.accd")
Author by
Tepken Vannkorn
Updated on July 27, 2020Comments
-
Tepken Vannkorn almost 4 years
Does anyone have any ideas of how to connect Access 2010 to java jdbc. I use this method, but when I call it, it doesn't work:
public void loadDb(){ try{ Class.forName("sun.jdbc.JdbcOdbcDriver"); File f = new File(System.getProperty("user.dir")) con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Acess Driver (*.mdb, *.accdb)}; DBQ="+ f.getPath() + "//db//JavaAccess.accd","",""); st = con. createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); }catch(ClassNotFoundException e){e.printStackTrace(); }catch(SQLException e){e.printStackTrace();} } //con and st are already defined
-
Tepken Vannkorn almost 13 yearshi guy, I still cannot access to the getConnection() method since it says that the type is void cannot be return, and on the other hand, it doesn't recognize the method getConnection() in some other palces.
-
Rishabh almost 13 yearsDeclare it as public it will work I have edited the code too.
-
Barett over 11 yearsThis is a better solution for 64-bit systems running Office 64-bit. You may also need the Access Database Engine to expose the 32-bit Access database via a 64-bit interface. (microsoft.com/en-us/download/details.aspx?id=13255)