Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Incorrect date value: null

Tags:

java

mysql

I'm trying to update a table from a Java application where a certain column may be NULL. I have tried several different approaches but I always get the following error:

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: 'null' for column 'scheidingsdatum' at row 1

I made sure that the table allowed NULL values for the scheidingsdatum field, and can insert NULL values when directly inserting in MySQL

This is the table structure in PHPMyAdmin:

PHPMyAdmin table structure

The tables use innoDB

I have tried the following solutions:

1: Just use the NULL variable in a 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 the query (inside if/else block)

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

stmnt.setString(1, ouder1);
stmnt.setString(2, ouder2);
stmnt.setString(3, huwelijksdatum);
stmnt.setString(4, nummer);

3: Use setNull(4, java.sql.Types.DATE)

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.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 = ?," 
                            + "ouder2 = ?," 
                            + "huwelijksdatum = ?,"
                            + "scheidingsdatum = ? "
                            + "WHERE gezinsNummer = ?");

stmnt.setString(1, ouder1);
stmnt.setString(2, ouder2);
stmnt.setString(3, huwelijksdatum);
stmnt.setNull(4, java.sql.Types.NULL);
stmnt.setString(5, nummer);

the following 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"));
like image 374
Cas Avatar asked Oct 19 '15 20:10

Cas


People also ask

How do I fix incorrect datetime value in MySQL?

The obvious way to fix the error is to change the formatting of your value into the format that MySQL can accept. But rather than editing the value manually, you can use the STR_TO_DATE() function to help you convert the string value into date value.

Can date be NULL in MySQL?

MySQL DOES accept null values for the datetime definition, but if you for some reason think otherwise and won't use a null value, consider simply using '1000-01-01' as the value and excluding rows which have that value for your bill_date column in your queries. Mysql does allow nulls in datetime fields.

How to fix error 1292 in MySQL?

Several options: change the sql_mode to allow zero dates, by removing NO_ZERO_DATE and NO_ZERO_IN_DATE . change the created column to allow NULL values, and update the existing rows to change the zero dates to null values. update the existing rows to change the zero dates to a valid date.

Can we change date format in MySQL?

MySQL uses yyyy-mm-dd format for storing a date value. This format is fixed and it is not possible to change it. For example, you may prefer to use mm-dd-yyyy format but you can't. Instead, you follow the standard date format and use the DATE_FORMAT function to format the date the way you want.


1 Answers

I just made a test and it worked for me with stmnt.setNull(4, java.sql.Types.Date);, are you sure that for stmnt.setString(3, huwelijksdatum); the value of huwelijksdatum is a valid mysql date string and not "null" ?

like image 102
Eusebiu Biroas Avatar answered Oct 17 '22 03:10

Eusebiu Biroas