CallableStatements
to Execute Stored ProceduresConnector / J完全实现了 java.sql.CallableStatement
接口。
Connector / J通过JDBC CallableStatement
接口公开存储过程功能。
以下示例显示了一个存储过程,该存储过程返回inOutParam
递增1 的值,并将使用的字符串inputParam
作为 ResultSet
:
Connector / J:调用存储过程
1 CREATE PROCEDURE demoSp(IN inputParam VARCHAR(255), 2 INOUT inOutParam INT) 3 BEGIN 4 DECLARE z INT; 5 SET z = inOutParam + 1; 6 SET inOutParam = z; 7 8 SELECT inputParam; 9 10 SELECT CONCAT(‘zyxw‘, inputParam); 11 END
要使用demoSp
Connector / J 的过程,请按照下列步骤操作:
1、使用准备可调用语句 Connection.prepareCall()
。
请注意,您必须使用JDBC转义语法,并且参数占位符周围的括号不是可选的:
Connector / J:使用 Connection.prepareCall()
1 import java.sql.CallableStatement; 2 3 ... 4 5 // 6 // Prepare a call to the stored procedure ‘demoSp‘ 7 // with two parameters 8 // 9 // Notice the use of JDBC-escape syntax ({call ...}) 10 // 11 12 CallableStatement cStmt = conn.prepareCall("{call demoSp(?, ?)}"); 13 14 15 16 cStmt.setString(1, "abcdefg");
2、注册输出参数(如果存在)
要检索输出参数(参数指定的值,OUT
或 INOUT
在创建存储过程),JDBC要求他们执行语句之前,使用各种指定 registerOutputParameter()
的方法CallableStatement
接口:
Connector / J:注册输出参数:
1 import java.sql.Types; 2 ... 3 // 4 // Connector/J supports both named and indexed 5 // output parameters. You can register output 6 // parameters using either method, as well 7 // as retrieve output parameters using either 8 // method, regardless of what method was 9 // used to register them. 10 // 11 // The following examples show how to use 12 // the various methods of registering 13 // output parameters (you should of course 14 // use only one registration per parameter). 15 // 16 17 // 18 // Registers the second parameter as output, and 19 // uses the type ‘INTEGER‘ for values returned from 20 // getObject() 21 // 22 23 cStmt.registerOutParameter(2, Types.INTEGER); 24 25 // 26 // Registers the named parameter ‘inOutParam‘, and 27 // uses the type ‘INTEGER‘ for values returned from 28 // getObject() 29 // 30 31 cStmt.registerOutParameter("inOutParam", Types.INTEGER); 32 ...
3、设置输入参数(如果存在)
输入和输入/输出参数设置为 PreparedStatement
对象。但是, CallableStatement
还支持按名称设置参数:
Connector / J:设置CallableStatement
输入参数:
1 ... 2 3 // 4 // Set a parameter by index 5 // 6 7 cStmt.setString(1, "abcdefg"); 8 9 // 10 // Alternatively, set a parameter using 11 // the parameter name 12 // 13 14 cStmt.setString("inputParam", "abcdefg"); 15 16 // 17 // Set the ‘in/out‘ parameter using an index 18 // 19 20 cStmt.setInt(2, 1); 21 22 // 23 // Alternatively, set the ‘in/out‘ parameter 24 // by name 25 // 26 27 cStmt.setInt("inOutParam", 1); 28 29 ...
4、执行CallableStatement
,并检索任何结果集或输出参数。
虽然CallableStatement
支持调用任何Statement
执行方法(executeUpdate()
, executeQuery()
或 execute()
),但最灵活的调用方法是execute()
,因为如果存储过程返回结果集,则不需要提前知道:
Connector / J:检索结果和输出参数值
1 ... 2 3 boolean hadResults = cStmt.execute(); 4 5 // 6 // Process all returned result sets 7 // 8 9 while (hadResults) { 10 ResultSet rs = cStmt.getResultSet(); 11 12 // process result set 13 ... 14 15 hadResults = cStmt.getMoreResults(); 16 } 17 18 // 19 // Retrieve output parameters 20 // 21 // Connector/J supports both index-based and 22 // name-based retrieval 23 // 24 25 int outputValue = cStmt.getInt(2); // index-based 26 27 outputValue = cStmt.getInt("inOutParam"); // name-based 28 29 ...
参考链接:
https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-statements-callable.html
使用JDBC CallableStatements执行存储过程
原文:https://www.cnblogs.com/yuanchao-blog/p/10713516.html