Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

save Date() into mysql Timestamp

 
Mike Southgate
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic