Java – how to insert the current date and time in the database using SQL?

I use the following code, but the datetime field in SQL is represented as:

2005-04-08 00:00:00

I also want to have time How can I change it?

This is my code as follows:

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

Solution

Try using settimestamp instead of setDate because timestamp is the preferred format for data and time

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>