my dog learned polymorphism*
The moose likes JDBC and the fly likes how can i insert current date and time to db Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how can i insert current date and time to db" Watch "how can i insert current date and time to db" New topic
Author

how can i insert current date and time to db

Mullin Yu
Greenhorn

Joined: Feb 24, 2005
Posts: 14
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);
omar bili
Ranch Hand

Joined: Aug 13, 2004
Posts: 177
try using java.util.GregorianCalendar
Bili
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Even i had the same proble..where the time got converted to 0's...

and date remained fine..

What i did was used util.date for dates instead of sql.date

and while updating the db converted that into string of the format

"yyyy-MM-dd HH:mm:ss"

And it worked fine for me..

Even though we should have been using sql.date but i could nt get it working..I know it was a turnaround..but

Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

User java.sql.Timestamp instead of java.sql.Date. java.sql.Date does not carry time information (as per the SQL specification!); java.sql.Timestamp does.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Originally posted by Joel McNary:
User java.sql.Timestamp instead of java.sql.Date. java.sql.Date does not carry time information (as per the SQL specification!); java.sql.Timestamp does.

Indeed.

It's an extremely important skill to learn to read the API and become familiar with the tools you will use to program Java. Java has an extensive set of documentation that you can even download for your convenience. These "javadocs" are indexed and categorized so you can quickly look up any class or method. Take the time to consult this resource whenever you have a question - you'll find they typically contain very detailed descriptions and possibly some code examples.

Java� API Specifications
Java� 1.5 JDK Javadocs
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
Actually, depending on the database you're using, you might consider setting up your table so that it uses a timestamp field, or so your date field otherwise has the current date/time as the default value.

Then you don't have to worry about the current date/time in your Java code. You simply insert the new record, either passing a "null", or just not giving it any data for that field. The database engine will automatically use the current date/time when it adds the record.

Although, again, whether you can do this, and how you would do it, all depends on your back-end database.

- Jeff
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Moving to JDBC...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 388
You can also insert directly using SQL with SYSDATE
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can i insert current date and time to db