I am new to this and trying to add some dates into a Oracle database using prepared statements. this is my code:
CODE REMOVED, code updated.
This produces the following:
INSERT into TEST_RESULTS12233 (DB_ID, DATED1, DATED2, DATED3, DATED4) values (t1_seq.nextval, to_date(?, 'dd/mm/yyyy hh24.mi'), to_date(?, 'dd/mm/yyyy hh24.mi'), to_date(?, 'dd/mm/yyyy hh24.mi'), to_date(?, 'dd/mm/yyyy hh24.mi'))
Event1
Event2
Event3
Event4
Exception in thread "main" java.sql.BatchUpdateException: ORA-01858: a non-numeric character was found where a numeric was expected
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10500)
at oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:230)
at sandpit.testsand.main(testsand.java:85)
If i replace pstatement in setString with Event1, the batch executes. Does anyone know how I can get this to work?? Thanks
Here is the full code as requested:
//MORE CODE AS REQUESTED
String inserttotable = "INSERT into TEST_RESULTS12233 (DB_ID";
String stateclose = ")";
String insertvalues =(" values (t1_seq.nextval");
StringBuilder insertBuilder = new StringBuilder();
StringBuilder valueBuilder = new StringBuilder();
int timestoloop = 4;
for (i=1; i<=timestoloop; i++)
{
insertBuilder.append(", DATED"+i);
valueBuilder.append(", to_date(?, 'dd/mm/yyyy hh24.mi')");
}
String finalinsert = inserttotable + insertBuilder.toString()+ stateclose;
String finalvalues = insertvalues + valueBuilder.toString() + stateclose;
String insertsql = finalinsert + finalvalues;
System.out.println(insertsql);
String insertresults = insertsql;
PreparedStatement prepState = conn.prepareStatement(insertresults);
String Event1 = "22122014 13.26";
String Event2 = "22122014 13.27";
String Event3 = "22122014 13.28";
String Event4 = "22122014 13.29";
for (i=1; i<=timestoloop; i++)
{
String pstatement = "Event"+i;
System.out.println(pstatement);
prepState.setString(i, pstatement);
}
prepState.addBatch();
prepState.executeBatch();
setString(1, "Event1")is not the same assetString(1, Event1)...