Java Question

Resultset Closed

This code give me that:

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {

String UI=null;

String sql="select * from Outcome";
try {
sta=con.createStatement();
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
try {
rs=sta.executeQuery(sql);
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
try {

while(rs.next()){
UI=rs.getString("OutID");
if(UI.equals(jTextField5.getText())){
System.out.println("\n"+UI);
UI=jTextField5.getText();
String Des=jTextField6.getText();
String Mo=jTextField7.getText();
String Da=String.valueOf(jComboBox4.getSelectedItem())+"/"+String.valueOf(jComboBox5.getSelectedItem())+"/"+String.valueOf(jComboBox6.getSelectedItem());
String Not=jTextField8.getText();

String st = String.format("UPDATE Outcome SET OutID = '%s', Description = '%s', MoneyAmount = '%s', ODate = '%s', Notes = '%s'",UI,Des,Mo,Da,Not);
sta.executeUpdate(st);

// ps.setString(1, jTextField5.getText());
//ps.setString(2,jTextField6.getText());
// ps.setString(3,jTextField7.getText());
//ps.setString(4,String.valueOf(jComboBox4.getSelectedItem()+"/"+String.valueOf(jComboBox5.getSelectedItem())+"/"+String.valueOf(jComboBox6.getSelectedItem())));

sta.close();

con.close();

}


}
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}


}


The Exception:

java.sql.SQLException: ‏‏[Microsoft][ODBC Driver Manager] ‏‏حالة رأس المؤشر غير صالحة
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)
at comcentrt.Outcomes.jButton4ActionPerformed(Outcomes.java:861)
at comcentrt.Outcomes.access$3(Outcomes.java:846)
at comcentrt.Outcomes$5.actionPerformed(Outcomes.java:380)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Answer

You have closed the connection inside the while loop

con.close();

that is why you are getting exception. Close that after the while loop

while(rs.next())
{
 // your code
}
con.close();
Comments