Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invaild Cursor State

 
Amna Ibrahim
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having the Invaild Cursor State when I run the bellow code. The code is runing fine in all cases except when I want to find record which is not in the dB:- I am posting this code for the second time.....

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class FindRecord implements ActionListener
{
private DataInputPanel fields;
private JTextArea output;
private Connection connection;
public FindRecord( Connection c, DataInputPanel f, JTextArea o )
{
connection = c;
fields = f;
output = o;
}
public void actionPerformed( ActionEvent e )
{
try
{
if ( !fields.TelNum.getText().equals( "" ))
{
Statement statement =connection.createStatement();
String query = "SELECT * FROM dedEmps WHERE Tel = '" + fields.TelNum.getText() +"'";
output.append( "\nSending query: " + connection.nativeSQL( query ) + "\n" );
ResultSet rs = statement.executeQuery( query );
display( rs );
output.append( "\nQuery successful\n" );
}
else
fields.TelNum.setText( "Enter Telephone Number here then press Find" );
}
catch ( SQLException sqlex )
{
sqlex.printStackTrace();
output.append( sqlex.toString() );
}
}
public void display( ResultSet rs )
{
try
{
rs.next();
String recordNumber = rs.getString(1);
if( recordNumber != null )
{
fields.EmpNum.setText(recordNumber);
fields.FName.setText( rs.getString(2));
fields.DeptName.setText( rs.getString(3));
fields.FloorNum.setText( rs.getString(4));
fields.TelNum.setText(rs.getString(5));
}
else
{output.append( "\nNo record found\n" );}
}
catch ( SQLException sqlex )
{
sqlex.printStackTrace();
output.append( sqlex.toString() );
}
}
}
 
Samuel C. Michaelis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Amna,
look at this code-sample, and imagine rs being an empty ResultSet

(1) rs.next() returns false, because there's no next recordset
(2) now, your driver throws an Exception ,because there is no recordset available at this point
So me'd write following code

Hope, I could help you,
best regards,
Samuel
 
Amna Ibrahim
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankx for your help....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic