问题描述
我正在尝试在java中运行一个查询,该查询使用java.sql.Timestamp对象作为与where子句进行比较的日期。
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.
以下是用Java构建的查询字符串的方式
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";
语句 parseDate.getTimeStamp()
返回包含日期的 java.sql.TimeStamp
对象。以下是 System.out.println(parseDate.getTimeStamp())的示例输出;
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
任何线索?
推荐答案
使用PreparedStatement:
Use PreparedStatement: http://download.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html
永远不要使用字符串连接将争论传递给SQL命令(安全风险:SQL注入)!
Never use string concatenation to pass arguements to SQL commands (security risk: SQL injection)!
这篇关于在sql查询中使用java.sql.Timestamp对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!