0

I'm using a sql statement in my program.

stringBuffer sql=new StringBuffer();
sql.append("insert into customer (id,createddate) ");
sql.append("values (1,");
sql.append("'"+new Timestamp(System.currentTimeMillis())+"'");

String results=jdbcTemplate.update(sql.toString();

when i executed above command,i got this exception nested exception is java.sql.SQLException: ORA-01843: not a valid month

i should bind the current date ,how do i solve this issue.

Thanks.

Bernad Ali
  • 1,699
  • 6
  • 23
  • 29
  • 3
    Don't use String literals. Use a prepared statement and pass a Timestamp object –  May 07 '12 at 09:44

1 Answers1

4

If you can use the DB's date then use sysdate ie:

stringBuffer sql=new StringBuffer();
sql.append("insert into customer (id,createddate) ");
sql.append("values (1,sysdate)");

Or you can add a to_date to the query:

stringBuffer sql=new StringBuffer();
sql.append("insert into customer (id,createddate) ");
sql.append("values (1,to_date(");
sql.append("'"+<your TimeStamp converted to a String like yyyyMMddHHmmss>+"', 'yyyymmddhh24miss'");

Or use a PreparedStatement as here

Community
  • 1
  • 1
A.B.Cade
  • 16,735
  • 1
  • 37
  • 53