Chinaunix首页 | 论坛 | 博客
  • 博客访问: 167148
  • 博文数量: 16
  • 博客积分: 473
  • 博客等级: 二等列兵
  • 技术积分: 152
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-07 12:58
文章分类

全部博文(16)

文章存档

2012年(8)

2011年(8)

分类: Java

2011-12-19 19:41:47

转载来自:http://blog.sina.com.cn/s/blog_5591786b0100ath6.html

  1. -----------------无返回值示例------------------------

  2. 存储过程:

  3. ·添加学生,如果班级不存在,则先添加班级信息,再添加学生。

  4. create or replace procedure sp_add_stu
  5. (
  6.   p_stu_id t_stu.s_id%type, --参数类型定义为字段类型
  7.   p_s_name t_stu.s_name%type,
  8.   p_c_id t_class.c_id%type
  9. )
  10. as
  11.   num number;
  12.   new_name t_stu.s_name%type;
  13. begin
  14.   --处理姓名(如果不从查询结果取值,用 := )
  15.   new_name := substr(trim(p_s_name),1,8);
  16.  
  17.   --将查询结果保存到变量,只能用select into
  18.    select count(*) into num from t_class c where c.c_id=p_c_id;
  19.    if(num=0) then
  20.        insert into t_class(c_id,c_name) values(p_c_id,'新班');
  21.     end if;
  22.    
  23.   insert into t_stu(s_id,s_name,c_id,s_***)
  24.   values(p_stu_id,new_name,p_c_id,'1');
  25.    
  26.   commit;
  27.  
  28.   dbms_output.put_line('执行完毕');
  29. end;

  30.  -----------------在Oracle中调用(无返回值)-----------------

  31. SQL> call sp_add_stu(800,'李小龙',88);

  32. Method called

  33. SQL>

  34.  

  35. -----------------JDBC调用(无返回值)-----------------

  36. Connection conn = null;

  37. CallableStatement cstmt = null;

  38. conn = DriverManager.getConnection(url, user, password);

  39. String procedure = "{call sp_add_stu(?,?,?)}";

  40. cstmt = conn.prepareCall(procedure);

  41. cstmt.setString(1, "2000");

  42. cstmt.setString(2, "张三);

  43. cstmt.setString(3, "40");

  44. cstmt.executeUpdate();

  45. -----------------返回结果值示例------------------------

  46. ·指定参数为out类型即可返回值


  47. create or replace procedure sp_value(
  48. id1 in number,
  49. id2 out number
  50. )
  51. as
  52. begin
  53.   id2 := id1*200;
  54. end;

  55.  

  56. -----------------调用(有简单返回值)----------------

  57. Connection conn = null;

  58. CallableStatement cstmt = null;

  59. conn = DriverManager.getConnection(url, user, password);

  60. String procedure = "{call sp_value(?,?)}";

  61. cstmt = conn.prepareCall(procedure);

  62. --------------------JDBC获取存储过程中的值-------------------

  63. //学号、姓名、班级编号

  64. cstmt.setString(1, "2000");

  65. //注册输出参数

  66. cstmt.registerOutParameter(2, java.sql.Types.INTEGER);

  67. cstmt.executeUpdate();

  68. //从输出参数中获取值

  69. int value = cstmt.getInt(2);

  70. System.out.println("返回: "+value);

  71. -----------------返回结果集示例------------------------

  72. ·建包,包中定义游标类型变量

  73. CREATE OR REPLACE PACKAGE TESTPACKAGE AS

  74.     TYPE Test_CURSOR IS REF CURSOR;

  75. end;

  76. ·存储过程返回指定游标类型变量

  77. CREATE OR REPLACE PROCEDURE sp_select_stu

  78. (

  79. p_c_id t_class.c_id%type,

  80. p_cursor out TESTPACKAGE.Test_CURSOR

  81. ) IS

  82. BEGIN

  83. OPEN p_CURSOR FOR

  84.    SELECT s.stu_id,s.s_name,to_char(s.s_birthday,'yy.mm') FROM t_stu s

  85.    where s.c_id=p_c_id

  86.    order by s.s_name;

  87. END;

  88.  
  89. -----------------JDBC获取存储过程中的结果集---------------

  90. Connection conn = null;

  91. CallableStatement cstmt = null;

  92. conn = DriverManager.getConnection(url, user, password);

  93. String procedure = "{call sp_select_stu(?,?)}";

  94. cstmt = conn.prepareCall(procedure);

  95. //学号、姓名、班级编号

  96. cstmt.setString(1, "C01");

  97. //注册输出参数

  98. cstmt.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);

  99. cstmt.execute();

  100. //从输出参数中获取值

  101. ResultSet rs = (ResultSet)cstmt.getObject(2);//第2个?所代表的参数

  102. ResultSetMetaData rmd =rs.getMetaData();

  103. while(rs.next()){

  104. for(int i=0;i

  105.   System.out.print(rs.getString(i+1)+" ");

  106. }
  107. System.out.println("");

  108. }

  109. rs.close();
阅读(5619) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~