Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert current date and time in a database using SQL?

Tags:

java

sql

I used the following code, but the DateTime field in SQL is represented as:

2005-04-08 00:00:00

I want to have the time too. what should I change?

Here is my code below:

// Get the system date and time.
java.util.Date utilDate = new Date();
// Convert it to java.sql.Date
java.sql.Date date = new java.sql.Date(utilDate.getTime());
...
...
...
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setDate(1, date);
like image 697
lakshmi Avatar asked Jul 28 '10 07:07

lakshmi


1 Answers

Try using setTimestamp instead of setDate, since a timestamp is the prefered format for data and time.

like image 74
Christian Seifert Avatar answered Sep 22 '22 06:09

Christian Seifert