使用 DELETE 语句截断不正确的 DOUBLE 值

Truncated incorrect DOUBLE value using DELETE statement

int d1;
String attribute = comboBox1.getSelectedItem().toString(); // a combo box
System.out.println(attribute);
String data = t.getText(); // a textfield
System.out.println(data);
if (attribute.equals("COURSE_ID")) {
     IsNumber in = new IsNumber();
     d1 = in.stringToInt(data);
     
    try {
                            
        Connection connection = DriverManager.getConnection(url, username, password);
        System.out.println("connection success!!");
                                                
        String sql = "DELETE FROM course WHERE ? = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, attribute);
        statement.setInt(2, d1);
                            
        boolean rows = statement.execute();
        if (rows == true) {
            new ViewDatabase(user, name, pswrd);
            System.out.println("COURSE_ID UNIT UPDATE SUCCESSFUL!");
            frame.setVisible(false);
        } else if (rows == false) {
            JOptionPane.showMessageDialog(null, "Cannot find row!",
                      "ERROR", JOptionPane.ERROR_MESSAGE);
        }
        
        statement.close();
        connection.close();
    } catch (SQLException e) {
            System.out.println("& i oop");
            e.printStackTrace();
    }

对于这段代码,每当我尝试 运行 它时,它 returns "Data t运行cation: T运行cated incorrect DOUBLE value: 'COURSE_ID'”。我不确定这是指什么,我搜索并发现有人说这个错误消息具有误导性,尽管我只找到了选择、插入和更新的答案,但没有找到删除的答案。

我也按照互联网上的建议在 MySQL 中关闭了严格模式,但无济于事。

不能将字符串绑定到准备好的语句中的实际列名。因此,属性列名称必须是硬编码的。一种可能有效的模式是:

String sql = "";
if ("COL1".equals(attribute)) {
    sql = "DELETE FROM course WHERE COL1 = ?";
}
else if ("COL2".equals(attribute)) {
    sql = "DELETE FROM course WHERE COL2 = ?";
}
else {
    sql = "DELETE FROM course WHERE COL3 = ?";
}
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, d1);
boolean rows = statement.execute();