sql timestamp java,在sql查询中使用java.sql.Timestamp对象

在尝试使用Java执行SQL查询时遇到了错误'ORA-01843:not a valid month'。问题出在将java.sql.Timestamp对象转换为字符串进行日期比较时格式不正确。解决方案是避免直接字符串拼接SQL命令,以防止SQL注入风险,并确保日期格式与数据库期望的格式匹配。
摘要由CSDN通过智能技术生成

I am trying to run a query in java that uses a java.sql.Timestamp object as the date to compare with in the where clause.

Here is how the query string that is built in Java

String getTransactionsSQL = "Select transaction_seq " +

"From transactions ct " +

"Where id = 'ABCD'" +

"And ct.out_msg_timestamp" +

"<= to_date('" + parseDate.getTimeStamp() +"','YYYY-MM-DD HH:MI:SS..')" +

"order by transaction_seq";

The statement parseDate.getTimeStamp() returns a java.sql.TimeStamp object that contains a date. Here is an example output of System.out.println(parseDate.getTimeStamp());

2011-03-07 05:47:57.0

When i run the above query i get this error

java.sql.SQLException: ORA-01843: not a valid month

Any clues?

解决方案

Never use string concatenation to pass arguements to SQL commands (security risk: SQL injection)!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值