-1

am getting the error shown from this code: enter image description here

private void formComponentShown(java.awt.event.ComponentEvent evt) {                                    
    try{
        Connection conn = ConnectionProvider.getCon();
        Statement st = conn.createStatement();
        ResultSet rs  = st.executeQuery("select max(pId) from product");
        if(rs.first()){
            int id = rs.getInt(1);
            id = id+1;
            String str = String.valueOf(id);
            jLabel4.setText(str);
        }
        else{
             jLabel4.setText("1");        
        }
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null,e);
    }
}

i wanted the code to change text on the label from 100 to whatever that was largest value in an id column in product table of a database. the code was to increase that value after new data is entered and keep that as it's current value

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • Please post _error text_, not _pictures_ of error text, unless otherwise [relevant](https://meta.stackoverflow.com/q/285551/230513). – trashgod Mar 18 '23 at 12:50
  • In addition to changing to `rs.next()` as has been recommended, you should really be using try-with-resources for your database objects – g00se Mar 18 '23 at 13:26
  • this is essentially unrelated to any ui (solve the backend problem before adding complexity of the ui, f.i. with simple printOuts). As to the ui: looks like swing, why did you tag javafx? – kleopatra Mar 18 '23 at 13:35

1 Answers1

4

Change rs.first() to rs.next(). Your connection provider is returning a connection with a limitation that you can only read forward, and not randomly traverse the dataset. Calling rs.first() is effectively asking to rewind to the first row, which is unnecessary since you're already poised to read forward to the first row.

phatfingers
  • 9,770
  • 3
  • 30
  • 44