java 通过调用存储过程获取结果集
一般在java中,数据查询是通过Statement, PreparedStatement获取结果集,今天向大家介绍通过CallableStatement调用存储过程,从而获取结果集. 本文是所用的数据库为oracle. 一. 测试数据库表:
- create table wilent_user(
- id number(5) primary key,
- name varchar2(100),
- sex varchar2(1), --Y为男,F为女;
- group_id number(5),
- teach varchar2(50) --学历;
- );
- create table wilent_group(
- id number(5) primary key,
- name varchar2(100)
- );
- insert into wilent_group values(1,'组1');
- insert into wilent_group values(2,'组2');
- insert into wilent_group values(3,'组3');
- insert into wilent_group values(4,'组4');
- insert into wilent_group values(5,'组5');
- insert into wilent_user values(1,'吴','Y',1,'大专');
- insert into wilent_user values(2,'李','Y',1,'大专');
- insert into wilent_user values(3,'赵','N',2,'本科');
- insert into wilent_user values(4,'金','Y',2,'高中');
- insert into wilent_user values(5,'钱','N',2,'大专');
- insert into wilent_user values(6,'孙','N',1,'大专');
- insert into wilent_user values(7,'高','Y',3,'本科');
- insert into wilent_user values(8,'宋','N',3,'高中');
- insert into wilent_user values(9,'伍','Y',3,'大专');
- insert into wilent_user values(10,'欧','Y',4,'本科');
- insert into wilent_user values(11,'庄','N',4,'硕士');
- insert into wilent_user values(12,'纪','Y',4,'本科');
- insert into wilent_user values(13,'陈','Y',5,'大专');
- insert into wilent_user values(14,'龙','N',5,'大专');
- insert into wilent_user values(15,'袁','Y',5,'高中');
- insert into wilent_user values(16,'杨','Y',1,'本科');
- insert into wilent_user values(17,'江','N',1,'大专');
- insert into wilent_user values(18,'刘','Y',1,'硕士');
- insert into wilent_user values(19,'郭','N',3,'硕士');
- insert into wilent_user values(20,'张','Y',3,'大专');
- insert into wilent_user values(21,'文','N',3,'硕士');
- insert into wilent_user values(22,'李','N',4,'大专');
- insert into wilent_user values(23,'梅','Y',4,'本科');
- insert into wilent_user values(24,'王','N',4,'大专');
- insert into wilent_user values(25,'吕','N',5,'高中');
- insert into wilent_user values(26,'范','Y',5,'本科');
- insert into wilent_user values(27,'许','N',1,'大专');
- insert into wilent_user values(28,'墨','Y',1,'高中');
- insert into wilent_user values(29,'孔','N',1,'本科');
- insert into wilent_user values(30,'蔡','Y',1,'大专');
二. oracle 存储过程
- --自定义类型;
- Create Or Replace Type wilent_row_table As Object
- (
- group_name Varchar2(100),
- group_count Number(4),
- male_count Number(4),
- woman_count Number(4),
- da_count Number(4),
- ben_count Number(4)
- );
- /
- --定义一个嵌套表类型;
- Create Or Replace Type wilent_tab_type Is Table Of wilent_row_table;
- /
- --返回一个游标类型;
- Create Or Replace Package wilent_types As
- Type cursor_type Is Ref Cursor;
- End wilent_types;
- /
- Create Or Replace Procedure wilent_group_count(recordSet Out wilent_types.cursor_type)
- As
- v_tab wilent_tab_type := wilent_tab_type();
- index_max Number(4); --wilent_group最大的id;
- index_min Number(4); --wilent_group最小的id;
- index_for Number(4);
- group_name Varchar2(100);
- user_count Number(4);
- male_count Number(4);
- woman_count Number(4);
- da_count Number(4);
- ben_count Number(4);
- Begin
- dbms_output.put_line('as');
- Select Max(g.Id) Into index_max From wilent_group g;
- --dbms_output.put_line(index_max);
- Select Min(g.Id) Into index_min From wilent_group g;
- --dbms_output.put_line(index_min);
- For index_for In Index_min..index_max Loop
- --添加新记录;
- v_tab.Extend;
- Select Name Into group_name From wilent_group Where Id=index_for;
- Select Count(*) Into user_count From wilent_user u, wilent_group g Where u.group_id=g.Id And g.Id=index_for;
- Select Count(*) Into male_count From wilent_user u, wilent_group g Where u.group_id=g.Id And g.Id=index_for And sex='Y';
- Select Count(*) Into woman_count From wilent_user u, wilent_group g Where u.group_id=g.Id And g.Id=index_for And sex='N';
- Select Count(*) Into da_count From wilent_user u, wilent_group g Where u.group_id=g.Id And g.Id=index_for And teach='大专';
- Select Count(*) Into ben_count From wilent_user u, wilent_group g Where u.group_id=g.Id And g.Id=index_for And teach='本科';
- --把记录写入;
- v_tab(v_tab.Last) := wilent_row_table(group_name,user_count,male_count,woman_count,da_count,ben_count);
- End Loop;
- --把记录放在游标里;
- Open recordset For
- --Table(Cast(v_tab As wilent_tab_type))目的是把v_tab强转为wilent_tab_type表
- Select group_name,group_count ,male_count ,woman_count ,da_count ,ben_count From Table(Cast(v_tab As wilent_tab_type)) Order By group_name;
- End wilent_group_count;
- /
- --测试wilent_group_count();
- declare
- recordset wilent_types.cursor_type;
- Begin
- wilent_group_count(recordset);
- End;
三. java代码:
- package com.wilent.oracle;
- import java.sql.CallableStatement;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import oracle.jdbc.driver.OracleTypes;
- import com.wilent.db.ConnectionManager;
- public class TestProcedure {
- public static void main(String[] args) {
- //获得conn连接,读者可以自行写;
- Connection conn = ConnectionManager.getConnection();
- ResultSet rs = null;
- try {
- CallableStatement proc = conn.prepareCall("{call wilent_group_count(?)}");
- proc.registerOutParameter(1, OracleTypes.CURSOR);
- proc.execute();
- rs = (ResultSet) proc.getObject(1);
- System.out.println("组名\t总计\t男性\t女性\t大专\t本科");
- while(rs.next())
- {
- StringBuffer buffer = new StringBuffer();
- buffer.append(rs.getString("group_name"));
- buffer.append("\t");
- buffer.append(rs.getInt("group_count"));
- buffer.append("\t");
- buffer.append(rs.getInt("male_count"));
- buffer.append("\t");
- buffer.append(rs.getInt("woman_count"));
- buffer.append("\t");
- buffer.append(rs.getInt("da_count"));
- buffer.append("\t");
- buffer.append(rs.getInt("ben_count"));
- System.out.println(buffer.toString());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- finally{
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
四. 运行结果
组名 总计 男性 女性 大专 本科 组1 10 6 4 6 2 组2 3 1 2 1 1 组3 6 3 3 2 1 组4 6 3 3 2 3 组5 5 3 2 2 1
java 通过调用存储过程获取结果集的更多相关文章
- jdbc调用存储过程获取多个结果集
jdbc调用存储过程获取多个结果集 2017年07月26日 21:20:22 Kenny-Liu 阅读数:1486 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...
- java程序调用存储过程
java程序调用存储过程 PL/SQL子程序,很多情况下是给应用程序来调用的,所有我们要掌握使用其他编程语言来调用我们写好的存储过程.下面我们介绍下使用java调用Oracle的存储过程. ...
- java程序调用存储过程和存储函数
java程序调用存储过程 jdbcUtil.java文件 package cn.itcast.oracle.utils; import java.sql.Connection; import java ...
- PostgreSQL 调用存储过程返回结果集
创建返回结果集类型的存储过程: CREATE OR REPLACE FUNCTION public.f_get_member_info( id integer, productname charact ...
- Java JDBC调用存储过程:无参、输入带参、输出及输出带参
Java JDBC调用存储过程:无参.输入带参.输出及输出带参 示例代码: package xzg; import java.sql.CallableStatement; import java.sq ...
- Java 调用存储过程 返回结果集
这里使用Oracle数据库的thin连接. 下面是存储过程SQL 1 createorreplaceprocedure proc3(stid in student.stuid%type, stname ...
- Java代码调用存储过程和存储方法
准备一个oracle 的JDBC jar 包:ojdbc14_11g.jar 首先找到你的 oracle 安装位置,例如: 1.创建一个JDBC数据库连接工具类: package com.test.d ...
- java——jdbc调用存储过程
1,加载驱动: 2,获取连接 3,设置参数 4,执行: 5,释放连接 普通jdbc的执行过程: conn.prepareCall() 上面是一个调用存储过程的示例.
- java mybaits 调用存储过程
@Override public BaseResultMessage saveOrderConfirm(String billNo) { BaseResultMessage rm = Utils.re ...
随机推荐
- Linux下C++开发常用命令
本页面记录本人在Linux下进行C++开发时使用的常用命令,注意这里不包括比如ls,mv等linux命令,这里会持续更新.首先假设你只有一个源程序文件,叫vec.cpp,编译后的可执行程序叫vec(本 ...
- Path;Paths和Files;FileVisitor
package filet; import java.io.FileOutputStream; import java.nio.file.FileStore; import java.nio.file ...
- linux 下 vi 编辑器 使用
命令模式(command mode).插入模式(Insert mode)和底行模式(last line mode) 1.进入插入模式 按「i」切换进入插入模式「insert mode」,按“i”进入插 ...
- 各种height/width总结
CSS盒模型是比较复杂的,尤其是当页面中有滚动条时,仅仅通过css来操作高度宽度是不够的,幸运的是Javascript提供了不少这样的接口.Javascript中clientHeight / clie ...
- python中的生成器(一)
我们先考虑一个场景: 有个情景需要循环输出1——10. 这里给两种方法: list1 = [1,2,3,4,5,6,7,8,9,10] for i in list1: print(i) for i i ...
- 使用VNC访问Windows桌面
1. 背景介绍 两台电脑,一个笔记本,一个台式机 笔记本上装的是Windows 10, 通过上网小助手上网 (P.S. 上网小助手...Stupid Policy...) 台式机上装的是Ubuntu ...
- Elasticsearch集群和索引常用命令
https://www.cnblogs.com/pilihaotian/p/5846173.html REST API用途 ES提供了很多全面的API,大致可以分成如下几种: 1 检查集群.节点.索引 ...
- SQL语句的增删改查(详细)
摘录自:http://blog.csdn.net/a88055517/article/details/6736284 一.增:有2种方法 1.使用insert插入单行数据: 语法:insert [in ...
- J2EE的体系架构
J2EE是Java2平台企业版(Java 2 Platform,Enterprise Edition),它的核心是一组技术规范与指南,提供基于组件的方式来设计.开发.组装和部署企业应用.J2EE使用多 ...
- visual studio清理nuget包缓存
最近在使用nuget包的时候发现一个问题.昨天我组长明明发了一个新版本的包上去,可在我电脑上死活找不到这个新版本的包.刷新,重启vs,重启电脑,好长时间才出来.今天又碰到这个问题了,在同事电脑上都能搜 ...