一般在java中,数据查询是通过Statement, PreparedStatement获取结果集,今天向大家介绍通过CallableStatement调用存储过程,从而获取结果集.        本文是所用的数据库为oracle.       一.  测试数据库表:

sql 代码
  1. create table wilent_user(
  2. id number(5) primary key,
  3. name varchar2(100),
  4. sex varchar2(1),    --Y为男,F为女;
  5. group_id number(5),
  6. teach varchar2(50)    --学历;
  7. );
  8. create table wilent_group(
  9. id number(5) primary key,
  10. name varchar2(100)
  11. );
  12. insert into wilent_group values(1,'组1');
  13. insert into wilent_group values(2,'组2');
  14. insert into wilent_group values(3,'组3');
  15. insert into wilent_group values(4,'组4');
  16. insert into wilent_group values(5,'组5');
  17. insert into wilent_user values(1,'吴','Y',1,'大专');
  18. insert into wilent_user values(2,'李','Y',1,'大专');
  19. insert into wilent_user values(3,'赵','N',2,'本科');
  20. insert into wilent_user values(4,'金','Y',2,'高中');
  21. insert into wilent_user values(5,'钱','N',2,'大专');
  22. insert into wilent_user values(6,'孙','N',1,'大专');
  23. insert into wilent_user values(7,'高','Y',3,'本科');
  24. insert into wilent_user values(8,'宋','N',3,'高中');
  25. insert into wilent_user values(9,'伍','Y',3,'大专');
  26. insert into wilent_user values(10,'欧','Y',4,'本科');
  27. insert into wilent_user values(11,'庄','N',4,'硕士');
  28. insert into wilent_user values(12,'纪','Y',4,'本科');
  29. insert into wilent_user values(13,'陈','Y',5,'大专');
  30. insert into wilent_user values(14,'龙','N',5,'大专');
  31. insert into wilent_user values(15,'袁','Y',5,'高中');
  32. insert into wilent_user values(16,'杨','Y',1,'本科');
  33. insert into wilent_user values(17,'江','N',1,'大专');
  34. insert into wilent_user values(18,'刘','Y',1,'硕士');
  35. insert into wilent_user values(19,'郭','N',3,'硕士');
  36. insert into wilent_user values(20,'张','Y',3,'大专');
  37. insert into wilent_user values(21,'文','N',3,'硕士');
  38. insert into wilent_user values(22,'李','N',4,'大专');
  39. insert into wilent_user values(23,'梅','Y',4,'本科');
  40. insert into wilent_user values(24,'王','N',4,'大专');
  41. insert into wilent_user values(25,'吕','N',5,'高中');
  42. insert into wilent_user values(26,'范','Y',5,'本科');
  43. insert into wilent_user values(27,'许','N',1,'大专');
  44. insert into wilent_user values(28,'墨','Y',1,'高中');
  45. insert into wilent_user values(29,'孔','N',1,'本科');
  46. insert into wilent_user values(30,'蔡','Y',1,'大专');

二.  oracle 存储过程

sql 代码
  1. --自定义类型;
  2. Create Or Replace Type wilent_row_table As Object
  3. (
  4. group_name Varchar2(100),
  5. group_count Number(4),
  6. male_count Number(4),
  7. woman_count Number(4),
  8. da_count Number(4),
  9. ben_count Number(4)
  10. );
  11. /
  12. --定义一个嵌套表类型;
  13. Create Or Replace Type wilent_tab_type Is Table Of wilent_row_table;
  14. /
  15. --返回一个游标类型;
  16. Create Or Replace Package wilent_types As
  17. Type cursor_type Is Ref Cursor;
  18. End wilent_types;
  19. /
  20. Create Or Replace Procedure wilent_group_count(recordSet Out wilent_types.cursor_type)
  21. As
  22. v_tab wilent_tab_type := wilent_tab_type();
  23. index_max Number(4);                         --wilent_group最大的id;
  24. index_min Number(4);                         --wilent_group最小的id;
  25. index_for Number(4);
  26. group_name Varchar2(100);
  27. user_count Number(4);
  28. male_count Number(4);
  29. woman_count Number(4);
  30. da_count Number(4);
  31. ben_count Number(4);
  32. Begin
  33. dbms_output.put_line('as');
  34. Select Max(g.Id) Into index_max From wilent_group g;
  35. --dbms_output.put_line(index_max);
  36. Select Min(g.Id) Into index_min From wilent_group g;
  37. --dbms_output.put_line(index_min);
  38. For index_for In Index_min..index_max Loop
  39. --添加新记录;
  40. v_tab.Extend;
  41. Select Name Into group_name From wilent_group Where Id=index_for;
  42. Select Count(*) Into user_count From wilent_user u, wilent_group g Where u.group_id=g.Id And g.Id=index_for;
  43. 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';
  44. 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';
  45. 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='大专';
  46. 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='本科';
  47. --把记录写入;
  48. v_tab(v_tab.Last) := wilent_row_table(group_name,user_count,male_count,woman_count,da_count,ben_count);
  49. End Loop;
  50. --把记录放在游标里;
  51. Open recordset For
  52. --Table(Cast(v_tab As wilent_tab_type))目的是把v_tab强转为wilent_tab_type表
  53. 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;
  54. End wilent_group_count;
  55. /
  56. --测试wilent_group_count();
  57. declare
  58. recordset wilent_types.cursor_type;
  59. Begin
  60. wilent_group_count(recordset);
  61. End;

三. java代码:

