File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Invalid Coursor Name... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Invalid Coursor Name..." Watch "Invalid Coursor Name..." New topic
Author

Invalid Coursor Name...

Amna Ibrahim
Greenhorn

Joined: Oct 13, 2003
Posts: 3
I am having the following exception if I want to find name for person who has no record on the DB:- java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

With following Code:--

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;
private String url;
ResultSet rs;
public FindRecord( Connection c, DataInputPanel f, JTextArea o )
{
connection = c;
fields = f;
output = o;
url = "jdbc dbc ed";
}
public void actionPerformed( ActionEvent e )
{
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
connection = DriverManager.getConnection( url );
if ( !fields.TelNum.getText().equals( "" ) )
{
Statement statement = connection.createStatement();
String query = "SELECT * FROM dedEmps WHERE Tel = " + Integer.parseInt(fields.TelNum.getText());
output.append( "\nSending query: " + connection.nativeSQL( query ) + "\n" );
rs = statement.executeQuery( query );
display( rs );
output.append( "\nQuery successful\n" );
statement.close();
}
else
fields.TelNum.setText( "Enter Telephone Number here then press Find" );
}
catch ( ClassNotFoundException cnfex )
{
System.err.println( "Failed to load JDBC/ODBC driver." );
cnfex.printStackTrace();
System.exit( 1 );
}
catch ( SQLException sqlex )
{
sqlex.printStackTrace();
output.append( sqlex.toString() );
}
}
public void display( ResultSet rs )
{
try
{
rs.next();
int recordNumber = rs.getInt(1);
if ( recordNumber != 0 )
{
fields.EmpNum.setText(String.valueOf(recordNumber));
fields.FName.setText( rs.getString(2));
fields.DeptName.setText( rs.getString(3));
fields.FloorNum.setText( String.valueOf(rs.getInt(4)));
fields.TelNum.setText(String.valueOf(rs.getInt(5)));
}
else
output.append( "\nNo record found\n" );
}
catch ( SQLException sqlex )
{
sqlex.printStackTrace();
output.append( sqlex.toString() );
}
}
}
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Closing the Statement object before iterating through the result set is a Bad Idea.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Invalid Coursor Name...