Invalid cursor state

HI,i am a student from Malaysia

i am doing my JDBC project with my group members and i get the same problem as you all, that is === INVALID CURSOR STATE!!!!!!

what is this about? some one can explain this? define this statement? my 3 files got this kind of problem and cause me cannot delete record or update record... how to solve this problem? help!!!!!!!!!

below are my source code of the file

import java.awt.*;

import java.awt.event.*;

import java.sql.*;

import javax.swing.*;

public class SearchSegmentPlanRecord implements ActionListener

{

private SegmentPlanScrollingPanel fields;

private JTextArea area;

private Connection connection;

public SearchSegmentPlanRecord (Connection c, SegmentPlanScrollingPanel f,JTextArea a)

{

connection = c;

fields = f;

area = a;

}

public void actionPerformed (ActionEvent e)

{

try

{

if(!fields.treecode.getText ().equals( "" ) )

{

Statement statement = connection.createStatement ();

String query = "SELECT * FROM SEGMENTPLAN "

+ " WHERE TREECODE = ' " +fields.treecode.getText().toUpperCase()+ " ' ";

area.append("\nSending Query: " + connection.nativeSQL (query) + "\n" );

ResultSet result = statement.executeQuery (query);

display ( result );

area.append( "\nQuery Successful\n");

statement.close();

}

else

area.append ( "\nEnter TreeCode then press Find\n" );

}

catch ( SQLException sqlex )

{

sqlex.printStackTrace();

area.append( sqlex.toString() );

area.setText("No Record Found");

}

}

public void display( ResultSet result )

{

try

{

result.next();

if ( !fields.treecode.getText().equals(""))

{

fields.segmentcode.setText(result.getString("SEGMENTCODE"));

fields.segmentcode.setEditable(false);

fields.treecode.setText( result.getString("TREECODE"));

fields.treecode.setEditable(false);

fields.numbertrees.setText( result.getString ("NUMBER_OF_TREES" ) );

fields.planteddate.setText( result.getString ("PLANTED_DATE" ) );

fields.officer.setText( result.getString ("FOREST_OFFICER" ) );

}

else

area.append( "\nNo Record found\n" );

}

catch ( SQLException sqlex )

{

sqlex.printStackTrace ();

area.append( sqlex.toString() );

}

}

}

Any one can correct this source code or not? this source code i have use in the another file (the first file) and it works!!. but after that , i duplicated other 3 files with the same source code above, and the error comes out!!!. how ? and why? some one can explain or any suggestion to fix it? one more thing i have to explain at here, i have used 4 tables in a database. then when i duplicate the source code for other 3 files, it cannot work at all, only for searching options. i still can use the Add function defined in anothe class. because of this error, i cannot search record, update record and delete record.

then my group member told me, if one file for database, which means in a database you only have a table for a particular file( i got 4 files, 1st file can work, and the rest all die!!!) , it won't cause the error of Invalid Cursor State. how this thing will happen? my project due date is next week... anyone have a better suggestion to solve this kind of situation.?

your suggestions and solutions are really really appreciated

[3579 byte] By [cheekhye] at [2007-9-26 1:16:19]
# 1

U have to close the ResultSet after using it.

I think this is the main reason for getting

an exception Invalid Cursor State.

rs.close(); in display function.

ex:

rs=stmt.executeQuery("select * from dept");

while(rs.next()){

System.out.println(rs.getString(1));

}

rs.close();

rameshchanamolu at 2007-6-29 0:44:01 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
in your display(ResultSet result) method, you have this statement:result.next()this returns a boolean not void.place it like thisif( result.next()) ...
deandaniel at 2007-6-29 0:44:01 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
You get this error because you are trying to move the cursor to next value by using result.next() when there is no next value. You can use it like while(rs.next()){}
aparna_chitragar at 2007-6-29 0:44:01 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

I have had a similar problem. I get the error "Invalid Cursor State" from SQL Server 2000 when I use more than one open Statement (actually, I'm using CallableStatement, but it goes all the way back to the Statement class at least...) on the same Connection at once. Check this article out for a little more information.

http://support.microsoft.com/default.asp?scid=kb;EN-US;q253010 By the way, I'm using MDAC 2.7 (which is supposed to not have this problem) and their workarounds didn't work for me.

titojermaine at 2007-6-29 0:44:01 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...