gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:01:15 24 4
gpt4 key购买 nike

我正在努力从 MySQL 存储过程中获取结果 OUT 变量。我收到以下错误:

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

存储过程如下所示:

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

我的 java 函数采用字符串数组* 并动态创建 CallableStatement 对象:

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 ;
}

*) 我想我应该使用 int 数组而不是字符串数组,但这似乎不是错误消息的内容。

无论如何,这是它生成的输出:

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)

任何想法可能是什么问题? :)

提前致谢!

最佳答案

我的代码略有不同

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

注意括号,在我的例子中是?在句子的开头。不使用括号我得到了和你一样的异常

关于java.sql.SQLException : Parameter number X is not an OUT parameter 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3017934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com