java - Using setDate in PreparedStatement -


in order make our code more standard, asked change places hardcoded our sql variables prepared statements , bind variables instead.

i facing problem setdate().

here code:

        dateformat dateformatymd = new simpledateformat("yyyy/mm/dd hh:mm:ss");         dateformat dateformatmdy = new simpledateformat("mm/dd/yyyy hh:mm:ss");         date = new date();         string vdateymd = dateformatymd.format(now);         string vdatemdy = dateformatmdy.format(now);         string vdatemdysql =  vdatemdy ;         java.sql.date date = new java.sql.date(0000-00-00);     requestsql = "insert credit_req_title_order (request_id," +                  " order_dt, followup_dt) " +  "values(?,?,?,)";                   prs = conn.preparestatement(requestsql);                  prs.setint(1,new integer(requestid));                  prs.setdate(2,date.valueof(vdatemdysql));                 prs.setdate(3,date.valueof(sqlfollowupdt)); 

i error when sql gets executed:

    java.lang.illegalargumentexception     @ java.sql.date.valueof(date.java:138)     @ com.cmsi.evaluate.taf.tafmodulemain.calltaf(tafmodulemain.java:1211) 

should use setstring() instead to_date()?

❐ using java.sql.date

if table has column of type date:

  • java.lang.string

    the method java.sql.date.valueof(java.lang.string) received string representing date in format yyyy-[m]m-[d]d. e.g.:

    ps.setdate(2, java.sql.date.valueof("2013-09-04")); 
  • java.util.date

    suppose have variable enddate of type java.util.date, make conversion thus:

    ps.setdate(2, new java.sql.date(enddate.gettime()); 
  • current

    if want insert current date:

    ps.setdate(2, new java.sql.date(system.currenttimemillis()));  // since java 8 ps.setdate(2, java.sql.date.valueof(java.time.localdate.now())); 

❐ using java.sql.timestamp

if table has column of type timestamp or datetime:

  • java.lang.string

    the method java.sql.timestamp.valueof(java.lang.string) received string representing date in format yyyy-[m]m-[d]d hh:mm:ss[.f...]. e.g.:

    ps.settimestamp(2, java.sql.timestamp.valueof("2013-09-04 13:30:00"); 
  • java.util.date

    suppose have variable enddate of type java.util.date, make conversion thus:

    ps.settimestamp(2, new java.sql.timestamp(enddate.gettime())); 
  • current

    if require current timestamp:

    ps.settimestamp(2, new java.sql.timestamp(system.currenttimemillis()));  // since java 8 ps.settimestamp(2, java.sql.timestamp.from(java.time.instant.now())); ps.settimestamp(2, java.sql.timestamp.valueof(java.time.localdatetime.now())); 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -