一般在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. kafka多线程消费topic的问题

    案例: topic:my-topic,分区:6 消费者:部署三台机器,每台机器上面开启6个线程消费. 消费结果:只有一台机器可以正常消费,另外两台机器直接输出六条告警日志: No broker par ...

  2. php 使用 rabbitmq

    1,配置好rabbitmq 服务器 (参照 http://www.cnblogs.com/spicy/p/7017603.html)(我是linux) 2,新增了一个用户 并点击该用户 增加权限如下

  3. 爱奇艺视频显示列表CSS实现

    css: body{margin:0;font-size: 12px;font-family: "宋体":} ul{margin:0;padding:0;list-style: n ...

  4. JAVA练手--异常

    1. 基本的 public static void main(String[] args) { //1. try catch基本用法 { try{ int[] intA = new int[2]; i ...

  5. TP2.1 加载扩展配置文件参数

    维护老项目真的恶心!!!!!!!!! TP3.2好像有这样一个配置参数,可以设置有那些扩展配置文件,系统会自动加载. 方法一: 'LOAD_EXT_CONFIG' => 'user,db',// ...

  6. 编译android源码遇到错误及其解决方法

    升级ubuntu的14.04后,android的源码又编译错误了,一下是错误说明赫解决方法: 1.make: *** [out/host/linux-x86/obj/EXECUTABLES/aidl_ ...

  7. SmartGit破解使用的个人方法

    转自:https://www.cnblogs.com/nn839155963/p/5912788.html SmartGit是收费的,可以30天的试用期,30天试用期过后,smartgit 需要输入序 ...

  8. WPF中ScrollViewer嵌套引发滚动失灵的Bug

    事情起因 测试报告说存在滚动条不能拖动的情况,我们几个开发人员多次测试都未重现该问题.后面发现是操作系统的问题,在XP和部分Win7上会存在该问题.而在我们开发人员的机器上,包括Win7 SP1,Wi ...

  9. The configuration section 'system.serviceModel' cannot be read because it is missing a section decla

    将Asp.Net 2.0的Web Site搭建在IIS7(7.5)上时,运行出现500.19错误, 错误提示为 The configuration section 'system.serviceMod ...

  10. 关于mysql的 sql_mode=only_full_group_by 报错

    在mysql中执行 : SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY','')); 官网:https://dev ...