This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes save Date() into mysql Timestamp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "save Date() into mysql Timestamp" Watch "save Date() into mysql Timestamp" New topic
Author

save Date() into mysql Timestamp

Mike Southgate
Ranch Hand

Joined: Jul 18, 2003
Posts: 183
I have an object with a Date() member. I need to insert this in a MySql database column of type timestamp.

my code is as folllows:

String insrtSQL = "INSERT INTO run VALUES (?,?,?,?,?,NOW(),?,?)";
PreparedStatement ps = cnctn.prepareStatement(insrtSQL);
ps.setString(1, this.userId);
ps.setInt(2, maxRunId);
ps.setTimestamp(3, new Timestamp(((Lap)this.laps.get(1)).getTs().getTime()));
ps.setNull(4, Types.NULL);
ps.setNull(5, Types.NULL);
ps.setString(6, this.comment);
ps.setNull(7, Types.NULL);
ps.execute();

laps is an arraylist containing Lap objects Lap.getTs() returns a java.util.Date. As you can see, I convert java.util.Date into a java.sql.Timestamp. println shows the timestamp as populated correctly but in the database it is just zeros. Any suggestions?

ms


ms<br />SCJP, SCJD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: save Date() into mysql Timestamp
 
Similar Threads
Timestamp assistance
MySQL memory leak
memory leak - java.lang.OutOfMemory
Inserting records into database
I face a New java.sql.SQLException