Skip to content
Advertisement

Java sql error when trying to update database

I am having a sql syntax issue when I am trying to update my databse through my update method which is trigured by a button

here is the button code

update.addActionListener(e -> {

               int i = table.getSelectedRow();
               if (i >= 0) {
                   model.setValueAt(PackId.getText(), i, 0);
                   model.setValueAt(PackName.getText(), i, 1);
                   model.setValueAt(VendorName.getText(), i, 2);
                   model.setValueAt(PackValue.getText(), i, 3);
                   try {
                       updatepacks(PackId,PackName,VendorName,PackValue);
                   } catch (SQLException throwables) {
                       throwables.printStackTrace();
                   }

               } else {
                   System.out.println("Update Error");
               }

The update method code

public void updatepacks(JTextField PackId, JTextField PackName, JTextField VendorName, JTextField PackValue) throws SQLException {


        try{
            Connection conn = DriverManager.getConnection("jdbc:sqlite:packsver3.db");
            String sqlupdate = "Update  packs" + " SET PackName = ?" + " VendorName = ?"  + "PackValue = ? " + "Where PackId = ? ";
            try (PreparedStatement ps = conn.prepareStatement(sqlupdate)) {
                ps.setString(1, String.valueOf(PackId));
                ps.setString(2, String.valueOf(PackName));
                ps.setString(3, String.valueOf(VendorName));
                ps.setString(4, String.valueOf(PackValue));
                ps.executeUpdate();
            }


        } catch (Exception e) {
            e.printStackTrace();

            }
        }

And the error

enter image description here

Advertisement

Answer

You missed the commas in the UPDATE statement. It should look like:

String sqlupdate = "Update  packs" 
  + " SET PackName = ?, " // added comma at the end
  + " VendorName = ?, " // added comma at the end
  + "PackValue = ? "
  + "Where PackId = ? ";
       
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement