I am trying to pass a date parameter into a mysql stored procedure using a java preparedStatement. But regardless of whether or not the variable is populated, I am getting the error
**com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: ''**
According to the database schema, the dateofbirth column can be null.
I am not sure why the error is coming. I am using a string input and converting it to sql date using the following code:
if(!dob.equals("")){
try {
contact.setDob(new Date(format.parse(dob).getTime()));
System.out.println("New date:" + contact.getDob());
} catch (ParseException e) {
e.printStackTrace();
}
}else{
contact.setDob(null);
}
My stored procedure is below:
DELIMITER $$
DROP PROCEDURE IF EXISTS `Contacts`.`InsertContactInfo` $$
CREATE PROCEDURE `Contacts`.`InsertContactInfo` (salutation Text, category Text, firstName varchar(45), middleName varchar(45), lastName varchar(45), dob date, dom date, sex char(1), email varchar(30))
BEGIN
Insert into contact_info(Salutation,Category,sex,FirstName,LastName,MiddleName, DateofBirth, DateofMarriage, Email)
Values(salutation,category,sex,firstname,middlename,lastname,dob,dom,email);
END $$
DELIMITER ;
and am using the following code to enter the data:
java.sql.PreparedStatement statement = con.prepareStatement("call InsertContactInfo(?,?,?,?,?,?,?,?,?)");
statement.setString(1, contact.getSalutation());
statement.setString(2, contact.getCategory());
statement.setString(3, contact.getSex());
statement.setString(4, contact.getFirstName());
statement.setString(5, contact.getLastName());
statement.setString(6, contact.getMiddleName());
if(contact.getDob()!= null){
statement.setDate(7, contact.getDob());
}else{
statement.setNull(7, Types.DATE);
}
if(contact.getDom()!= null){
statement.setDate(8, contact.getDom());
}else{
statement.setDate(8, null);
}
statement.setString(9, contact.getEmail());
statement.executeUpdate();
Not sure where am making a mistake. Any ideas?