JDBC(13)—JDBC调用存储过程和函数
步骤:
JDBC调用存储过程和函数
步骤:①:通过Connection对象的prepareCall()方法创建一个CallableStatement对象的实例,在使用Connection对象的preparedCall()方法时,需要传入一个String类型的字符串,该字符串指明如何调用存储过程。
函数: [? = call [(,, … )]]
存储过程: [call [(,, … )]]②:通过CallableStatement对象reisterOutParameter方法注册OUT参数
③:通过CallableStatement对象setXxx()方法设定IN或IN OUT参数,若想把参数设为null,使用setNull()方法
④:通过CallableStatement对象的execute()方法执行存储过程。
⑤:如果调用的是带返回值的存储过程,还需要CallableStatement对象的getXxx()方法获取其返回值。
注:通过数据字典查看存储过程或函数的定义。
2 .示例代码
java调用存储过程
/**
* 调用存储过程
*/
@Test
public void testCallableStatement(){
Connection conn = null;
CallableStatement callablestatement = null;
try {
conn = TestTools.getConnection();
String sql = "{ call add0 (?, ?, ?) }";
//1.获取CallableStatement类是对象
callablestatement = conn.prepareCall(sql);
//2.注册OUT参数
callablestatement.registerOutParameter(3, Types.NUMERIC);
//3.通过setXxx()方法设定IN或OUT参数值
callablestatement.setInt(1, 10);
callablestatement.setInt(2, 20);
//4.执行存储过程
callablestatement.execute();
//5.若有返回值,需要使用getXxx()接收
int sum = callablestatement.getInt(3);
System.out.println("结果:"+sum);//结果:30
} catch (Exception e) {
e.printStackTrace();
}finally{
TestTools.release(callablestatement, conn);
}
}
创建存储过程的SQL语句
#定义有返回值的存储过程,大小写无所谓,功能:计算两个数的值
#注意:过程名不能使用add
CREATE PROCEDURE add0(IN a INT,IN b INT,OUT result INT)
BEGIN
IF a IS NULL THEN SET a = 0;
END IF;
IF b IS NULL THEN SET b = 0;
END IF;
SET result = a + b;
SELECT result AS SUM;
END;
#调用存储过程
call add0 (10, 20, @result);
##定义无返回值的存储过程,大小写无所谓,功能:计算两个数的值
CREATE PROCEDURE add1( a INT, b INT)
BEGIN
DECLARE result INT;
IF a IS NULL THEN SET a = 0;
END IF;
IF b IS NULL THEN SET b = 0;
END IF;
SET result = a + b;
SELECT result AS SUM;
END;
#调用存储过程
call add1 (10, 20);
java调用函数
/**
* 调用函数
*/
@Test
public void testCallableStatement1(){
Connection conn = null;
CallableStatement callablestatement = null;
try {
conn = TestTools.getConnection();
String sql = "{? = call add_fun (?, ?) }";
//1.获取CallableStatement类是对象
callablestatement = conn.prepareCall(sql);
//2.注册OUT参数,其中1表示第一个问号,表示输出
callablestatement.registerOutParameter(1, Types.NUMERIC);
//3.通过setXxx()方法设定IN或OUT参数值,2,3表示第二个和第三个问号,表述输入。
callablestatement.setInt(2, 10);
callablestatement.setInt(3, 20);
//4.执行存储过程
callablestatement.execute();
//5.若有返回值,需要使用getXxx()接收
int sum = callablestatement.getInt(1);
System.out.println("结果:"+sum);
} catch (Exception e) {
e.printStackTrace();
}finally{
TestTools.release(callablestatement, conn);
}
}
创建函数的mysql语句:
#创建有返回值的函数,注意分号不要少
create function add_fun(a int, b int)
returns int
begin
declare c int;
set c = a + b;
return c;
end;
#调用函数
select add_fun(10,10) as sum
JDBC(13)—JDBC调用存储过程和函数的更多相关文章
- Java 调用存储过程、函数
一.Java调用存储Oracle存储过程 测试用表: --创建用户表 create table USERINFO ( username ) not null, password ) not null ...
- 转:EF调用存储过程、函数
EF调用存储过程.函数 2014-04-02 09:12:20| 分类: ORM框架|举报|字号 订阅 一.ef4.1 codeFirst 修改表结构 增加字段等 EF code ...
- MySQL学习笔记:调用存储过程或函数报1418错误
问题 MySQL开启bin-log后,调用存储过程或者函数以及触发器时,会出现错误号为1418的错误: ERROR 1418 (HY000): This function has none of DE ...
- JDBC第二篇--【PreparedStatment、批处理、处理二进制、自动主键、调用存储过程、函数】
这是我JDBC的第一篇 http://blog.csdn.net/hon_3y/article/details/53535798 1.PreparedStatement对象 PreparedState ...
- JDBC【PreparedStatment、批处理、处理二进制、自动主键、调用存储过程、函数】
1.PreparedStatement对象 PreparedStatement对象继承Statement对象,它比Statement对象更强大,使用起来更简单 Statement对象编译SQL语句时, ...
- jdbc调用存储过程和函数
1.调用存储过程 public class CallOracleProc { public static void main(String[] args) throws Exception{ Stri ...
- 【转】java调用存储过程和函数
一.概述 如果想要执行存储过程,我们应该使用 CallableStatement 接口. CallableStatement 接口继承自PreparedStatement 接口.所以CallableS ...
- MyBatis中调用存储过程和函数
一.调用存储过程 1.首先在数据库中定义存储过程,定义的存储过程的代码如下: //定义存储过程 create or replace procedure pag_add(p1 varchar2,p2 v ...
- MySQL存储过程和自定义函数、Navicat for mysql、创建存储过程和函数、调用存储过程和函数的区别
1 MySQL存储过程和函数 过程和函数,它们被编译后保存在数据库中,称为持久性存储模块(Persistent Stored Module,PSM),可以反复调用,运行速度快. 1.1 存储过程 存储 ...
随机推荐
- Java集合源码学习(四)HashMap
一.数组.链表和哈希表结构 数据结构中有数组和链表来实现对数据的存储,这两者有不同的应用场景,数组的特点是:寻址容易,插入和删除困难:链表的特点是:寻址困难,插入和删除容易:哈希表的实现结合了这两点, ...
- bzoj 松鼠的新家
哈夫曼距离与切比雪夫距离的转化
- windows server远程连接提示“终端服务器超出了最大允许连接”
- Codeforces 781D Axel and Marston in Bitland 矩阵 bitset
原文链接https://www.cnblogs.com/zhouzhendong/p/CF781D.html 题目传送门 - CF781D 题意 有一个 n 个点的图,有 m 条有向边,边有两种类型: ...
- 046 SparlSQL中的函数
一:SparkSQL中的函数 1.说明 2.展示所有的函数 qlContext.sql("show functions").show(300) 3.functions类 所有内置支 ...
- Maya cmds pymel 获取安装选择顺序选择的物体
Maya cmds pymel 获取安装选择顺序选择的物体 import maya.cmds as cmds 先设置选择顺序 cmds.selectPref(trackSelectionOrder = ...
- hdu1202解题报告
#include<stdio.h>int main(){ int n,i; double sum,s,p,sum1; while(scanf("%d" ...
- 6491: Daydream
题目描述 You are given a string S consisting of lowercase English letters. Another string T is initially ...
- Codeforces 919D Substring 【拓扑排序】+【DP】
<题目链接> 题目大意:有一个具有n个节点,m条边的有向图,每个点对应一个小写字母,现在给出每个顶点对应的字母以及有向边的连接情况,求经过的某一条路上相同字母出现的最多次数.如果次数无限大 ...
- UVA 796 Critical Links(模板题)(无向图求桥)
<题目链接> 题目大意: 无向连通图求桥,并将桥按顺序输出. 解题分析: 无向图求桥的模板题,下面用了kuangbin的模板. #include <cstdio> #inclu ...