获取 CURRENT_TIMESTAMP 作为结果集的一部分
除了 IDENTITY 字段之外,还有什么方法可以检索 Java 中数据库生成的值吗?我可以轻松地从 ResultSet 获取 IDENTITY 值,但我想获取数据库生成的日期字段的值 (CURRENT_TIMESTAMP)。我不想发送另一个 SELECT 查询来获取日期。
statement = connection.prepareStatement("INSERT INTO foo (bar_date) VALUES (CURRENT_TIMESTAMP)");
ResultSet generatedKey = statement.getGeneratedKeys();
while (generatedKey.next()) {
// read the key..., this unfortunately only returns IDENTITY columns.
}
is there any way of retrieving database generated values in Java other than IDENTITY fields? I can easily get IDENTITY values from a ResultSet, but I'd like to get the value of a date field which has been generated by the database (CURRENT_TIMESTAMP). I prefer not to send another SELECT query to get the date.
statement = connection.prepareStatement("INSERT INTO foo (bar_date) VALUES (CURRENT_TIMESTAMP)");
ResultSet generatedKey = statement.getGeneratedKeys();
while (generatedKey.next()) {
// read the key..., this unfortunately only returns IDENTITY columns.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过类似的内容
看看 OUTPUT 子句(事务 SQL)
Have you tried something like
Have a look at OUTPUT Clause (Transact-SQL)
假设您使用的是 SQL 2005 或更高版本,您可以将
OUTPUT
子句添加到插入语句以使其返回结果集。我不确定检索结果集的 Java 语法,但它会类似于以下内容:
Assuming you're using SQL 2005 or later, you could add an
OUTPUT
clause to your insert statement to have it return a result set.I'm not sure of the Java syntax to retrieve the resultset, but it would be something like the following: