简体   繁体   English

通过CallableStatement调用存储过程时出现“参数不是OUT参数”错误

[英]“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: 我会尝试两件事:

  1. Use "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")的调用
  2. Swap setInt and registerOutParameter lines. 交换setIntregisterOutParameter行。

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 the USE 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.

相关问题 从Java(Eclipse)调用MySQL存储过程中的OUT参数错误 - OUT Parameter Error in Calling MySQL Stored Procedure from Java (Eclipse) Callablestatement错误:索引处缺少IN或OUT参数:1 - Callablestatement error : Missing IN or OUT parameter at index:: 1 使用带有布尔 IN 参数的 CallableStatement 在 Java 中调用 Oracle PL/SQL 过程会产生 PLS-00306 oracle 错误: - Calling an Oracle PL/SQL procedure in Java using a CallableStatement with a boolean IN parameter gives an PLS-00306 oracle error: 错误:未为存储过程定义参数输出 - Error: Parameter out was not defined for stored procedure 使用Java调用过程时,显示参数编号2不是OUT参数 - Showing parameter number 2 is not an OUT parameter while calling a procedure using java 以数组为参数调用存储过程 - Calling a stored procedure with an array as parameter 使用列表参数调用存储过程 - Calling Stored Procedure With List Parameter 使用 OUT 参数调用存储过程 Spring Spring JPA 2.* - Calling stored procedure using OUT parameter with Spring Spring JPA 2.* 使用jpa 2.0调用具有out参数的存储过程 - calling a stored procedure having out parameter using jpa 2.0 MySQL Java 存储过程给出错误“参数索引 1 超出范围” - MySQL Java stored procedure gives error "Parameter index of 1 is out of range"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM