NJUGUNA WAINAINA
NJUGUNA WAINAINA

Reputation: 1

Java Database Exceptional error that is very unique

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

Upvotes: -1

Views: 53

Answers (1)

phatfingers
phatfingers

Reputation: 10250

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.

Upvotes: 4

Related Questions