我一直在尝试在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)
我一直试图找出错误很多小时,但没有成功。 我看到很多问题,但是: this is useless because in function cannot be transaction
this is useless because when i use named parameter NullPointerEx is throwed(why??)
this is useless because i cant see any obvious mistake
this is useless because update JDBC connector didnt help
所以我的问题很简单:任何想法可能出错?
答案 0 :(得分:0)
我会尝试两件事:
"jdbc:mysql://127.0.0.1/test_database"
连接字符串,然后移除execute("USE test_database")
setInt
和registerOutParameter
行。一般来说,执行USE test_database
应该没问题,但MySQL documentation明确提醒使用它:
始终使用
Connection.setCatalog()
方法在JDBC应用程序中指定所需的数据库,而不是USE database
语句。
虽然上下文略有不同,但似乎同样的建议也适用于您的情况。