Tepken Vannkorn
Tepken Vannkorn

Reputation: 211

How to connect java to Ms Access 2010?

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

Upvotes: 10

Views: 44830

Answers (6)

sarvesh
sarvesh

Reputation: 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.

Upvotes: -1

Sushil Pugalia
Sushil Pugalia

Reputation: 37

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.

Upvotes: 0

Anthony O.
Anthony O.

Reputation: 24297

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")

Upvotes: 2

poduska
poduska

Reputation: 61

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

Upvotes: 6

Rishabh
Rishabh

Reputation: 3852

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());
 }
}

Upvotes: 5

CoolBeans
CoolBeans

Reputation: 20800

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

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

Upvotes: 8

Related Questions