我正在为我的网站工作人员开发一个小型Java JFrame桌面应用程序,只需点击一下即可删除所有报告的帖子。
到目前为止,GUI是正确的,有2个按钮 - 一个getReports按钮来提供帖子数量,还有一个deleteReports按钮。
getReports函数正常运行。但是,无论出于何种原因,DELETE查询都不起作用。有趣的是,当我在数据库上手动使用查询时,它会删除有问题的行,所以我不认为这是查询的问题。
以下是getReports和deleteReports方法的顺序:
public static int getReports() throws InstantiationException, IllegalAccessException {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
String sql = "SELECT count(*) as amount FROM `tryreports`";
ResultSet rs = stmt.executeQuery(sql);
rs.first();
int quantity = rs.getInt("amount");
conn.close();
return quantity;
}
catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
return 0;
}
}
public static void deleteReports() throws InstantiationException, IllegalAccessException {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, username, password);
//System.out.println("Connected database successfully...");
Statement stmt = conn.createStatement();
String sql = "DELETE FROM `tryreports` WHERE posts IS NOT NULL";
stmt.executeUpdate(sql);
}
catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
//return 0;
}
}
想法?任何帮助将不胜感激。
答案 0 :(得分:0)
这对我来说是一个简单且无法容忍的错误。
该应用有2个按钮,getReports和deleteReports。我忘了在监听器方法中为删除按钮调用deleteReports()方法。