How to insert current date into MySQL database use Java?

java, mysql

Solution

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);

Problem

I want to insert the current date and time into a columns defined as datetime type. I typed the following code: ``` java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime()); PrepStmt.setDate(1, sqlDate); ``` When I check the database, I find that the date inserted correctly, but the time is: 00:00:00. What is the fix ?

Original source