简体   繁体   中英

what is wrong with my code it says array out of bounds index -1

try{
    String sql = "UPDATE Products SET ID=?, Category=?,Product=?,Price=?,InItem=?, OutItem=?, TimeOutItem=?, TotalStocks=?"
                      + "WHERE ID=? ";   
    pst = conn.prepareStatement(sql);
    pst.setString(0, ID.getText());
    pst.setString(1, Category.getSelectedItem().toString());
    pst.setString(2, Product.getText());
    pst.setString(3, Price.getText());
    pst.setString(4, Quantity.getText());
    pst.setString(5, out.getText());
    pst.setString(6, date.getDateFormatString().toString());
    pst.setString(7, Stocks.getText());

    int rowsUpdated = pst.executeUpdate();
    if(rowsUpdated > 0){
        UpdateJTable();
        JOptionPane.showMessageDialog(null, "Update");
    }
}catch(Exception e){
    JOptionPane.showMessageDialog(null, e);
}

Array index out of bounds -1. I don't know why there's still an error on my code please help.

set the parameter starting from 1 not from 0

 pst.setString(1, ID.getText());
 pst.setString(2, Category.getSelectedItem().toString());

Edit :Complete code

try{
    String sql = "UPDATE Products SET Category=?,Product=?,Price=?,InItem=?, OutItem=?, TimeOutItem=?, TotalStocks=?"
                      + " WHERE ID=? ";   
    pst = conn.prepareStatement(sql);

    pst.setString(1, Category.getSelectedItem().toString());
    pst.setString(2, Product.getText());
    pst.setString(3, Price.getText());
    pst.setString(4, Quantity.getText());
    pst.setString(5, out.getText());
    pst.setString(6, date.getDateFormatString().toString());
    pst.setString(7, Stocks.getText());
    pst.setString(8, ID.getText());

    int rowsUpdated = pst.executeUpdate();
    if(rowsUpdated > 0){
        UpdateJTable();
        JOptionPane.showMessageDialog(null, "Update");
    }
}catch(Exception e){
    JOptionPane.showMessageDialog(null, e);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM