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 ...
随机推荐
- javascript中操作元素属性
1. setAttribute():设置属性的值: getAttribute():得到属性的值: removeAttribute():移除属性: 2.offsetWidth:offsetWidth = ...
- python-pymongo使用
#-*- coding: utf-8 -*- #python2.7x from pymongo import MongoClient def get_db(): #建立连接 client = Mong ...
- CSS动态控制DIV居中
1.所谓的动态:就是即使手动去拖拉浏览器,DIV还是会自动居中 2.之前一直以为这个事情是JavaScript做的, 步骤:通过先获取页面的Height和Width, 然后定义DIV的Height和W ...
- Java线程池及其底层源码实现分析
1.相关类 Executors ExecutorService Callable ThreadPool Future 2.相关接口 Executor Executor接口的使用: p ...
- window.location和document.location的区别分析
用户不能改变document.location(因为这是当前显示文档的位置).但是,可以改变window.location (用其它文档取代当前文档)window.location本身也是一个对象,而 ...
- Kernel的IIC驱动分析
涉及到的文件: drivers/i2c/i2c-core.c drivers/i2c/i2c-dev.c drivers/i2c/busses/i2c-imx.c 等等 在下面分析的代码中,不想关或者 ...
- Java jxl导入excel文件,导入的数字、身份证号码、手机号变成了科学计数法,解决方案
原文出自:https://blog.csdn.net/seesun2012 这是一个execl文件导入数据库操作,使用jxl解析execl导入数据库过程出现了科学计数法,与想要导入的数据不匹配,以下是 ...
- WinForm-SuspendLayout、ResumeLayout、PerformLayou——转载
WinForm-SuspendLayout.ResumeLayout.PerformLayou——转载 https://www.cnblogs.com/si-shaohua/archive/2011/ ...
- PL/SQL之游标的使用
Oracle中的游标有两种: 显式游标 用CURSOR...IS 命令定义的游标,它可以对查询语句(SELECT)返回的多条记录进行处理. 隐式游标 是在执行插入(INSERT).删除(DELETE) ...
- 使用MUI框架,模拟手机端的下拉刷新,上拉加载操作。
套用mui官方文档的一句话:“开发者只需关心业务逻辑,实现加载更多数据即可”.真的是不错的框架. 想更多的了解这个框架:http://dev.dcloud.net.cn/mui/ 那么如何实现下拉刷新 ...