Hello

I am trying to connect to an access database, i have tried using the

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

but this does not work. I am using Eclipse IDE and running windows 7 64bit. Does anyone know how to make it work?

Thanks

Or you can run the code under a 32-bit JVM. The 64-bit JVM will not find the 32-bit Access driver in ODBC, but a 32-bit installation will.

I tried that driver but when i ran my program i get this error:

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

Then you got a connection and the driver worked.

Invalid cursor state could be occurring if you tried to read a row value without calling next(). Post the code and the stack trace if you want a more definitive answer.

Here is the code

import java.sql.*;


class Test
{
 public static void main(String[] args)
 {
 try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = DriverManager.getConnection("jdbc:odbc:Cont","","");
Statement s = con.createStatement();
s.executeQuery("SELECT * FROM Customer");
ResultSet rset = s.getResultSet();

while(rset.next());
{
	System.out.println(rset.getString("Name") +" " +
			rset.getString("Email") + " " +
			rset.getInt("Comments")); 
}

 }
 catch (ClassNotFoundException exp) {
System.err.println(exp);

 }
 catch(SQLException exp)
 {
	 System.err.println(exp);
 }
 }
}

Try changing your query call to this and see if it makes a difference

ResultSet rset = s.executeQuery("SELECT * FROM Customer");

Replace the println() statements in your catch blocks with exp.printStackTrace() calls so you can see exactly which line is causing the error.

I don't see anything specifically wrong with your code. I suggested the previous change because the Access ODBC driver is picky about re-reading values that have already been read.

Statement s = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

check and set ResultSet#Type as you really needed, just for my ... then if you are able to call rset.first();

This article has been dead for over six months. Start a new discussion instead.