1 回答
TA贡献1798条经验 获得超7个赞
引用javadoc executeUpdate()
:
返回:
(1) SQL 数据操作语言 (DML) 语句的行计数或 (2) 0(不返回任何内容的 SQL 语句)
所以:
String url = "jdbc:derby://localhost:1527/Cookbook";
try (Connection conn = DriverManager.getConnection(url)) {
String query = "DELETE from RECIPES WHERE NAME = ?";
try (PreparedStatement pst = conn.prepareStatement(query)) {
pst.setString(1, name);
int rowCount = pst.executeUpdate();
if (rowCount == 0) {
JOptionPane.showMessageDialog(null, "'" + name + "' was not found");
} else if (rowCount == 1) {
JOptionPane.showMessageDialog(null, "'" + name + "' was successfully deleted");
} else { // cannot happen if `NAME` has unique index in the database
JOptionPane.showMessageDialog(null, rowCount + " recipes named '" + name + "' were deleted");
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "something went wrong: " + e);
}
添加回答
举报