java - sql删除没有错误但不要删除记录(除非我在Where子句中使用Firstname)

标签 java sql

我创建了一个删除数据库中人员姓名的方法。使用名字(where 子句),sql 命令正在运行并成功删除记录。但是,在 sql 命令中使用“姓氏”可以工作,但不会删除记录。我输入了姓氏,在名字之前

public static void deletePerson (String lname ) {

     String  host = "jdbc:mysql://localhost/Students";
     String username = "username";
     String password = "password";
    try {
        Connection connect = DriverManager.getConnection(host, username, password);

            // this is not working, I dont know what wrong with this ione
        PreparedStatement statement = connect.prepareStatement("DELETE FROM person WHERE Lastname = ?");
        statement.setString(2, lname);
        statement.close();
        connect.close();
        System.out.println("worrrk");


          // this is the working
        //PreparedStatement statement = connect.prepareStatement("DELETE FROM person WHERE Firstname = ?");
        //statement.setString(1, fname);



    } catch (SQLExcepton e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

最佳答案

让我们看看您已采取的步骤...

您已经创建了一个 PreapredStatement,很好...

PreparedStatement statement = connect.prepareStatement("DELETE FROM person WHERE Lastname = ?");

您设置的参数值...不正确...

statement.setString(2, lname);

只有一个参数,所以应该是

statement.setString(1, lname);

您关闭了声明...

statement.close();

等等,什么?您是否打算实际对数据库“执行”某些命令?

尝试在 statement.close(); 之前插入 statement.executeUpdate()

现在我们已经介绍了这一点,让我们讨论一下您的(缺乏)资源管理...

如果 Connection connect = ...connect.close() 之间出现问题,connect.close() (可能statement.close())语句永远不会被执行,使数据库资源保持打开状态,但不执行任何操作(被浪费)...

幸运的是,例如,Java 7+ 现在可以轻松管理此内容

try (Connection connect = DriverManager.getConnection(host, username, password)) {
    try (PreparedStatement statement = connect.prepareStatement("DELETE FROM person WHERE Lastname = ?")) {

        statement.setString(1, lname);
        int rowsDeleted = statement.executeUpdate();
        // You could inspect the number of rows deleted
        System.out.println("worrrk");

    }
} catch (SQLException e) {
    e.printStackTrace();
}

看看The try-with-resources Statement了解更多详情

已更新

在某些情况下,Connection 可能未设置为自动提交,因此您需要手动执行此操作,因此在 int rowsDeleted = statements.executeUpdate(); 您应该在关闭 PreparedStatement 或 `Connection

之前调用 connection.commit();
try (Connection connect = DriverManager.getConnection(host, username, password)) {
    try (PreparedStatement statement = connect.prepareStatement("DELETE FROM person WHERE Lastname = ?")) {

        statement.setString(1, lname);
        int rowsDeleted = statement.executeUpdate();
        if (!connect.getAutoCommit()) { 
            connect.commit();
        }
        // You could inspect the number of rows deleted
        System.out.println("worrrk");

    }
} catch (SQLException e) {
    e.printStackTrace();
}

关于java - sql删除没有错误但不要删除记录(除非我在Where子句中使用Firstname),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28336711/

相关文章:

java - 通过 Java 使用 MySQL

sql - COALESCE(SUM(...),0) COALESCE(COUNT(...),0) 在我的代码中不起作用

sql - 选择所有从未合作过但都与第三方合作过的人对

sql - 如果 Postgresql 的父表中不存在外键,则忽略复制(读取)该行

java - 浏览器代理 : How to determine IP for proxy server?

java - SugarORM 中的持久化对象

java - JApplet repaint() 不起作用

mysql - SQL:如何使用 SQL 查询获取一行中的项目值?

mysql 允许选择无效日期

java - 我怎样才能让这个Java程序循环呢?