我写了以下java方法,当我调用它时,给我错误
Parameter index out of range (2 > number of parameters, which is 1).
有人可以解释一下我的代码有什么问题吗?
提前多多感谢!
这是代码
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();
}
}
}
完整堆栈跟踪
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)
答案 0 :(得分:2)
你应该使用
psEmail = hrmsCon.prepareStatement(queryInsert );
而不是
psEmail = hrmsCon.prepareStatement( query );
复制时应该小心。粘贴代码......: - )