java sql insert preparedstatment错误

时间:2017-04-07 16:38:43

标签: java sql jdbc netbeans

>>> a
    A   B   C  D
0  98  84   3  1
1  13  35  76  1
2  17  84  28  1
3  22   9  41  1
4  54   3  20  1
>>> b
    A   B   C  D
0  98  84   3  1
1  13  35  76  1
2  17  84  28  1
3  22   9  41  1
4  54   3  20  1
>>> 

错误

String sql = " INSERT INTO `tblservice` (`ServiceID`,`accountID`, `Kind`, `Description`, `Price`, "
        + "`Quantity`, `Total`, `DateAndTime`) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pstm = conn.prepareStatement(sql);
pstm.setInt(1, this.accountID);
pstm.setString(2, "" + SelectionBox.getSelectedItem());
pstm.setString(3, desc);
pstm.setFloat(4, Float.parseFloat(PriceTF.getText()));
pstm.setFloat(5, Float.parseFloat(QuantityTF.getText()));
pstm.setFloat(6, this.getTotal());
pstm.setDate(7, dateAdded);

pstm.executeUpdate();

1 个答案:

答案 0 :(得分:0)

而不是在查询VALUES (NULL, ...)中使用NULL,而是使用setNull,例如:

pstm.setNull(1, java.sql.Types.INTEGER);

这个字段的类型,在这个例子中我认为它是java.sql.Types.INTEGER它可以是java.sql.Types.VARCHAR或任何sql type

所以你的查询应该是这样的:

String sql = "INSERT INTO tblservice (ServiceID, accountID, Kind, Description, 
               Price, Quantity, Total, DateAndTime) 
               VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

PreparedStatement pstm = conn.prepareStatement(sql);
pstm.setNull(1, java.sql.Types.INTEGER);
pstm.setInt(2, this.accountID);
....