简体   繁体   English

jdbc DELETE更新无法在Java代码中运行。

[英]jdbc DELETE update not working in Java code.

I am working on a small Java JFrame desktop application for my website's staff to be able to delete all reported posts with just one click. 我正在为我的网站工作人员开发一个小型Java JFrame桌面应用程序,以便能够一键删除所有报告的帖子。

So far, the GUI is correct, with 2 buttons- a getReports button to give the amount of posts, and a deleteReports button. 到目前为止,GUI是正确的,有2个按钮-一个getReports按钮(用于提供帖子数量)和一个deleteReports按钮。

The getReports function is working properly. getReports函数正常运行。 However, for whatever reason, the DELETE query is not working. 但是,无论出于何种原因,DELETE查询都不起作用。 Interestingly enough, when I manually use the query on the database, it deletes the rows in question, so I don't think it is a problem with the query. 有趣的是,当我在数据库上手动使用查询时,它删除了有问题的行,因此我认为查询没有问题。

Here are, in order, the getReports and deleteReports methods: 依次是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;
        } 



}

thoughts? 想法? any help would be appreciated. 任何帮助,将不胜感激。

This was a simple and unbeleivable mistake on my part. 就我而言,这是一个简单而难以想象的错误。

The app has 2 buttons, getReports and deleteReports. 该应用程序有2个按钮:getReports和deleteReports。 I forgot to call the deleteReports() method in the listener method for the delete button. 我忘记了在删除按钮的侦听器方法中调用deleteReports()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM