使用JDBC CallableStatements执行存储过程
Using JDBC CallableStatements to Execute Stored Procedures
Connector / J完全实现了 java.sql.CallableStatement接口。
Connector / J通过JDBC CallableStatement接口公开存储过程功能。
以下示例显示了一个存储过程,该存储过程返回inOutParam递增1 的值,并将使用的字符串inputParam作为 ResultSet:
Connector / J:调用存储过程
CREATE PROCEDURE demoSp(IN inputParam VARCHAR(255), \
INOUT inOutParam INT)
BEGIN
DECLARE z INT;
SET z = inOutParam + 1;
SET inOutParam = z; SELECT inputParam; SELECT CONCAT('zyxw', inputParam);
END
要使用demoSpConnector / J 的过程,请按照下列步骤操作:
1、使用准备可调用语句 Connection.prepareCall()。
请注意,您必须使用JDBC转义语法,并且参数占位符周围的括号不是可选的:
Connector / J:使用 Connection.prepareCall()
import java.sql.CallableStatement;
...
//
// Prepare a call to the stored procedure 'demoSp'
// with two parameters
//
// Notice the use of JDBC-escape syntax ({call ...})
//
CallableStatement cStmt = conn.prepareCall("{call demoSp(?, ?)}");
cStmt.setString(1, "abcdefg");
2、注册输出参数(如果存在)
要检索输出参数(参数指定的值,OUT或 INOUT在创建存储过程),JDBC要求他们执行语句之前,使用各种指定 registerOutputParameter()的方法CallableStatement接口:
Connector / J:注册输出参数:
import java.sql.Types;
...
//
// Connector/J supports both named and indexed
// output parameters. You can register output
// parameters using either method, as well
// as retrieve output parameters using either
// method, regardless of what method was
// used to register them.
//
// The following examples show how to use
// the various methods of registering
// output parameters (you should of course
// use only one registration per parameter).
// //
// Registers the second parameter as output, and
// uses the type 'INTEGER' for values returned from
// getObject()
// cStmt.registerOutParameter(2, Types.INTEGER); //
// Registers the named parameter 'inOutParam', and
// uses the type 'INTEGER' for values returned from
// getObject()
// cStmt.registerOutParameter("inOutParam", Types.INTEGER);
...
3、设置输入参数(如果存在)
输入和输入/输出参数设置为 PreparedStatement对象。但是, CallableStatement还支持按名称设置参数:
Connector / J:设置CallableStatement输入参数:
...
//
// Set a parameter by index
//
cStmt.setString(1, "abcdefg");
//
// Alternatively, set a parameter using
// the parameter name
//
cStmt.setString("inputParam", "abcdefg");
//
// Set the 'in/out' parameter using an index
//
cStmt.setInt(2, 1);
//
// Alternatively, set the 'in/out' parameter
// by name
//
cStmt.setInt("inOutParam", 1);
...
4、执行CallableStatement,并检索任何结果集或输出参数。
虽然CallableStatement支持调用任何Statement执行方法(executeUpdate(), executeQuery()或 execute()),但最灵活的调用方法是execute(),因为如果存储过程返回结果集,则不需要提前知道:
Connector / J:检索结果和输出参数值
...
boolean hadResults = cStmt.execute();
//
// Process all returned result sets
//
while (hadResults) {
ResultSet rs = cStmt.getResultSet();
// process result set
...
hadResults = cStmt.getMoreResults();
}
//
// Retrieve output parameters
//
// Connector/J supports both index-based and
// name-based retrieval
//
int outputValue = cStmt.getInt(2); // index-based
outputValue = cStmt.getInt("inOutParam"); // name-based
...
参考链接:
https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-statements-callable.html
使用JDBC CallableStatements执行存储过程的更多相关文章
- JAVA使用JDBC技术操作SqlServer数据库执行存储过程
Java使用JDBC技术操作SqlServer数据库执行存储过程: 1.新建SQLSERVER数据库:java_conn_test 2.新建表:tb_User 3.分别新建三个存储过程: 1>带 ...
- JDBC连接执行MySQL存储过程报权限错误
今天在测试项目的时候 突然就报了一个错出来. User does not have access to metadata required to determine stored procedure ...
- JDBC连接执行 MySQL 存储过程报权限错误:User does not have access to metadata required to determine stored procedure parameter types. If rights can not be granted,
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- JDBC连接执行mysql存储过程报权限错误:User does not have access to metadata required to determine stored procedure parameter types. If rights can not be granted,
分析:JDBC在调用存储过程时不光用户要有execute的权限,还需要对mysql.proc具有访问权限.否则它无法访问metadata 解决方案:给数据库用户赋权,赋执行mysql.proc表的se ...
- Jdbc执行存储过程报数据库事务无法执行的异常
Jdbc执行存储过程报数据库事务无法执行的异常 环境: Eclipse+Jdk1.7+spring-jdbc-3.0.7+同版本的jdbctemplate+Sqlserver 2012 问题: 一个小 ...
- 原生jdbc执行存储过程
//定时任务,结转 . //表名 fys_sch_lvyou2 ,存储过程名:fys_sch_lvyou2_carrayover //无参调用:{call insertLine} //有参调用:{ca ...
- JDBC执行存储过程的四种情况 (转)
本文主要是总结 如何实现 JDBC调用Oracle的存储过程,从以下情况分别介绍: [1].只有输入IN参数,没有输出OUT参数 [2].既有输入IN参数,也有输出OUT参数,输出是简单值(非列表) ...
- JDBC使用MySQL存储过程错误
JDBC连接执行 MySQL 存储过程报权限错误:User does not have access to metadata required to determine stored procedur ...
- Java JDBC下执行SQL的不同方式、参数化预编译防御
相关学习资料 http://zh.wikipedia.org/wiki/Java数据库连接 http://lavasoft.blog.51cto.com/62575/20588 http://blog ...
随机推荐
- PAT Advanced 1032 Sharing(25) [链表]
题目 To store English words, one method is to use linked lists and store a word letter by letter. To s ...
- PAT Basic 1070 结绳(25) [排序,贪⼼]
题目 给定⼀段⼀段的绳⼦,你需要把它们串成⼀条绳.每次串连的时候,是把两段绳⼦对折,再如下图所示套接在⼀起.这样得到的绳⼦⼜被当成是另⼀段绳⼦,可以再次对折去跟另⼀段绳⼦串连.每次串 连后,原来两段绳 ...
- Qt QRect与QRectF的区别
一直在与QRect和QRectF打交道.甚至在使用过程中因为QRect而出现了致命的Bug.因为QRect在数据存储表示上有一个很大的“历史遗留问题”! QRect Class 也就是说,对于QR ...
- Java 工厂模式登陆系统实现
没有工厂模式 设定一个登陆系统 UserServiceImp.java public class UserServiceImp { public boolean login(String userna ...
- 第二季第八天 HTML5新特性
在函数内部window.a = a 在全局中就可以拿到这个变量 变量命名.作为函数的参数的时候要详细.调用的时候可以简单点 做全局变量的两个方案 1.绑在标签上data 2.闭包 视频一般都是二进 ...
- 简单的tab栏切换
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Windows10配置Jmeter环境
注:在安装Jmeter之前,请先检查下电脑有没有装JDK:[Win+R]然后输入cmd->进入命令行界面,输入java -version 出现以下信息就是此电脑已安装了JDK.由于jmeter要 ...
- Caffe Ubuntu14.04 + CUDA 8 (支持GTX1080 1070等Pascal架构显卡)
1. 前言 本教程使用的系统是Ubuntu 14.04 LTS 64-bit,使用的CUDA版本为8. 理论上本教程支持Pascal架构显卡,如游戏卡GeForce GTX1070,GTX 1080, ...
- 数学是什么?_题跋—>数学是什么?
题跋—>数学是什么? 数学的定义在不同的解释中有不同的释义,它又像是哲学.又像是逻辑性:即研究数量关系.有研究结构和空间关系等等.因此很难给予一个非常准确的定义,正因为如此数学是渗透于生活的各个 ...
- 三十八、LNMP潮流组合搭建
一.安装mysql 数据库 1.1 mysql数据库安装的三种方法: 1)编译安装,在lamp经典组合安装是5.1版本,是configure,make,make install,这里如果是5.5版本 ...