Reputation: 4112
I wrote following java method and when I call it, gives me the error
Parameter index out of range (2 > number of parameters, which is 1).
Can anyone please explain me what's wrong with my code?
Thanks a lot in advance!
here is the code
private void updateTskUserEmail( Connection hrmsCon ) throws SQLException
{
ResultSet rsUserEmailMap = null;
PreparedStatement ps = null;
PreparedStatement psEmail = null;
for ( String lbUserEmail : lbUserList )
{
try
{
String query = "SELECT * FROM tsk_user WHERE email=? ";
int count = 0;
ps = hrmsCon.prepareStatement( query );
if ( lbUserEmail == null )
{
ps.setNull( ++count, java.sql.Types.VARCHAR );
}
else
{
ps.setString( ++count, lbUserEmail );
}
rsUserEmailMap = ps.executeQuery();
while ( rsUserEmailMap.next() )
{
String queryInsert = "INSERT INTO tsk_user_email (";
queryInsert += "user_email,";
queryInsert += "user_id)";
queryInsert += " VALUES(?,?) ON DUPLICATE KEY UPDATE user_id=? ";
int countInsert = 0;
psEmail = hrmsCon.prepareStatement( query );
if ( lbUserEmail == null )
{
psEmail.setNull( ++countInsert, java.sql.Types.VARCHAR );
}
else
{
psEmail.setString( ++countInsert, lbUserEmail );
}
psEmail.setInt( ++countInsert, rsUserEmailMap.getInt( "user_id" ) );
psEmail.setInt( ++countInsert, rsUserEmailMap.getInt( "user_id" ) );
psEmail.execute();
}
}
finally
{
if ( ps != null )
{
ps.close();
}
if ( rsUserEmailMap != null )
{
rsUserEmailMap.close();
}
psEmail.close();
}
}
}
full stack trace
java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3711)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3695)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3737)
at com.mysql.jdbc.PreparedStatement.setInt(PreparedStatement.java:3681)
at it.codegen.LbEmailMapper.updateTskUserEmail(LbEmailMapper.java:155)
at it.codegen.LbEmailMapper.main(LbEmailMapper.java:47)
Upvotes: 1
Views: 7886
Reputation: 5921
You should use
psEmail = hrmsCon.prepareStatement(queryInsert );
instead of
psEmail = hrmsCon.prepareStatement( query );
You should be careful when copy & paste code... :-)
Upvotes: 3