File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Problem with PreparedStatement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Problem with PreparedStatement" Watch "Problem with PreparedStatement" New topic

Problem with PreparedStatement

Rahul Dasgupta

Joined: Jun 01, 2002
Posts: 20
My java program compiled but throws an exception.
Can you tell me why this is happening?
Thanks for any help
import java.sql.*;
public class DBaccess
public static void main(String args[])
ResultSet res;
Connection con = DriverManager.getConnection("jdbc dbc:nw");
PreparedStatement ps = con.prepareStatement(
"Select * from Products where ProductID = ? " );
BufferedReader in = new BufferedReader(
new InputStreamReader( ) );
System.out.print( "Enter Product ID : ");
ps.setString( 1, in.readLine( ) );
System.out.println( "Read");
res = ps.executeQuery( );
System.out.println( "Name Is : " + res.getString( 6 ) );
catch(Exception e)
System.out.println("Some exception has occured in DBaccess");
Error Message -
Enter Product ID : 4
Some exception has occured in DBaccess
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
at sun.jdbc.odbc.JdbcOdbc.createSQLException(
at sun.jdbc.odbc.JdbcOdbc.standardError(
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(
at DBaccess.main(
PS. I am working with Northwind sample database (Access) on Win XP.
Fields in Products table are:
ProductID AutoNumber
ProductName Text
SupplierID Number
CategoryID Number
QuantityPerUnit Text
UnitPrice Currency
UnitsInStock Number
UnitsOnOrder Number
ReorderLevel Number
Discontinued Yes/No
[ December 12, 2002: Message edited by: Rahul Dasgupta ]
[ December 12, 2002: Message edited by: Rahul Dasgupta ]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Moving this to JDBC...

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
You need to call to position the ResultSet cursor on the first row, before you can access any values from that row. I know that seems strange when you've only got one row - but it makes more sense as part of the standard idiom for processing multiple rows:

"I'm not back." - Bill Harding, Twister
Rahul Dasgupta

Joined: Jun 01, 2002
Posts: 20
Working fine now,
- Rahul
I agree. Here's the link:
subject: Problem with PreparedStatement
jQuery in Action, 3rd edition