当用户在删除字段中输入错误的ID时,我想要弹出一个错误.但即使输入了错误的ID,查询仍会继续,但不会删除任何数据.这是我的代码:
String value = jTextField19.getText();
if (value == null || "".equals(value)) {
JOptionPane.showMessageDialog(null,"The field is blank!");
} else {
theQuery("DELETE FROM inventorydb WHERE item_id=('"+jTextField19.getText()+"') AND item_id IS NOT NULL");
}
theQuery方法:
private void theQuery(String query) {
Connection con = null;
Statement st = null;
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventory","root","");
st = con.createStatement();
st.executeUpdate(query);
JOptionPane.showMessageDialog(null,"Done!");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null,"Error!");
}
}