Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect java to Ms Access 2010?

Tags:

java

jdbc

odbc

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
like image 699
Tepken Vannkorn Avatar asked Jun 14 '11 04:06

Tepken Vannkorn


People also ask

How to connect MS Access database with Java (JDBC)?

MS Access is a part of Microsoft Office and used as database management system (dbms). For making a new database go to MS Access > Blank Database. Give a name to database and click on Createbutton to create the database. Also Read: How to Connect Java (JDBC) with MySQL or Oracle Database Below example shows jdbc ms access database connectivity.

Is it possible to connect to Microsoft Access in 64-bit Java?

There are a number of errors, though, if you attempt to use a 64-bit version of Java that are not as well documented. This article points out some of those issues and a working strategy for how to successfully connect to a Microsoft Access file via JDBC in 64-bit Java.

How do I connect to a Microsoft Access database?

In order to connect to an Access database, you must first install the Microsoft Access Driver. If you're on Windows, you can do this by either installing Access or installing the Access Database Engine. Whichever product you install, you must make sure that the architecture matches your JVM.

How to connect MS Access database in Eclipse IDE?

To connect your java program with the MS Access database in Eclipse IDE, you need to include UCanAccess Jar files to the Eclipse. I have given the download link of the zip file in the above. Extract the zip archive and you will get the Jar files.


2 Answers

According to msdn it should be sun.jdbc.odbc.JdbcOdbcDriver. So replace this line of code:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

like image 136
CoolBeans Avatar answered Sep 27 '22 16:09

CoolBeans


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

like image 45
poduska Avatar answered Sep 27 '22 16:09

poduska