Problem with PreparedStatements parameter settings
Posted by:
tolle
Date: February 01, 2005 07:00AM
Hi,
I am using MySQL version 4.1.8 and MySQL Connector/J version 3.1.6.
When running the following code I receive the error detailMessage= "java.lang.ArrayIndexOutOfBoundsException"
when executing the prepared statement. (Using the statement st
the insertion works fine.)
public void insert() {
try {
Statement st = this.con.createStatement();
st.execute("insert into resources (ID, URI, LIT) values (1, 'test', 0)");
con.commit();
pstmt = this.con.prepareStatement("insert into resources (ID, URI, LIT) values (?, ?, ?)");
pstmt.setInt(1,1);
pstmt.setString(2,"test");
pstmt.setInt(3,0);
pstmt.execute();
} catch (Exception e) {
System.out.println(e);
}
}
This was a test run to better locate the error.
The original problem occured by porting an application running on IBMs DB2 to MySQL.
There I have a bunch of PreparedStatements and the error message I receive
there differs from the one above.
At the first time of using a PreparedStatement there I receive:
detailMessage= "Parameter index out of bounds. 2 is not between valid values of 1 and 1"
... but there are 2 ? in the SQL statement:
originalSql= "insert into resources (ID, URI, LIT) values (?, ?, 0)"
... later in the execution I receive:
detailMessage= "Statement parameter 4 not set."
... having the SQL statement:
originalSql= "insert into classes values (?, ?, ?)"
Am I doing something wrong?
Thanks for any help.
Karsten