Raj Sehmi Raj Sehmi - 4 days ago 5
Java Question

java.sql.SQLException: After end of result set

Trying to make two comboboxes work together but... right now my first combo box is giving me error... I tried to make code changes but nothing much happened.

cbArea = new javax.swing.JComboBox();
cbArea.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
cbArea.setModel(new javax.swing.DefaultComboBoxModel());
try {
Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/database", "user", "passwd");
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("select area_name from area");
while (rs.next()) {
cbArea.addItem(rs.getString("area_name"));
}
if (con != null)
con.close();
if (stmt != null)
stmt.close();
if (rs != null)
rs.close();
} catch (Exception ex) {
System.out.println(ex);
}

cbArea.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
cbAreaItemStateChanged(evt);
}
});
cbArea.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cbAreaActionPerformed(evt);
}
});


It gives me error as java.sql.SQLException: After end of result set
What to do?

Answer

First close the resultset, then the statement and finally close the connection. Order matters here.

Connection con = null;
Statement stmt = null;
ResultSet rs = null;

try{
  ....//your lohic
}catch(SQLException e){}
}finally{
   try{
      if (rs != null)
        rs.close();
      if (stmt != null) 
        stmt.close();
      if (con != null) 
        con.close();
   }catch(SQLException e){}
}
Comments