我需要将当前日期和时间(毫秒)插入到MySQL中。我做了以下工作:
long time = System.currentTimeMillis();
java.sql.Timestamp timestamp = new java.sql.Timestamp(time);
System.out.println("Time in milliseconds :" + timestamp);我以毫秒为单位正确地获得了时间(例如,2013-03-21 00:08:33.523)。但是,我需要将这些信息插入MySQL数据库。我使用PreparedStatement尝试了以下操作
prepStmt.setTimestamp(2,timestamp);但是插入的时间没有毫秒(例如,2013-03-21 00:08:33)。
如何插入毫秒的时间。
编辑:数据库中的列是DATETIME类型的。
发布于 2013-03-20 21:12:31
java.util.Date date = new java.util.Date();
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
preparedStatement.setTimestamp(1, timestamp);发布于 2013-03-20 21:15:54
它的列类型为DATETIME,使用PreparedStatement的PreparedStatement方法。
java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
stmnt.setDate(1, date);发布于 2014-05-24 16:53:35
您可以创建一个方法getCurrentTimeStamp()并从JDBC语句中调用它
preparedStatement.setTimestamp(4,getCurrentTimeStamp());
方法:
private static java.sql.Timestamp getCurrentTimeStamp() {
java.util.Date today = new java.util.Date();
return new java.sql.Timestamp(today.getTime());
}https://stackoverflow.com/questions/15534775
复制相似问题