为什么要明确设置' null' JDBC Update操作的值是否失败?

时间:2018-04-12 16:18:42

标签: java jdbc

我正在尝试使用null值更新某些属性。但它总是错误的。这是我的代码

// deleting records of column overtime, medical, bonus, other and totalamount
try {
        String deleteQuery = "update paydb.allowance set "
        + "overtime = ?, "
        + "medical = ?,"
        + "bonus = ?,"
        + "other = ?,"
        + "totalamount = ?"
        + "where emp_id = ?";

        PreparedStatement dpst = conn.prepareStatement(deleteQuery);
        dpst.setNull(1, java.sql.Types.DOUBLE);
        dpst.setNull(2, java.sql.Types.DOUBLE);
        dpst.setNull(3, java.sql.Types.DOUBLE);
        dpst.setNull(4, java.sql.Types.DOUBLE);
        dpst.setNull(5, java.sql.Types.DOUBLE);
        dpst.setString(6, txt_search.getText());

        dpst.executeUpdate();

        JOptionPane.showMessageDialog(null, "Record deleted successfully");
    }
    catch (SQLException e)
    {
        JOptionPane.showMessageDialog(null, e);
    }

1 个答案:

答案 0 :(得分:0)

try {
        String deleteQuery = "update paydb.allowance set "
        + "overtime = ?,"
        + "medical = ?,"
        + "bonus = ?,"
        + "other = ?, "
        + "totalamount = ? "
        + "where emp_id = ? ";
    enter code here
        PreparedStatement dpst = conn.prepareStatement(deleteQuery);
        dpst.setNull(1, java.sql.Types.DOUBLE);
        dpst.setNull(2, java.sql.Types.DOUBLE);
        dpst.setNull(3, java.sql.Types.DOUBLE);
        dpst.setNull(4, java.sql.Types.DOUBLE);
        dpst.setNull(5, java.sql.Types.DOUBLE);
        dpst.setString(6, txt_search.getText());

        dpst.executeUpdate();

        JOptionPane.showMessageDialog(null, "Record deleted successfully");
    }
    catch (SQLException e)
    {
        JOptionPane.showMessageDialog(null, e);
    }