Saturday, June 23, 2007

I was trying to connect to an ms access database using JDBC. Am a bad learner so it took a lot of time for me to finish it. After googling i found some code to work with java and access. Some of the code is shown below

 

import java.sql.*;

 

try {
        System.out.println("Begining conn");
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String filename = "c:/nsu.mdb";
            String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
            database+= filename.trim() + ";PWD=test}"; // add on to the end
            // now we can get the connection from the DriverManager
            Connection con = DriverManager.getConnection( database ,"shimul","test"); 
         Statement stmt = con.createStatement();
        System.out.println("Conn done succesfully");
        stmt.execute("select * from t");
        
         ResultSet rs = stmt.getResultSet(); // get any Resultt that came from our query
         if (rs != null)
          while ( rs.next() ){
              System.out.println("Name: " + rs.getString("a") + " ID: "+rs.getString("b"));
          }
          stmt.close();
          con.close();
     }
     catch (Exception err) {
        System.out.println("ERROR: " + err);
     }

 

But i got some error like

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Record(s) cannot be read; no read permission on 't'.

 

I tried googling but found few result, after a lot of googling i came to know some solutions which was near to this then finally got the solution

 

First open the file in access, give password, then From Tool->Security-> User and Group Permissions -> then select the table and for the admin account give it all kind of access. This will solve your access problems.

 

You can use the above code by changing the statements for insertion deletion update etc. You might have to change the stmt.execute accordingly. Jdbc driver may be required which can be downloaded from sun.java.com . I dont know but mine was already there, new versions of JDK have built in jdbc:odbc support.

2 Comments:

  1. dam said...
    This comment has been removed by a blog administrator.
    Alex said...
    This comment has been removed by a blog administrator.

Post a Comment