zoukankan      html  css  js  c++  java
  • java 通过调用存储过程获取结果集

            一般在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.   
    9.                 create table wilent_group(  
    10.                     id number(5) primary key,  
    11.                     name varchar2(100)  
    12.                  );  
    13.                  
    14.                         insert into wilent_group values(1,'组1');  
    15.         insert into wilent_group values(2,'组2');  
    16.         insert into wilent_group values(3,'组3');  
    17.         insert into wilent_group values(4,'组4');  
    18.         insert into wilent_group values(5,'组5');  
    19.          
    20.         insert into wilent_user values(1,'吴','Y',1,'大专');  
    21.         insert into wilent_user values(2,'李','Y',1,'大专');  
    22.         insert into wilent_user values(3,'赵','N',2,'本科');  
    23.         insert into wilent_user values(4,'金','Y',2,'高中');  
    24.         insert into wilent_user values(5,'钱','N',2,'大专');  
    25.         insert into wilent_user values(6,'孙','N',1,'大专');  
    26.         insert into wilent_user values(7,'高','Y',3,'本科');  
    27.         insert into wilent_user values(8,'宋','N',3,'高中');  
    28.         insert into wilent_user values(9,'伍','Y',3,'大专');  
    29.         insert into wilent_user values(10,'欧','Y',4,'本科');  
    30.         insert into wilent_user values(11,'庄','N',4,'硕士');  
    31.         insert into wilent_user values(12,'纪','Y',4,'本科');  
    32.         insert into wilent_user values(13,'陈','Y',5,'大专');  
    33.         insert into wilent_user values(14,'龙','N',5,'大专');  
    34.         insert into wilent_user values(15,'袁','Y',5,'高中');  
    35.         insert into wilent_user values(16,'杨','Y',1,'本科');  
    36.         insert into wilent_user values(17,'江','N',1,'大专');  
    37.         insert into wilent_user values(18,'刘','Y',1,'硕士');  
    38.         insert into wilent_user values(19,'郭','N',3,'硕士');  
    39.         insert into wilent_user values(20,'张','Y',3,'大专');  
    40.         insert into wilent_user values(21,'文','N',3,'硕士');  
    41.         insert into wilent_user values(22,'李','N',4,'大专');  
    42.         insert into wilent_user values(23,'梅','Y',4,'本科');  
    43.         insert into wilent_user values(24,'王','N',4,'大专');  
    44.         insert into wilent_user values(25,'吕','N',5,'高中');  
    45.         insert into wilent_user values(26,'范','Y',5,'本科');  
    46.         insert into wilent_user values(27,'许','N',1,'大专');  
    47.         insert into wilent_user values(28,'墨','Y',1,'高中');  
    48.         insert into wilent_user values(29,'孔','N',1,'本科');  
    49.         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. --定义一个嵌套表类型;
    14. Create Or Replace Type wilent_tab_type Is Table Of wilent_row_table;  
    15. /  
    16. --返回一个游标类型;  
    17. Create Or Replace Package wilent_types As   
    18.        Type cursor_type Is Ref Cursor;  
    19. End wilent_types;  
    20. /  
    21. Create Or Replace Procedure wilent_group_count(recordSet Out wilent_types.cursor_type)  
    22. As  
    23.   v_tab wilent_tab_type := wilent_tab_type();               
    24.   index_max Number(4);                         --wilent_group最大的id;  
    25.   index_min Number(4);                         --wilent_group最小的id;  
    26.   index_for Number(4);  
    27.     
    28.   group_name Varchar2(100);  
    29.   user_count Number(4);  
    30.   male_count Number(4);  
    31.   woman_count Number(4);  
    32.   da_count Number(4);  
    33.   ben_count Number(4);  
    34. Begin  
    35.      dbms_output.put_line('as');  
    36.      Select Max(g.Id) Into index_max From wilent_group g;  
    37.      --dbms_output.put_line(index_max);  
    38.      Select Min(g.Id) Into index_min From wilent_group g;  
    39.      --dbms_output.put_line(index_min);  
    40.      For index_for In Index_min..index_max Loop  
    41.          --添加新记录;  
    42.          v_tab.Extend;  
    43.          Select Name Into group_name From wilent_group Where Id=index_for;  
    44.          Select Count(*) Into user_count From wilent_user u, wilent_group g Where u.group_id=g.Id And g.Id=index_for;  
    45.          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';  
    46.          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';  
    47.          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='大专';  
    48.          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='本科';  
    49.          --把记录写入;  
    50.          v_tab(v_tab.Last) := wilent_row_table(group_name,user_count,male_count,woman_count,da_count,ben_count);  
    51.      End Loop;  
    52.        
    53.      --把记录放在游标里;  
    54.      Open recordset For  
    55.       --Table(Cast(v_tab As wilent_tab_type))目的是把v_tab强转为wilent_tab_type表 
    56.           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;  
    57. End wilent_group_count;  
    58. /  
    59.   
    60. --测试wilent_group_count();  
    61. declare  
    62.   recordset wilent_types.cursor_type;  
    63. Begin  
    64.     wilent_group_count(recordset);  
    65. End;  

                    三. java代码:           

    java 代码
     
    1. package com.wilent.oracle;  
    2.   
    3. import java.sql.CallableStatement;  
    4. import java.sql.Connection;  
    5. import java.sql.ResultSet;  
    6. import java.sql.SQLException;  
    7.   
    8. import oracle.jdbc.driver.OracleTypes;  
    9.   
    10. import com.wilent.db.ConnectionManager;  
    11.   
    12. public class TestProcedure {  
    13.     public static void main(String[] args) {  
    14.         //获得conn连接,读者可以自行写;  
    15.         Connection conn = ConnectionManager.getConnection();  
    16.         ResultSet rs = null;  
    17.         try {  
    18.             CallableStatement proc = conn.prepareCall("{call wilent_group_count(?)}");  
    19.             proc.registerOutParameter(1, OracleTypes.CURSOR);  
    20.             proc.execute();  
    21.               
    22.             rs = (ResultSet) proc.getObject(1);  
    23.             System.out.println("组名 总计 男性 女性 大专 本科");  
    24.             while(rs.next())  
    25.             {  
    26.                 StringBuffer buffer = new StringBuffer();  
    27.                 buffer.append(rs.getString("group_name"));  
    28.                 buffer.append(" ");  
    29.                 buffer.append(rs.getInt("group_count"));  
    30.                 buffer.append(" ");  
    31.                 buffer.append(rs.getInt("male_count"));  
    32.                 buffer.append(" ");  
    33.                 buffer.append(rs.getInt("woman_count"));  
    34.                 buffer.append(" ");  
    35.                 buffer.append(rs.getInt("da_count"));  
    36.                 buffer.append(" ");  
    37.                 buffer.append(rs.getInt("ben_count"));  
    38.                 System.out.println(buffer.toString());  
    39.             }  
    40.         } catch (Exception e) {  
    41.             e.printStackTrace();  
    42.         }  
    43.         finally{  
    44.             try {  
    45.                 conn.close();  
    46.             } catch (SQLException e) {  
    47.                 e.printStackTrace();  
    48.             }  
    49.         }  
    50.     }  
        四. 运行结果
        组名    总计    男性    女性    大专    本科     组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
  • 相关阅读:
    字符数组+数组复习
    C语言博客作业05-指针
    C语言博客作业04 数组
    C语言博客作业03 函数
    Java与C# socket通信
    JDBC复制数据库(sqlite)
    mysql Connector/net不能更新或删除(转载)
    MATLAB回归、插值、逼近、拟合【转载】
    前端请求RestController发送数据方法汇总
    elementUI el-input 输入框 设置高度和宽度
  • 原文地址:https://www.cnblogs.com/firstdream/p/8652002.html
Copyright © 2011-2022 走看看