【问题标题】:Parameter index out of range (2 > number of parameters, which is 1)参数索引超出范围(2 > 参数个数,即 1)
【发布时间】:2011-09-07 08:23:29
【问题描述】:

我写了下面的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)

【问题讨论】:

  • 完整的堆栈跟踪或方法名称会很有帮助。
  • 嗨 KARASZI István,我刚刚将它添加到问题中 :)...
  • 这个重复的行是一个错误吗? psEmail.setInt( ++countInsert, rsUserEmailMap.getInt( "user_id" ) );
  • 我想我发现了我所做的错误:) psEmail = hrmsCon.prepareStatement(query);应该是 psEmail = hrmsCon.prepareStatement(queryInsert);非常感谢大家:)

标签: java mysql prepared-statement


【解决方案1】:

你应该使用

psEmail = hrmsCon.prepareStatement(queryInsert );

而不是

psEmail = hrmsCon.prepareStatement( query );

复制和粘贴代码时要小心... :-)

【讨论】:

  • 非常感谢您的回答和建议:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-21
  • 2018-08-09
  • 2017-08-29
  • 2017-05-15
  • 2014-03-29
  • 1970-01-01
相关资源
最近更新 更多