Parameter index out of range error

i have a PreparedStatement I construct using this for loop:

preparedStatement ="insert into ? values('?','?','";

int numberOfMnemonics = mnemonicList.size();

for(int i=0;i<numberOfMnemonics-1;i++){

preparedStatement +="? '" + COMMA +"' ";

}

preparedStatement +="?');";

the 'numberOfMnemonics' field is usually somewhere around 50-75.

Then I run the following code:

PS = conn.prepareStatement(preparedStatement);

PS.setString(1,tableName);

PS.setString(2,count);//count is just an int

on the second line I get the following error:

java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).

why is this? Do i need to insert the '?' into the PreparedStatement string in a

different way? What am i missing here? I need to be able to fill in those other 60+ parameters as well.

Thanks

[1222 byte] By [pnandrusa] at [2007-11-27 3:43:40]
# 1

You don't need quotes for the strings in the prepared statement. If you use setString, it will know to add them on its own. Also. I am not sure you can assign the table name the way you are currently doing it.

I dont' think you need the semicolon at the end either.

If you can construct the query string first, print it and make sure its valid syntax, then add the values to it, it might make it a little easier.

Message was edited by:

kdajani

kdajania at 2007-7-12 8:47:17 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...