1

我正在使用以下语句从我的 SQL DB 中获取时间戳:

stmt.setTimestamp(i++, new java.sql.Timestamp(example.getExampleDate().getTime()))

哪个工作得很好并返回:

2013-02-22 12:27:34.0 

现在碰巧我需要它更精确,像这样:

2013-02-22 12:27:34.000

所以我在文档中找到了以下方法,这显然正是我想要的:

setNanos(int n)

将此 Timestamp 对象的 nanos 字段设置为给定值。

但我需要弄清楚如何将其包含在我准备好的陈述中?

我试过例如

stmt.setTimestamp(i++, new java.sql.Timestamp(example.getExampleDate().getTime()).setNanos(3));

但除此之外返回以下错误:

The method setTimestamp(int, Timestamp) in the type PreparedStatement is not applicable for the arguments (int, void)

非常感谢你的帮助!

4

1 回答 1

1

setNanos()返回无效。所以表达式new java.sql.Timestamp(example.getExampleDate().getTime()).setNanos(3)是 void 类型。您不能将 void 传递给方法setTimestamp()。您必须通过时间戳。

所以使用一个变量:

Timestamp timestamp = new Timestamp(example.getExampleDate().getTime());
timestamp.setNanos(3);
stmt.setTimestamp(i++, timestamp);
于 2016-11-11T12:45:39.027 回答