Reputation: 1329
I'm trying to save a timestamp to a table in mySQL but whenever I look at the results it just shows 0000-00-00 00:00:00.
I assume I'm not using the timestamp right but anyways in my table I have a column named time and its property is TIMESTAMP
In my Java I have:
java.sql.Timestamp timestamp = new java.sql.Timestamp(0);
String query = "insert ignore into time(time_now) values (?)";
pstmt.setString(1, timestamp);
pstmt.executeUpdate();
My database connection is fine as I have a lot of other information that's being uploaded to it with no problem, I'm just having trouble with the timestamp
Upvotes: 1
Views: 184
Reputation: 204746
If you only want to store the time then use the TIME
data type instead and to insert the current time of the SQL server use CURTIME()
like this
insert into your_table (time_column)
values (curtime())
Upvotes: 1
Reputation: 21
Take a look at java.sql.Timestamp. You are instantiating a new Timestamp object with '0'.
Upvotes: 0
Reputation: 1620
Something like this should work:
java.util.Date date = new java.util.Date();
Timestamp timestamp = new Timestamp(date.getTime());
preparedStatement = connection.prepareStatement("insert ignore into time(time_now) values (?)");
preparedStatement.setTimestamp(1, timestamp);
Upvotes: 1