oracle 在 sql 查询中使用 java.sql.Timestamp 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5265000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a java.sql.Timestamp object in an sql query
提问by ziggy
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 中运行一个查询,该查询使用 java.sql.Timestamp 对象作为要在 where 子句中进行比较的日期。
Here is how the query string that is built in Java
这是在 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());
该语句parseDate.getTimeStamp()
返回一个java.sql.TimeStamp
包含日期的对象。这是一个示例输出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?
有什么线索吗?
回答by Puce
Use PreparedStatement: http://download.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html
使用 PreparedStatement:http: //download.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html
Never use string concatenation to pass arguements to SQL commands (security risk: SQL injection)!
切勿使用字符串连接将参数传递给 SQL 命令(安全风险:SQL 注入)!