Hi Everybody

I am using tomcat on Mac OS X. One of my servlets does the following:

1. I am creating a table ROLE as follows:
________________________________________________________________________ _______________

stmt.executeUpdate( "CREATE TABLE ROLE (" +
"ID INTEGER NOT NULL AUTO_INCREMENT, " +
"NAME VARCHAR(20) NOT NULL, " +
"OWNERID INTEGER NOT NULL, " +
"PRIMARY KEY( ID )" +
")");
________________________________________________________________________ _______________


2. I want to insert (ID, name, ownerid) in the above table .. if ID != -1 and only (name, ownerid) if ID == -1;
I do this as follows:
________________________________________________________________________ _______________

PreparedStatement pstmt = null;

if(roleId != -1)
{
pstmt = con.prepareStatement("INSERT INTO ROLE VALUES (?,?,?)");
pstmt.setInt(1, roleId);
pstmt.setString(2, roleName);
pstmt.setInt(3, ownerId);
}
else
{
pstmt = con.prepareStatement("INSERT INTO ROLE VALUES (?,?)");
pstmt.setString(1, roleName);
pstmt.setInt(2, ownerId);
}

pstmt.executeUpdate();
________________________________________________________________________ _______________

It throws an exception at pstmt.executeUpdate() .. saying:
________________________________________________________________________ _______________
java.sql.SQLException: General error, message from server: "Column count doesn't match value count at row 1"
________________________________________________________________________ _______________

Therefore I understood that maybe i need to make a change to the above code as follows:
________________________________________________________________________ _______________
...
// SAME AS BEFORE
...
else
{
pstmt = con.prepareStatement("INSERT INTO ROLE VALUES (?,?)");
pstmt.setString(2, roleName);
pstmt.setInt(3, ownerId);
}

pstmt.executeUpdate();

________________________________________________________________________ _______________
Now it gives me an error saying:
________________________________________________________________________ _______________
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
________________________________________________________________________ _______________

Does anybody know what i am doing wrong here ?

Thanks in advance
Mufaddal.

Reply via email to