0

I've searched stackoverflow but did not found the solution (at least the way I want it).

I have a JSP page which calls a Java method to insert a date into Oracle database. It passes a String. The problem, how to build the string to execute the insert?

String myInsert = "INSERT INTO table_name 
  values (..., to_date(<<Java variable name>>, 'yyyy/mm/dd hh:mm'), ....);

where Java variable name refers to a variable of type String. I want to let Oracle to the job, not necessarily using SimpleDateFormat, if it's possible. So, should I use '' or " " Youre help would be very much appreciated

  • What is your question here? – Anil Reddy Yarragonda Jul 03 '15 at 14:57
  • possible duplicate of [Java Date - Insert into database](http://stackoverflow.com/questions/1081234/java-date-insert-into-database). Focus on [this Answer](http://stackoverflow.com/a/1081242/642706). And please **search StackOverflow before posting**. I found that Answer in the *first hit* of a search for "java date insert". – Basil Bourque Jul 03 '15 at 17:54

1 Answers1

0

You should use a PreparedStatement along with its setDate function and not deal with date to string conversions yourself.

See http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setDate(int,%20java.sql.Date)

String myInsert="INSERT INTO table_name values(...?)";
PreparedStatement ps=connection.prepareStatement(myInsert);
ps.setDate(1,<<java variable name>>);
...
JP Moresmau
  • 7,388
  • 17
  • 31