How to delete records (strings) Java and MySQL
•
Java
I successfully deleted an integer, but when I tried to change it to string, it would say
Here is my code:
private void DeleteButtonActionPerformed(java.awt.event.ActionEvent evt) { int del = (prompt): if (del == JOptionPane.YES_OPTION){ DelCurRec(); } } public void DelCurRec() { String id = field.getText(); String sql = "DELETE FROM inventory WHERE ItemCode = "+id+" "; try { Class.forName(connectio); } catch (Exception e) { JOptionPane.showMessageDialog(null,""+e.getMessage(),"JDBC Driver Error",JOptionPane.WARNING_MESSAGE); } Statement stmt = null; Connection con = null; //Creates connection to database try { con = DriverManager.getConnection("Connection"); stmt = con.createStatement(); } catch (Exception e) { JOptionPane.showMessageDialog(null,"Connection Error",JOptionPane.WARNING_MESSAGE); } //Execute the sql statment for deleting records try { stmt.executeUpdate(sql); //This closes the connection to the database con.close(); //This closes the dialog JOptionPane.showMessageDialog(null,"Deleted Succesfully","Delete Successful",JOptionPane.WARNING_MESSAGE); } catch (Exception e) { JOptionPane.showMessageDialog(null,"Communication Error",JOptionPane.WARNING_MESSAGE); } }
Solution
Try to change the line:
String sql = "DELETE FROM inventory WHERE ItemCode = "+id+" ";
to
String sql = "DELETE FROM inventory WHERE ItemCode = '"+id+"' ";
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码