初学JDBC(七)-使用CallableStatement接口调用存储过程
来源:互联网 发布:人工智能丛书 编辑:程序博客网 时间:2024/04/30 17:18
上一篇博客讲了对大数据对象的处理操作,用到了Clob和blob接口,这一篇博客我来说说CallableStatement接口调用存储过程。
CallableStatement的所有超级接口为PreparedStatement、Statement、Wrapper。其中继承自PreparedStatement接口。CallableStatement主要是调用数据库中的存储过程。在使用CallableStatement时可以接收存储过程的返回值。CallableStatement对象为所有的DBMS提供了一种标准的形式去调用数据库中已存在的存储过程。对数据库中存储过程的调用是CallableStatement对象所含的内容。有两种形式:1:形式带结果参数;2:形式不带结果参数。结果参数是一种输出参数(存储过程中的输出OUT参数),是存储过程的返回值。两种形式都有带有数量可变的输入、输出、输入和输出的参数。用问号做占位符。
形式带结果参数语法格式:{ ? = call 存储过程名[(?, ?, ?, ...)]};
形式不带结果参数语法格式:{ call 存储过程名[(?, ?, ?, ...)]};PS方括号里面的内容可有可无。
CallableStatement接口中常用的方法。
1:getInt(int parameterIndex)、getInt(String parameterName)、还有getString、getBigDecimal、getString、getDate、getURL等等都类似和PreparedStatement与Statement中的用法类似。
2:registerOutParameter(int parameterIndex, int sqlType):按顺序位置parameterIndex将OUT参数注册为JDBC类型sqlType。
3:wasNull():查询最后一个读取的OUT参数是否为SQL Null。等等还有很多方法,感兴趣的读者可以自行查阅JDK API文档。
讲解了那么多,不如一个例子来的痛快。下面通过一个例子让读者更清楚的看到CallableStatement的用法。
首先在原先的t_employee表中添加表示干了多少年的tyear字段。
alter table t_employee add tyear int;
在数据库中编写存储过程统计指定id的userName的人,输出一下他一共赚了多少钱。
JDBC代码:
创建的存储过程为:
delimiter &&create procedure pro_getCountById(in tid int, out counts double, out userNames varchar(20))begin select salary*tyear into counts from t_employee where id = tid; select userName into userNames from t_employee where id = tid;end&&测试:call pro_getCountById(1, @counts, @userNames);select @counts, @userNames;怎么样,你学会了多少?要加油哦!PS下面多出的HTML代码我也不知道咋样删除,是没有用的。
0 0
- 初学JDBC(七)-使用CallableStatement接口调用存储过程
- 使用CallableStatement调用存储过程
- jdbc CallableStatement存储过程
- 调用存储过程CallableStatement
- callablestatement调用存储过程
- java基础巩固---jdbc接口CallableStatement执行存储过程
- JDBC回顾四CallableStatement进行存储过程的调用
- 老调重弹:JDBC系列 之 存储过程 CallableStatement(创建和使用)
- 老调重弹:JDBC系列 之 存储过程 CallableStatement(创建和使用)
- 使用CallableStatement执行存储过程
- JDBC进阶之存储过程+CallableStatement
- [疯狂Java]JDBC:CallableStatement执行存储过程
- 从零开始学JDBC--1.8 CallableStatement-- 存储过程
- 使用JDBC调用存储过程
- 使用JDBC调用存储过程
- 使用JDBC调用存储过程
- 使用JDBC调用存储过程
- 使用jdbc调用存储过程
- 解析Spark开源框架elephas之一
- 安卓Andriod使用入门(十七)【卡片视图的形式管理Activity】
- 现代通用编程语言简介
- 单例
- 如何通过ip访问MySql数据库
- 初学JDBC(七)-使用CallableStatement接口调用存储过程
- 免安装版tomcat 配置相关问题 startup闪退
- 安卓Touch事件的分发机制
- Android插件化学习之路(二)之ClassLoader完全解析
- 分布式系统知识罗列
- 【品高云7年】五、集团云的发展在于可持续运营
- 利用SharedPreferences进行对象存储
- 【杭电2050】折线分割平面-递推
- 汉化教程: PS3游戏资源分析