Mr.Smithyyy
Mr.Smithyyy

Reputation: 1329

Using timestamp with mysql and java

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

Answers (3)

juergen d
juergen d

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

eera
eera

Reputation: 21

Take a look at java.sql.Timestamp. You are instantiating a new Timestamp object with '0'.

Upvotes: 0

Dermot Blair
Dermot Blair

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

Related Questions