how to insert current date and time to db

M

Mullin

hi,

i use the following code, but the DateTime field at mysql is
2005-04-08 00:00:00

i want to have the time too. where should i change

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

Wannabee

"Mullin" wrote
hi,

i use the following code, but the DateTime field at mysql is
2005-04-08 00:00:00

Sorry, I don't really know the types in MySql, but maybe DateTime is
compatible with java.sql.TimeStamp ?

So you might create a TimeStamp and try to set that to the PreparedStatement
?
 
S

shakah

Mullin said:
hi,

i use the following code, but the DateTime field at mysql is
2005-04-08 00:00:00

i want to have the time too. where should i change

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

You can sidestep the issue by using the database's current date/time,
perhaps something like:
PreparedStatement stmt = connection.prepareStatement(
" INSERT INTO tableA(colA, colB, colC)"
+ " VALUES(?, NOW(), ?)"
) ;
stmt.setString(1, "contents of colA") ;
stmt.setString(2, "contents of colC") ;
stmt.executeUpdate() ;

That's arguably better, as it avoids issues with client-side
differences in clock settings. Otherwise, in your example you can
probably do stmt.setTimestamp(1, new java.util.Date()), though I'm not
sure.
 
C

christopher

I only use java dates, not sql dates, and store the long getTime()
value directly in the database
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top