簡體   English   中英

通過CallableStatement調用存儲過程時出現“參數不是OUT參數”錯誤

[英]“Parameter is not an OUT parameter” error while calling stored procedure via CallableStatement

我一直在嘗試在MySQL Workbanch中創建的調用過程“ proc”:

create database test_database;
use test_database;

delimiter &&
create procedure proc(inout param INT UNSIGNED)
begin
    set param = 2*param;
end&&

使用此應用程序:

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

但我總是得到這個錯誤:

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)

我一直試圖找出問題出在何處,但沒有成功。 我看到了很多問題,但是: 這是沒有用的,因為在函數中不能進行事務處理

這是沒有用的,因為當我使用命名參數時會拋出NullPointerEx(為什么?)

這是沒有用的,因為過程存在,並且當我僅使用IN參數時不拋出任何異常,但是使用INOUT或OUT時會拋出所提到的異常

這是沒有用的,因為我看不到任何明顯的錯誤

這是沒有用的,因為更新JDBC連接器沒有幫助

所以我的問題很簡單:知道有什么問題嗎?

我會嘗試兩件事:

  1. 使用"jdbc:mysql://127.0.0.1/test_database"連接字符串,並刪除對execute("USE test_database")的調用
  2. 交換setIntregisterOutParameter行。

通常,執行USE test_database應該可以,但是MySQL文檔明確警告再次使用它:

始終使用Connection.setCatalog()方法在JDBC應用程序中而不是USE database語句中指定所需的數據庫。

盡管上下文略有不同,但似乎相同的建議也適用於您的情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM