[英]“Parameter is not an OUT parameter” error while calling stored procedure via CallableStatement
I have been trying call procedure "proc" which were created in MySQL Workbanch: 我一直在尝试在MySQL Workbanch中创建的调用过程“ proc”:
create database test_database;
use test_database;
delimiter &&
create procedure proc(inout param INT UNSIGNED)
begin
set param = 2*param;
end&&
using this application : 使用此应用程序:
package test;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class Test {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1", "root", "root");
connection.createStatement().execute("USE test_database");
CallableStatement callableStatement = connection.prepareCall("{call proc(?)}");
callableStatement.setInt(1, 5);
callableStatement.registerOutParameter(1, java.sql.Types.INTEGER);
ResultSet result = callableStatement.executeQuery();
if (result.first()) {
System.out.println(result.getInt(1));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}}
but i always get this error: 但我总是得到这个错误:
java.sql.SQLException: Parameter number 1 is not an OUT parameter
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1094)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:997)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:983)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:928)
at com.mysql.jdbc.CallableStatement.checkIsOutputParam(CallableStatement.java:695)
at com.mysql.jdbc.CallableStatement.registerOutParameter(CallableStatement.java:2016)
at test.Test.main(Test.java:16)
I have been trying to find out what is wrong many hours, but without success. 我一直试图找出问题出在何处,但没有成功。 I saw many questions, but : this is useless because in function cannot be transaction
我看到了很多问题,但是: 这是没有用的,因为在函数中不能进行事务处理
this is useless because when i use named parameter NullPointerEx is throwed(why??) 这是没有用的,因为当我使用命名参数时会抛出NullPointerEx(为什么?)
this is useless because procedure exists, and dont throw any exception when i use only IN param, but with INOUT or OUT is throwed mentioned exception 这是没有用的,因为过程存在,并且当我仅使用IN参数时不抛出任何异常,但是使用INOUT或OUT时会抛出所提到的异常
this is useless because i cant see any obvious mistake 这是没有用的,因为我看不到任何明显的错误
this is useless because update JDBC connector didnt help 这是没有用的,因为更新JDBC连接器没有帮助
So my question is simple : Any idea what can be wrong? 所以我的问题很简单:知道有什么问题吗?
I would try two things: 我会尝试两件事:
"jdbc:mysql://127.0.0.1/test_database"
connection string, and remove the call of execute("USE test_database")
"jdbc:mysql://127.0.0.1/test_database"
连接字符串,并删除对execute("USE test_database")
的调用 setInt
and registerOutParameter
lines. setInt
和registerOutParameter
行。 In general, executing USE test_database
should be fine, but MySQL documentation explicitly cautions agains using it: 通常,执行
USE test_database
应该可以,但是MySQL文档明确警告再次使用它:
Always use the
Connection.setCatalog()
method to specify the desired database in JDBC applications, rather than theUSE database
statement.始终使用
Connection.setCatalog()
方法在JDBC应用程序中而不是USE database
语句中指定所需的数据库。
Although the context is slightly different, it appears that the same advise applies to your situation as well. 尽管上下文略有不同,但似乎相同的建议也适用于您的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.