java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
I am getting an exception (java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state)
while running this program given below. Please help.
Code :
import java.sql.*;
import java.io.*;
class Dbconnect1
{
public static void main(String[] args)
{
String str[]={"Yahoo","Yah"};
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc;dbc:db");
String query="Select distinct id from employee";
Statement st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery(query);
rs.beforeFirst();
Boolean b;
while(rs.next())
{
System.out.println(rs.getInt(1));
if(rs.isLast())
break;
}
st.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("Hello World!");
}
}
Re: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
Re: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
System.out.println(rs.getInt(1));
Re: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
Well here is a little rewrite for you.
Code :
import java.sql.*;
import java.io.*;
class Dbconnect1
{
public static void main(String[] args)
{
String str[]={"Yahoo","Yah"};
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc;dbc:db");
String query="Select distinct id from employee";
Statement st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery(query);
rs.beforeFirst();
boolean b = true;
while(b)
{
System.out.println(rs.getInt(1));
b = rs.next();
}
st.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("Hello World!");
}
}
Notice what I did to the while statement. rs.next actually moves the cursor forward so you are skipping the first row and if you only have one row you will end up in big problem.
Now I set b to true and loop while b is true and set b to rs.next(), if rs.next returns false the loop will break.
Give it a shot!
// Json
Re: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
Hi Json
i tried it. But its giving same exception again. Is there any problem with the query i am firing? Query:
String query="Select distinct id from employee";
I am using Microsoft Access as my Database.
Thanks,
guptapr
Re: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
Thanks for help Json. But i think i was able to rectify my problem. It sounds funny but i just changed the line:
Statement st=con.createStatement(ResultSet.TYPE_SCROLL_SENSI TIVE,ResultSet.CONCUR_UPDATABLE);
to
Statement st=con.createStatement();
i dont know why? But it works.
Regards,
guptapr
Re: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
I see, glad you managed to work it out :D
// Json