I am trying to insert a Date into mySql db . I accept date as a string and parse it to util.Date then convert it to sql.Date and then I am inserting into database . Here is my code
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
String mydate=dsd.getDoa();
Date mydate2=format.parse(mydate);
java.sql.Date sqlDate;
sqlDate = new java.sql.Date(mydate2.getTime());
System.out.println("Date"+sqlDate.getDate()+"Month"+sqlDate.getMonth()+"Year"+sqlDate.getYear());
....
int i=st.executeUpdate("insert into device_sales_details values("+sqlDate+"));
The error is as follow ,
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: '1980' for column 'device_sales_details_DOA' at row 1
The Sysout
prints as follow for input 22/12/2014
Date22Month11Year114
How to solve this problem , can any one help me in this please.
Use a
PreparedStatement
. It's almost never correct to use string concatenation to create SQL code to send through JDBC.More about not using string concatenation here: http://bobby-tables.com
Separately, it's generally best to specify the columns when doing an insert, e.g.:
insert into device_sales_details (columnName) values (?)