10

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
  • 211
  • 3
  • 4
  • 7
  • Also, you should accept answers to your questions if you've found them to be useful(See there is a tick there)and also use upvotes. It will help you get more answers. – Rishabh Jun 14 '11 at 11:32
  • Note that the JDBC-ODBC Bridge has been **removed** from Java 8 and is not supported (ref: [here](http://docs.oracle.com/javase/7/docs/technotes/guides/jdbc/bridge.html) and [here](http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6345277)). [UCanAccess](http://ucanaccess.sourceforge.net/site.html) is a popular alternative (details [here](http://stackoverflow.com/q/21955256/2144390)). – Gord Thompson Mar 20 '15 at 13:23

6 Answers6

8

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

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

CoolBeans
  • 20,016
  • 10
  • 81
  • 98
6

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

joran
  • 157,274
  • 30
  • 404
  • 439
poduska
  • 61
  • 1
  • 1
  • This 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. (http://www.microsoft.com/en-us/download/details.aspx?id=13255) – Barett Dec 21 '12 at 06:42
5

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());
 }
}
Rishabh
  • 3,312
  • 4
  • 42
  • 69
  • hi 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. – Tepken Vannkorn Jun 14 '11 at 05:03
  • Declare it as public it will work I have edited the code too. – Rishabh Jun 14 '11 at 05:39
2

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")
Gord Thompson
  • 98,607
  • 26
  • 164
  • 342
Anthony O.
  • 16,644
  • 12
  • 92
  • 151
0

Rishab's reply helped me to connect to my access database.

I did following correction in the code:

Instead of

String url = "jdbc:odbc:anime"; //anime is the database

I did

String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" + "d://institute//institutedata.accdb";

I explicitly defined driver and full database name with path and extension.

temoto
  • 4,871
  • 3
  • 26
  • 48
Sushil Pugalia
  • 39
  • 3
  • 13
-1

As today only we face the same problem and found that to check the version of java if your version of java if the version of the java is above 7 then the sun.jdbc.odbc.JdbcOdbcDriver will not be supported so just check the version of the java.