| 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: 8290
|
|
|
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]
|
 |
 |
|
|
subject: Invalid Coursor Name...
|
|
|