SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
java.util.Date parsedDate = sdf.parse("201212130900");
java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
try
{
PreparedStatement pstmt = connection.prepareStatement(
"select count(*) as counter from table1 where table_key > ?");
pstmt.setTimestamp(1,new java.sql.Timestamp(timestamp.getTime()));
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
System.out.println(rs.getInt( 1 ));
}
connection.close();
}
catch(Exception g){
System.err.println(g.getMessage());
}
In the output I am receiving an error as
ORA-01843: not a valid month.
Can anyone assist to solve the above error ?
解决方案
Your conversion from string to timestap is OK.(I tested and works).
You problem may be in the time zone or something.
Try to use this method instead,
setTimestamp(int parameterIndex, Timestamp x, Calendar cal)
Example:
setTimestamp(1, timestamp , Calendar.getInstance(TimeZone.getTimeZone("UTC")))
UPDATED: (table1.table_key is an CHAR(24))
You should give you date from java in a String, assuming key_value is formatted as "yyyyMMddhhmm"... (for obvious reasons other format will not work)
Other option is in your query convert key_value to TIMESTAMP
Try: (I'm assuming you are working with oracle db, and I do some search)
1) Do a "cast" to timestamp
"select count(*) as counter from table1 where timestamp(table_key) > ?"
"select count(*) as counter from table1 where TO_TIMESTAMP(table_key,'YYYYMMDDHHMMHH24MI') > ?"