一般在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. Idea查看代码相关技巧

    (1)查看类中的属性与方法快捷键  Alt+7 (2)查看方法被调用 在方法上右键find usages (3)查看方法说明  Ctrl+Q

  2. Hibernate5.1+Sqlserver2000分页查询

    前几天改到一个bug:从MS SQLserver上面同步表结构并且采集数据写入其他库.然后用的核心技术是用的Hibernate. 其中bug出在SQLServer2000版本上.排查下来发现2000版 ...

  3. 介绍Windows Azure HDInsight服务的Hadoop Storm的视频

    介绍Windows Azure HDInsight服务的Hadoop Storm的原理,用例及开发入门的视频,收藏一下: http://channel9.msdn.com/Shows/Data-Exp ...

  4. System.Web.Caching.Cache类 缓存 各种缓存依赖(转)

    转自:http://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntime ...

  5. google运维解密

    1.运维团队与开发团队的矛盾: 运维追求业务的稳定.开发更关注新功能的添加与版本的快速迭代.但是由于业务更新,有很大可能导致故障.从本质上来说,两部门是矛盾的. deops应该是: 1.对重复性工作有 ...

  6. C# 扩展方法一

    1. 何为扩展方法 扩展方法是C#3.0引入的语法特性,是一种特殊的静态方法.它使得我们能向现有的数据类型“动态”添加方法,而不需要创建行的派生类型.重新编译或直接修改原始类型的源代码. 注意扩展方法 ...

  7. 制作一个控制台小程序,要求:用户可以在控制到录入学生的姓名,当用户输入quit(不区分大小写)时,程序停止接收用户输入,并且显示出学生个数及姓名

    string name = string.Empty; //定义一个集合来接收学生 List<string> my = new List<string>(); do { Con ...

  8. HTML页面格式

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. 记一次C#面试

    最近参加了工作后的第一次面试,虽然最终没谈成,但是收获还是不少,不管是技术还是面试经验还是得多多积累呀. 这一次面试与在学校时候参加过的面试区别还是挺大的.校园招聘的面试问的问题似乎都比较具体,直接针 ...

  10. 三、hive JavaAPI示例

    在上文中https://www.cnblogs.com/lay2017/p/9973370.html 我们通过hive shell去操作hive,本文我们以Java代码的示例去对hive执行加载数据和 ...