简体   繁体   中英

insert date in oracle database via jsp java

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

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>>);
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM