javasqloracle-databasejdbcprepared-statement

Using setDate in PreparedStatement


In order to make our code more standard, we were asked to change all the places where we hardcoded our SQL variables to prepared statements and bind the variables instead.

I am however facing a problem with the setDate().

Here is the code:

DateFormat dateFormatYMD = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
DateFormat dateFormatMDY = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date now = 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 INTO 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 get this error when the SQL gets executed:

java.lang.IllegalArgumentException
    at java.sql.Date.valueOf(Date.java:138)
    at com.cmsi.eValuate.TAF.TAFModuleMain.CallTAF(TAFModuleMain.java:1211)

Should I use setString() instead with a to_date()?


Solution

  • Using java.sql.Date

    If your table has a column of type DATE:

    Using java.sql.Timestamp

    If your table has a column of type TIMESTAMP or DATETIME: