使用PreparedStatement在H2中设置日期/时间值

时间:2014-09-11 20:30:04

标签: java jdbc h2

我无法弄清问题是什么。这里没有做任何与其他数据库不同的事情,但在尝试使用H2(v1.3.176)时根本就没有运气。该表是:

CREATE TABLE AccessesByMinute (
  Count INT NOT NULL,
  UrlID BIGINT NOT NULL,
  ServerID BIGINT NOT NULL,
  RemoteIP VARCHAR(50) NOT NULL,
  Longitude FLOAT,
  Latitude FLOAT,
  Moment DATETIME,
  UserID DATETIME
);

代码是:

...
Date timestamp = Utils.getNearest(Calendar.MINUTE, access.getTimestamp());
...
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.SECOND, 0);
....

     try (PreparedStatement ps = con.prepareStatement("INSERT INTO AccessesByMinute (Moment, UserID, UrlID, ServerID, RemoteIP, Longitude, Latitude, Count) VALUES (?, ?, ?, ?, ?, ?, ?, 1)");
      {
        ps.setTimestamp(1, new Timestamp(timestamp.getTime()));
        ps.setLong(2, userId);
        ps.setLong(3, urlId);
        ps.setLong(4, serverId);
        ps.setString(5, access.getRemoteIp());
        ps.setFloat(6, (loc == null ? 0 : loc.longitude));
        ps.setFloat(7, (loc == null ? 0 : loc.latitude));
        ps.executeUpdate();  
      }

并继续收到错误:

  

org.h2.jdbc.JdbcSQLException:无法解析“TIMESTAMP”常量“1”;   SQL语句:

timestamp.getTime()返回一个long,它是java.sql.Timestamp的参数 我没看到什么?

0 个答案:

没有答案