0

Hi i have a column (type date).I want to insert custom date and time without using Preparedstatement .i have used

String date = sf.format(Calendar.getInstance().getTime());
String query = "Insert into entryTbl(name, joinedDate, ..etc)  values ("abc", to_date(date, 'yyyy/mm/dd HH:mm:ss'))";
statement.executeUpdate(query);

but am getting literal doesnot match error. so even tried with "SYSDATE".Its inserting only date not time.So how to insert the datetime using java into oracle?please any one help..

1
  • 1
    any specific reason to not use PreparedStatement? Commented Oct 26, 2012 at 4:40

2 Answers 2

1

It is strongly recommended to use PreparedStatement to counter potential SQL injection attacks, instead of building raw SQL queries to interact with the database.

That said, you can specify the value for date column as a string in yyyy-MM-dd HH:mm:ss format as well:

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sf.format(Calendar.getInstance().getTime());
String query = "INSERT INTO entryTbl(name, joinedDate, ..etc.,)  values ('abc', '" 
    + date + "', ...etc.,)";
statement.executeUpdate(query);
Sign up to request clarification or add additional context in comments.

1 Comment

Because of lot columns in my table.We do not know lot of fields whether we put setInt() ,setString() and where fields also by using PreparedStatement.So we have used directly query to execute tat command...and above query also not able to inserting wat u mentioned...isthere any other way?
0

I think yyyy/mm/dd HH:mm:ss should be updated as : YYYY/MM/DD HH:MI:SS TZH:TZM and use to_timestamp--documentation here

 String query = "INSERT INTO entryTbl(name, joinedDate, ..etc.,) "+
           "values ('abc', "+
           "TO_TIMESTAMP('"+dateString+"', 'YYYY/MM/DD HH:MI:SS TZH:TZM'), etc.,)";

1 Comment

@shrr: Please share the dataString? Also share the new error after using the above syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.