java 代码
  1. package com.wilent.oracle;
  2. import java.sql.CallableStatement;
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import oracle.jdbc.driver.OracleTypes;
  7. import com.wilent.db.ConnectionManager;
  8. public class TestProcedure {
  9. public static void main(String[] args) {
  10. //获得conn连接,读者可以自行写;
  11. Connection conn = ConnectionManager.getConnection();
  12. ResultSet rs = null;
  13. try {
  14. CallableStatement proc = conn.prepareCall("{call wilent_group_count(?)}");
  15. proc.registerOutParameter(1, OracleTypes.CURSOR);
  16. proc.execute();
  17. rs = (ResultSet) proc.getObject(1);
  18. System.out.println("组名\t总计\t男性\t女性\t大专\t本科");
  19. while(rs.next())
  20. {
  21. StringBuffer buffer = new StringBuffer();
  22. buffer.append(rs.getString("group_name"));
  23. buffer.append("\t");
  24. buffer.append(rs.getInt("group_count"));
  25. buffer.append("\t");
  26. buffer.append(rs.getInt("male_count"));
  27. buffer.append("\t");
  28. buffer.append(rs.getInt("woman_count"));
  29. buffer.append("\t");
  30. buffer.append(rs.getInt("da_count"));
  31. buffer.append("\t");
  32. buffer.append(rs.getInt("ben_count"));
  33. System.out.println(buffer.toString());
  34. }
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. finally{
  39. try {
  40. conn.close();
  41. } catch (SQLException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. }
  46. }

四. 运行结果
    组名    总计    男性    女性    大专    本科     组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 通过调用存储过程获取结果集的更多相关文章

  1. jdbc调用存储过程获取多个结果集

    jdbc调用存储过程获取多个结果集 2017年07月26日 21:20:22 Kenny-Liu 阅读数:1486 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...

  2. java程序调用存储过程

    java程序调用存储过程       PL/SQL子程序,很多情况下是给应用程序来调用的,所有我们要掌握使用其他编程语言来调用我们写好的存储过程.下面我们介绍下使用java调用Oracle的存储过程. ...

  3. java程序调用存储过程和存储函数

    java程序调用存储过程 jdbcUtil.java文件 package cn.itcast.oracle.utils; import java.sql.Connection; import java ...

  4. PostgreSQL 调用存储过程返回结果集

    创建返回结果集类型的存储过程: CREATE OR REPLACE FUNCTION public.f_get_member_info( id integer, productname charact ...

  5. Java JDBC调用存储过程:无参、输入带参、输出及输出带参

    Java JDBC调用存储过程:无参.输入带参.输出及输出带参 示例代码: package xzg; import java.sql.CallableStatement; import java.sq ...

  6. Java 调用存储过程 返回结果集

    这里使用Oracle数据库的thin连接. 下面是存储过程SQL 1 createorreplaceprocedure proc3(stid in student.stuid%type, stname ...

  7. Java代码调用存储过程和存储方法

    准备一个oracle 的JDBC jar 包:ojdbc14_11g.jar 首先找到你的 oracle 安装位置,例如: 1.创建一个JDBC数据库连接工具类: package com.test.d ...

  8. java——jdbc调用存储过程

    1,加载驱动: 2,获取连接 3,设置参数 4,执行: 5,释放连接 普通jdbc的执行过程: conn.prepareCall() 上面是一个调用存储过程的示例.

  9. java mybaits 调用存储过程

    @Override public BaseResultMessage saveOrderConfirm(String billNo) { BaseResultMessage rm = Utils.re ...

随机推荐

  1. javascript中操作元素属性

    1. setAttribute():设置属性的值: getAttribute():得到属性的值: removeAttribute():移除属性: 2.offsetWidth:offsetWidth = ...

  2. python-pymongo使用

    #-*- coding: utf-8 -*- #python2.7x from pymongo import MongoClient def get_db(): #建立连接 client = Mong ...

  3. CSS动态控制DIV居中

    1.所谓的动态:就是即使手动去拖拉浏览器,DIV还是会自动居中 2.之前一直以为这个事情是JavaScript做的, 步骤:通过先获取页面的Height和Width, 然后定义DIV的Height和W ...

  4. Java线程池及其底层源码实现分析

    1.相关类 Executors  ExecutorService   Callable   ThreadPool     Future 2.相关接口 Executor Executor接口的使用: p ...

  5. window.location和document.location的区别分析

    用户不能改变document.location(因为这是当前显示文档的位置).但是,可以改变window.location (用其它文档取代当前文档)window.location本身也是一个对象,而 ...

  6. Kernel的IIC驱动分析

    涉及到的文件: drivers/i2c/i2c-core.c drivers/i2c/i2c-dev.c drivers/i2c/busses/i2c-imx.c 等等 在下面分析的代码中,不想关或者 ...

  7. Java jxl导入excel文件,导入的数字、身份证号码、手机号变成了科学计数法,解决方案

    原文出自:https://blog.csdn.net/seesun2012 这是一个execl文件导入数据库操作,使用jxl解析execl导入数据库过程出现了科学计数法,与想要导入的数据不匹配,以下是 ...

  8. WinForm-SuspendLayout、ResumeLayout、PerformLayou——转载

    WinForm-SuspendLayout.ResumeLayout.PerformLayou——转载 https://www.cnblogs.com/si-shaohua/archive/2011/ ...

  9. PL/SQL之游标的使用

    Oracle中的游标有两种: 显式游标 用CURSOR...IS 命令定义的游标,它可以对查询语句(SELECT)返回的多条记录进行处理. 隐式游标 是在执行插入(INSERT).删除(DELETE) ...

  10. 使用MUI框架,模拟手机端的下拉刷新,上拉加载操作。

    套用mui官方文档的一句话:“开发者只需关心业务逻辑,实现加载更多数据即可”.真的是不错的框架. 想更多的了解这个框架:http://dev.dcloud.net.cn/mui/ 那么如何实现下拉刷新 ...