Skip to content
Advertisement

Why is it not updating

I’m trying to update my database where in the Book copies will be updated to 5 depending on the Book code.

The Book copies I initialize in the database was 10. For now, I have a fixed value of 5 to change it. My plan is to decrement the copies by 1 if users borrowed it. But for now, I’m trying to learn how to update the book first if it will work.

Here’s my code where the Book copies in my database still hasn’t updated to 5.

try{
    con = DriverManager.getConnection("jdbc:derby://localhost:1510/LibSyst", "student", "student");
    String updating = "UPDATE BOOKS SET copies = ? WHERE code = ?";
    ps = con.prepareStatement(updating);

    //trying to convert String to Int here
    String strCOde = bookCode.getText();
    int intCode = Integer.parseInt(strCOde);
    
    ps.setInt(7, 5);
    ps.setInt(1, intCode);
    ps.executeUpdate();
        
}catch (Exception e ){
    JOptionPane.showMessageDialog(null, "Update Failed");
}

Also, the copies data type is numeric, is that right?

Advertisement

Answer

You are making a silly mistake. The index in the prepared statement start from 1, so just make these changes it will work as expected.

 ps.setInt(1, 5);
 ps.setInt(2, intCode);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement