一般在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. C# 文件操作系列一

    在.Net环境中,所有关于文件操作的类都在System.IO命名空间下,注:在修改文件时,安全性显得格外重要,但是本随笔不过多讲述安全性,这里假设我们有足够的权限. 1.管理文件系统 先通过一幅图来了 ...

  2. 在eclipse中启动Tomcat报端口被占用的错误

    安装配置好Tomcat之后,在浏览器中输入localhost,能正取打开页面.然后在eclipse中建立项目,创建Servlet之后,启动Tomcat,报端口被占用的错误.如图: 原因:原来已经启动了 ...

  3. 关于function构造函数特别注意的

    function在javascript中是对象,所以function持有构造函数例子:var a = new Function("x","y","re ...

  4. 定时删除elasticsearch索引

    从去年搭建了日志系统后,就没有去管它了,最近发现大半年各种日志的index也蛮多的,就想着写个脚本定时清理一下,把一些太久的日志清理掉. 脚本思路:通过获取index的尾部时间与我们设定的过期时间进行 ...

  5. C#判断字符串中是否包含一个子字符串是可以直接使用Contains()方法

    1. 以前判断一个字符串中是否包含另一个子字符串时,习惯使用 IndexOf(); string str = "ABC@QQ"; if(str.IndexOf("@&qu ...

  6. 深入理解java集合框架之---------Linked集合 -----构造函数

    linked构造函数 1.LinkedList(): 构造一个空列表的集合 /** * 序列化 */ private static final long serialVersionUID = 1090 ...

  7. forms身份认证仍然能访问html页面解决办法

    asp.net的forms身份认证保护是一个非常棒的东西,用VS2010创建一个Web应用程序即可看到范例 在web.config中配置 <authentication mode="F ...

  8. 【转】Windows 平台下 Go 语言的安装和环境变量设置

    1. Go 语言 SDK 安装包下载和安装 最新稳定版 1.5.3 安装包 go1.5.3.windows-amd64.msi下载地址 https://golang.org/dl/,大小约 69 MB ...

  9. Ubuntu14.04默认cmake升级为3.x

    由于Ubuntu14.04的cmake版本为2.8.x,而如果需要cmake3.x版本时,无法生成makefile,有两种方法可以安装cmake3.4.1: sudo apt-get install ...

  10. 【SSH网上商城项目实战15】线程、定时器同步首页数据(类似于博客定期更新排名)

    转自:https://blog.csdn.net/eson_15/article/details/51387378 上一节我们做完了首页UI界面,但是有个问题:如果我在后台添加了一个商品,那么我必须重 ...