1

I have been using Java to store some records from a csv file. One of these records is a date. The problem here is I am using JDBC to store these records in a database. Now, the Date object of Java is showing an error while putting it into the database. I have been stuck on this for a while. Please let me know how to solve this. I have used type Date in mysql for storing it into the database. Here is the part creating the problem.

DateFormat d1 = new SimpleDateFormat("yyyy-MM-dd");
Date d = new Date(); 
System.out.println(d1.format(d));
String sql = "INSERT INTO TESTING"+
            "VALUES("+"DATE_FORMAT("+d+","+"'%Y-%m-%d'"+"))";
stmt.executeUpdate(sql);

If I execute this directly in MySqlWorkBench, it is storing the date properly. But through JDBC it is a problem.

The error is as shown below :

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Jun 30 14:04:03 IST 2016,'%Y-%m-%d'))' at line 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
    at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1402)
    at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1317)
    at payex.writeDB(payex.java:221)
    at payextestdrive.main(payextestdrive.java:11)
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • 2
    "Now, the Date object of Java is showing an error while putting it into the database" - What error? Using string concatenation is *not* right solution here... you should absolutely be using a PreparedStatement, but you need to use `java.sql.Date`, not `java.util.Date`. – Jon Skeet Jun 30 '16 at 08:48
  • You can refer to this post too http://stackoverflow.com/questions/2305973/java-util-date-vs-java-sql-date – shazinltc Jun 30 '16 at 08:49
  • 1
    `"INSERT INTO TESTING"+"VALUES...` can't work – wero Jun 30 '16 at 08:50
  • You should really be using PreparedStatement and queries with parameter plaeholders instead of concatenating values into a query string. – Mark Rotteveel Jun 30 '16 at 09:01

2 Answers2

1

The error message is pretty clear

right syntax to use near 'Jun 30 14:04:03 IST 2016,'%Y-%m-%d'))'

The format you provided is MMM DD HH:mm:ss Z YYYY, but the system expects %Y-%m-%d

Try

DateFormat d1 = new SimpleDateFormat("yyyy-MM-dd");
Date d = new Date(); 
System.out.println(d1.format(d));
String sql = "INSERT INTO TESTING"+
            " VALUES("+"DATE_FORMAT('"+d1.format(d)+"',"+"'%Y-%m-%d'"+"))";
stmt.executeUpdate(sql);

or simply

DateFormat d1 = new SimpleDateFormat("yyyy-MM-dd");
Date d = new Date(); 
System.out.println(d1.format(d));
String sql = "INSERT INTO TESTING"+
            " VALUES('"+d1.format(d)+"')";
stmt.executeUpdate(sql);
  • Hey thanks for your answer. I tried this but I'm getting an exception. – Yyash Bhatia Jul 01 '16 at 05:24
  • com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '2008' at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2983) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723) at com.mysql.jdbc.Connection.execSQL(Connection.java:3277) at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1402) at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1317) at payex.writeDB(payex.java:221) at payextestdrive.main(payextestdrive.java:11) – Yyash Bhatia Jul 01 '16 at 05:25
1

You add d.toString() to the SQL command when you want to add the formatted date string. Also a space is missing between the table name and VALUES:

String sql = "INSERT INTO TESTING VALUES(DATE_FORMAT(" + d1.format(d) +",'%Y-%m-%d'))";
wero
  • 32,544
  • 3
  • 59
  • 84
  • Hey I tried this. Thanks for your answer. But there's an exception again. – Yyash Bhatia Jul 01 '16 at 05:17
  • com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '2008' at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2983) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723) at com.mysql.jdbc.Connection.execSQL(Connection.java:3277) at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1402) at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1317) at payex.writeDB(payex.java:220) at payextestdrive.main(payextestdrive.java:11) – Yyash Bhatia Jul 01 '16 at 05:17