How to insert current date into MySQL database use Java?

You are using wrong method. You can use PreparedStatement#setTimestamp(int parameterIndex, Timestamp x) instead.


What you need to use is the setTimestamp(int parameterIndex,Timestamp x) method instead of the setDate() method.

One of the ways you can set the timestamp would be as follows:

Timestamp timestamp = new Timestamp(new Date().getTime());

You can then set the parameter as:

PrepStmt.setTimestamp(1, timestamp);

Since you are using datetime as your column type, you need to use java.sql.Timestamp to store your date, and PrepareStatement.setTimestamp to insert it.

Try using this: -

java.sql.Timestamp date = new java.sql.Timestamp(new java.util.Date().getTime());
PrepStmt.setTimestamp(1, date);

Tags:

Mysql

Java