我正在尝试从
Java应用程序更新一个表,其中某列可能为NULL.我尝试了几种不同的方法,但是我总是得到以下错误:
com.MysqL.jdbc.MysqLDataTruncation:数据截断:日期值不正确:第1列“scheidingsdatum”列为“null”
我确保该表允许scheidingsdatum字段的NULL值,并且可以在直接插入MySQL时插入NULL值
这是PHPMyAdmin中的表结构:
表使用innoDB
我已经尝试了以下解决方案:
1:只需在参数中使用NULL变量即可
stmnt = conn.prepareStatement("UPDATE gezinnen SET " + "ouder1 = ?," + "ouder2 = ?," + "huwelijksdatum = ?," + "scheidingsdatum = ? " + "WHERE gezinsNummer = ?"); stmnt.setString(1,ouder1); stmnt.setString(2,ouder2); stmnt.setString(3,huwelijksdatum); stmnt.setString(4,scheidingsdatum); stmnt.setString(5,nummer);
2:查询中的Hardcode NULL(if / else块内)
stmnt = conn.prepareStatement("UPDATE gezinnen SET " + "ouder1 = ?," + "scheidingsdatum = NULL " + "WHERE gezinsNummer = ?"); stmnt.setString(1,nummer);
3:使用setNull(4,java.sql.Types.DATE)
stmnt = conn.prepareStatement("UPDATE gezinnen SET " + "ouder1 = ?,huwelijksdatum); stmnt.setNull(4,java.sql.Types.DATE); stmnt.setString(5,nummer);
4:使用setNull(4,java.sql.Types.NULL)
stmnt = conn.prepareStatement("UPDATE gezinnen SET " + "ouder1 = ?,java.sql.Types.NULL); stmnt.setString(5,nummer);
以下是我的database.properties文件和连接创建:
database.properties
jdbc.drivers=com.MysqL.jdbc.Driver jdbc.url=jdbc:MysqL://IP:3306/TABLE_NAME jdbc.username=USER jdbc.password=PASSWORD
连接创建
Class.forName(props.getProperty("jdbc.drivers")).newInstance(); this.conn = (Connection) DriverManager.getConnection(props.getProperty("jdbc.url"),props.getProperty("jdbc.username"),props.getProperty("jdbc.password"));