本机查询错误
我是 sql 新手,我面临以下本机查询的问题
public void saveOfflineBatchDetails(BigInteger user_id) {
em.createNativeQuery("INSERT INTO rst_offline_transaction_batch (created_date , user_id)" +
"VALUES('?1', ?2)")
.setParameter(1, new java.util.Date())
.setParameter(2, user_id)
.executeUpdate();
}
它不会将值传递到数据库。创建日期应该是今天的日期和时间。谁能告诉我这个查询有什么问题。 多谢
Im new to sql and im facing a problem with the below native query
public void saveOfflineBatchDetails(BigInteger user_id) {
em.createNativeQuery("INSERT INTO rst_offline_transaction_batch (created_date , user_id)" +
"VALUES('?1', ?2)")
.setParameter(1, new java.util.Date())
.setParameter(2, user_id)
.executeUpdate();
}
It doesn't pass the values to the database. the created date should be the today's date and time. Can anyone tell me whats wrong in this query.
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,参数化查询不包含单引号。尝试:(
不带
?1
两边的引号)。否则(对于读者和解析器来说)很难判断您是否想要插入参数 1 还是文字值?1
。您还应该检查
executeUpdate()
的返回值,看看它是否认为它影响了任何行。这可能会给你零,但无论如何它都值得检查。最后,我认为日期需要特殊处理:
这是因为 Java Date 对象根本不是日期,而是时间戳 - 您需要确保选择正确的时间对象类型,以便将正确的值放置在询问。
所以,简而言之,类似:
You generally don't include the single quotes for parameterised queries. Try:
(without the quotes around the
?1
). Otherwise it's hard (for both readers and parsers) to tell whether you wanted parameter 1 or the literal value?1
to be inserted.You should also check the return value from
executeUpdate()
to see if it thinks it affected any rows. This will probably give you zero but it's worth checking anyway.And, finally, I think dates require special handling as per:
This is because the Java Date object is not a date at all but a timestamp - you need to ensure you select the correct temporal object type so that the right value is placed in the query.
So, in short, something like: