Java – incorrect MySQL date value: null

I'm trying to update a table from a Java application where a column may be null I tried several different methods, but I always got the following mistakes:

com. MysqL. jdbc. Mysqldatatruncation: data truncation: incorrect date value: column 1 'scheidingsdatum' column is' null '

I ensure that the table allows null values for scheidingsdatum fields and that null values can be inserted when inserting MySQL directly

This is the table structure in phpMyAdmin:

Table using InnoDB

I have tried the following solutions:

1: Just use a null variable in the parameter

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 in query (within if / else block)

stmnt = conn.prepareStatement("UPDATE gezinnen SET "
                            + "ouder1 = ?,"
                            + "scheidingsdatum = NULL "
                            + "WHERE gezinsNummer = ?");

stmnt.setString(1,nummer);

3: Use 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: Use setnull (4, Java. SQL. Types. Null)

stmnt = conn.prepareStatement("UPDATE gezinnen SET "
                            + "ouder1 = ?,java.sql.Types.NULL);
stmnt.setString(5,nummer);

Here is my database Properties file and connection creation:

database. properties

jdbc.drivers=com.MysqL.jdbc.Driver
jdbc.url=jdbc:MysqL://IP:3306/TABLE_NAME
jdbc.username=USER
jdbc.password=PASSWORD

Connection creation

Class.forName(props.getProperty("jdbc.drivers")).newInstance();
this.conn = (Connection) DriverManager.getConnection(props.getProperty("jdbc.url"),props.getProperty("jdbc.username"),props.getProperty("jdbc.password"));

Solution

I just did a test and it worked for me with stmnt setNull(4,java.sql.Types.Date); Are you sure about stmnt setString(3,huwelijksdatum); The value of huwelijksdatum is a valid MySQL date string, not "null"?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>