Search code examples
javaoracle-databaseprepared-statementsqlexception

insert throws invalid column index


I have an oracle database with following scheme:

  • DM_ID, auto numbering PK
  • t_ID, varchar
  • Workflow, varchar
  • Technology, varchar
  • Name_event, varchar
  • Time_event, date

I use following method to make some inserts in the database.

private void insert(String folder, int seconds) throws SQLException {
          stm = connection.prepareStatement("INSERT INTO DM_PROCMON (DM_ID,t_ID, WORKFLOW, TECHNOLOGY, NAME_EVENT, TIME_EVENT) VALUES(DM_ID.nextval,'?', 'up', 'Folder','?', ?)");
            stm.setString(1, String.format("%d%d%d.txt",random.nextInt(999),random.nextInt(999),random.nextInt(999)));
            stm.setString(2, folder);
            stm.setTime(3, new Time(seconds * 1000));
            stm.execute();
        }

What I get is an SQL exception which says:

java.sql.SQLException: Invalid column index

When I debug, I see it's on the line:

stm.setString(2, folder);

I don't see what the problem is.


Solution

  • Remove the apex around the ?

    stm = connection.prepareStatement("INSERT INTO DM_PROCMON (DM_ID,t_ID, WORKFLOW, TECHNOLOGY, NAME_EVENT, TIME_EVENT) VALUES(DM_ID.nextval,?, 'up', 'Folder',?, ?)");
    

    and try again.

    is the method setString of PreparedStatement that handle the apex around any string for you.