简体   繁体   中英

java.sql.SQLException: Parameter number X is not an OUT parameter

I'm struggling with getting the result OUT variable from a MySQL stored procedure. I get the following error:

java.sql.SQLException: Parameter number 3 is not an OUT parameter

The stored procedure looks like this:

CREATE DEFINER=`cv_admin`@`localhost` PROCEDURE `CheckGameEligibility`(
   IN gID INT(10),
   IN uID INT(10),

   OUT result TINYINT(1)
)
BEGIN
    # Do lots of stuff, then eventually:
    SET result = 1;
END 

My java function takes an array of strings* and creates the CallableStatement object dynamically:

public static int callAndReturnResult( String sql , String[] values )
{
    int out = 0 ;
    try
    {
        // construct the SQL. Creates: CheckGameEligibility(?, ?, ?)
        sql += "(" ;

        for( int i = 0 ; i < values.length ; i++ )
        {
            sql += "?, " ;
        }
        sql += "?)" ;

        System.out.println( "callAndReturnResult("+sql+"): constructed SQL: " + sql );

        // Then the statement
        CallableStatement cstmt = DB.prepareCall( sql );
        for( int i = 0 ; i < values.length ; i++ )
        {
            System.out.println( "   " + (i+1) + ": " + values[ i ] ) ;
            cstmt.setString(i+1, values[ i ] );
        }

        System.out.println( "   " + (values.length+1) + ": ? (OUT)" ) ;
        cstmt.registerOutParameter( values.length + 1 , Types.TINYINT );
        cstmt.execute();

        out = cstmt.getInt( values.length );
        cstmt.close();
    }
    catch( Exception e )
    {
        System.out.println( "*** db trouble: callAndReturnResult(" + sql + " failed: " + e );
        e.printStackTrace() ;
    }
    return out ;
}

*) I suppose I should be using an int array instead of a string array, but it doesn't seem to be what the error message was about.

Anyway, here's the output it generates:

callAndReturnResult(CheckGameEligibility(?, ?, ?)): constructed SQL: CheckGameEligibility(?, ?, ?)
1: 57
2: 29
3: ? (OUT)
*** db trouble: callAndReturnResult(CheckGameEligibility(?, ?, ?) failed: java.sql.SQLException: Parameter number 3 is not an OUT parameter
    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.CallableStatement.checkIsOutputParam(CallableStatement.java:692)
    at com.mysql.jdbc.CallableStatement.registerOutParameter(CallableStatement.java:1847)
    at org.apache.commons.dbcp.DelegatingCallableStatement.registerOutParameter(DelegatingCallabl>eStatement.java:92)
    at Tools.callAndReturnResult(Tools.java:156)

Any ideas what might be the problem? :)

Thanks in advance!

My code is slightly different

CallableStatement statement = connection.prepareCall("{? = call nextOperatorTransactionId()}");
statement.registerOutParameter(1, Types.BIGINT);
statement.execute();
id.set(statement.getLong(1));

Notice the brackets and in my case the ? at the begining of the sentence. Without using brackets I got the same exception than you

Try call CheckGameEligibility(?, ?, ?) instead of CheckGameEligibility(?, ?, ?) . It helped me in the same circumstance.

我不确定为什么 Java 或 Java 的 MySQL 驱动程序不喜欢这种语法,但是您是否考虑过将过程创建为返回RETURNS TINYINTFUNCTION

I just got this exception as well, and I found out it was simply a dumb little syntax error I made when constructing my CALL string. Double-check that yours is the correct syntax.

对我来说同样的问题,但我按照 Anthony 的建议修复了检查语法,我发现我正在使用骆驼大小写约定调用存储过程,例如:addPaiment 并且在数据库中它存储为 ADDPAIMENT 所以我相信 mysql 没有找到存储过程。

当您指定的过程名称与 db 中存在的实际过程不同时,我的 sql 会给出此无关的异常。

Here's tutorial on how to call stored procedures from jdbc:
http://java.sun.com/docs/books/tutorial/jdbc/basics/sql.html
The sql format is different from yours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM