一、任务提出

JAVA操作oracle11g存储过程实验需要完成以下几个实例:

1.调用没有返回参数的过程(插入记录、更新记录)

2.有返回参数的过程

3.返回列表的过程

4.返回带分页的列表的过程。

二、建立表和相应的存储过程

create table student (sno int ,sname varchar2(20),sage int);

  1. --创建存储过程testa1
  2. create or replace procedure testa1(para1 in int,para2 in varchar2,para3 in int)
  3. is
  4. begin
  5. insert into student(sno,sname,sage) values(para1,para2,para3);
  6. commit;
  7. end;
  8. /
  9. --创建存储过程testa2
  10. create or replace procedure testa2(para1 in int,para2 in int)
  11. is
  12. begin
  13. update student set sage=para2 where sno=para1;
  14. commit;
  15. end;
  16. /
  17. --创建有返回参数的存储过程testb
  18. create or replace procedure testb(para1 in int ,para2 out varchar2,para3 out int)
  19. is
  20. begin
  21. select sname,sage into para2,para3 from student where sno=para1;
  22. end;
  23. --创建返回集合的存储过程:
  24. --在oracle中,如果要返回集合必须是返回游标,不能是一张二维表。所以,要先建立包。
  25. create or replace package testpack
  26. is
  27. type test_cursor is ref cursor;
  28. end testpack;
  29. /
  30.  
  31. create or replace procedure testc(p_cursor out testpack.test_cursor)
  32. is
  33. begin
  34. open p_cursor for select * from student;
  35. end;
  36. /
  1. --实现分页的存储过程
  2. ---ps 每页几个记录,cs第几页
  3. create or replace procedure testd(ps int ,cs int ,p_cursor out testpack.test_cursor)
  4. is
  5. begin
  6. open p_cursor for
  7. select * from (select student.*,rownum rn from student) where rn>ps*(cs-1) and rn<=ps*cs;
  8. end;
  9. /

三、java操作调用上述存储过程

  1. package com.oaj;
  2. import java.sql.*;
  3.  
  4. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  5.  
  6. public class Test {
  7. String driver="oracle.jdbc.driver.OracleDriver";
  8. String strUrl="jdbc:oracle:thin:@localhost:1521:orcl";
  9. ResultSet rs=null;
  10. Connection conn=null;
  11.  
  12. CallableStatement cstmt=null;
  13.  
  14. public static void main(String[] args)
  15. {
  16. new Test().testPageSet(3,1);
  17. }
  18. public void testPageSet(int recordPerPage,int currentPage)
  19. {
  20. try
  21. {
  22. Class.forName(driver);
  23. conn=DriverManager.getConnection(strUrl,"scott","scott");
  24. cstmt=conn.prepareCall("{ call scott.testd(?,?,?)}");
  25. cstmt.registerOutParameter(3, oracle.jdbc.OracleTypes.CURSOR);//指定是oracle里规定的类型
  26. cstmt.setInt(1,recordPerPage);
  27. cstmt.setInt(2, currentPage);
  28. cstmt.execute();
  29. rs=(ResultSet)cstmt.getObject(3);
  30.  
  31. while(rs.next())
  32. {
  33. System.out.print("学号是:"+rs.getInt(1)+"的学生的名字是:"+rs.getString(2)+",年龄是:"+rs.getInt(3)+"\r\n");
  34. }
  35. }
  36. catch(SQLException ex)
  37. {
  38. ex.printStackTrace();
  39. }
  40. catch(Exception ex)
  41. {
  42. ex.printStackTrace();
  43.  
  44. }
  45. finally
  46. {
  47. try
  48. {
  49. if(cstmt!=null)
  50. cstmt.close();
  51. if(conn!=null)
  52. {
  53. conn.close();
  54. conn=null;
  55. }
  56.  
  57. }
  58. catch(SQLException ex)
  59. {
  60. ex.printStackTrace();
  61. }
  62. }
  63. }
  64. public void testOutResult()
  65. {
  66. try
  67. {
  68. Class.forName(driver);
  69. conn=DriverManager.getConnection(strUrl,"scott","scott");
  70. cstmt=conn.prepareCall("{ call scott.testc(?)}");
  71. cstmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);//指定是oracle里规定的类型
  72. cstmt.execute();
  73. rs=(ResultSet)cstmt.getObject(1);
  74.  
  75. while(rs.next())
  76. {
  77. System.out.print("学号是:"+rs.getInt(1)+"的学生的名字是:"+rs.getString(2)+",年龄是:"+rs.getInt(3)+"\r\n");
  78. }
  79. }
  80. catch(SQLException ex)
  81. {
  82. ex.printStackTrace();
  83. }
  84. catch(Exception ex)
  85. {
  86. ex.printStackTrace();
  87.  
  88. }
  89. finally
  90. {
  91. try
  92. {
  93. if(cstmt!=null)
  94. cstmt.close();
  95. if(conn!=null)
  96. {
  97. conn.close();
  98. conn=null;
  99. }
  100.  
  101. }
  102. catch(SQLException ex)
  103. {
  104. ex.printStackTrace();
  105. }
  106. }
  107. }
  108. public void testOutParameter(int inputSno)
  109. {
  110. try
  111. {
  112. Class.forName(driver);
  113. conn=DriverManager.getConnection(strUrl,"scott","scott");
  114. cstmt=conn.prepareCall("{ call scott.testb(?,?,?)}");
  115. cstmt.setInt(1, inputSno);
  116. cstmt.registerOutParameter(2, Types.VARCHAR);
  117. cstmt.registerOutParameter(3, Types.INTEGER);
  118.  
  119. cstmt.execute();
  120. String name=cstmt.getString(2);
  121. int age=cstmt.getInt(3);
  122.  
  123. System.out.print("学号是:"+inputSno+"的学生的名字是:"+name+",年龄是:"+age);
  124. }
  125. catch(SQLException ex)
  126. {
  127. ex.printStackTrace();
  128. }
  129. catch(Exception ex)
  130. {
  131. ex.printStackTrace();
  132.  
  133. }
  134. finally
  135. {
  136. try
  137. {
  138. if(cstmt!=null)
  139. cstmt.close();
  140. if(conn!=null)
  141. {
  142. conn.close();
  143. conn=null;
  144. }
  145.  
  146. }
  147. catch(SQLException ex)
  148. {
  149. ex.printStackTrace();
  150. }
  151. }
  152. }
  153. public void testNoOutParameterUpdate(int inputeSno,int inputSage)
  154. {
  155. try
  156. {
  157. Class.forName(driver);
  158. conn=DriverManager.getConnection(strUrl,"scott","scott");
  159. cstmt=conn.prepareCall("{ call scott.testa2(?,?)}");
  160. cstmt.setInt(1, inputeSno);
  161. cstmt.setInt(2, inputSage);
  162. cstmt.execute();
  163. System.out.println("执行成功!");
  164. }
  165. catch(SQLException ex)
  166. {
  167. ex.printStackTrace();
  168. }
  169. catch(Exception ex)
  170. {
  171. ex.printStackTrace();
  172.  
  173. }
  174. finally
  175. {
  176. try
  177. {
  178. if(cstmt!=null)
  179. cstmt.close();
  180. if(conn!=null)
  181. {
  182. conn.close();
  183. conn=null;
  184. }
  185.  
  186. }
  187. catch(SQLException ex)
  188. {
  189. ex.printStackTrace();
  190. }
  191. }
  192. }
  193. public void testNoOutParameterInsert(int a,String b,int c)
  194. {
  195. try
  196. {
  197. Class.forName(driver);
  198. conn=DriverManager.getConnection(strUrl,"scott","scott");
  199. cstmt=conn.prepareCall("{ call scott.testa1(?,?,?)}");
  200. cstmt.setInt(1, a);
  201. cstmt.setString(2, b);
  202. cstmt.setInt(3, c);
  203. cstmt.execute();
  204. }
  205. catch(SQLException ex)
  206. {
  207. ex.printStackTrace();
  208. }
  209. catch(Exception ex)
  210. {
  211. ex.printStackTrace();
  212.  
  213. }
  214. finally
  215. {
  216. try
  217. {
  218. if(cstmt!=null)
  219. cstmt.close();
  220. if(conn!=null)
  221. {
  222. conn.close();
  223. conn=null;
  224. }
  225.  
  226. }
  227. catch(SQLException ex)
  228. {
  229. ex.printStackTrace();
  230. }
  231. }
  232. }
  233. }

JAVA操作ORACLE数据库的存储过程的更多相关文章

  1. Java操作Oracle数据库以及调用存储过程

    操作Oracle数据库 publicclass DBConnection {     //jdbc:oracle:thin:@localhost:1521:orcl     publicstaticf ...

  2. Java 操作Oracle数据库

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...

  3. JAVA操作Oracle数据库中的事务

    实验1: create table yggz(code int, salary number(7,2)); insert into yggz values(1, 1000); insert into ...

  4. Java java jdbc thin远程连接并操作Oracle数据库

    JAVA jdbc thin远程连接并操作Oracle数据库 by:授客 QQ:1033553122 测试环境 数据库:linux 下Oracle_11g_R2 编码工具:Eclipse 编码平台:W ...

  5. loadrunner 脚本开发-调用java jar文件远程操作Oracle数据库测试

    调用java jar文件远程操作Oracle数据库测试 by:授客 QQ:1033553122 测试环境 数据库:linux 下Oracle_11g_R2 Loadrunner:11 备注:想学ora ...

  6. dos命令行连接操作ORACLE数据库

    C:\Adminstrator> sqlplus "/as sysdba" 查看是否连接到数据库 SQL> select status from v$instance; ...

  7. java 操作Oracle 批量入库的问题

    java 操作Oracle 批量入库的问题 先说下我运行的环境: Windows7 64位操作系统 (四核)Intel i5-2300 CPU @2.80GHz 内存4G 硬盘1T Jdk1.6+My ...

  8. 在.NetCore(C#)中使用ODP.NET Core+Dapper操作Oracle数据库

    前言 虽然一直在说"去IOE化",但是在国企和政府,Oracle的历史包袱实在太重了,甚至很多业务逻辑都是写在Oracle的各种存储过程里面实现的-- 我们的系统主要的技术栈是Dj ...

  9. Java操作Oracle

    public class DBCon { // 数据库驱动对象 public static final String DRIVER = "oracle.jdbc.driver.OracleD ...

随机推荐

  1. python04 面向对象编程02

    为啥要用类而不用函数呢 记住两个原则: 减少重复代码 代码会经常变更 2    会对变量或字符串的合法性检测(在实例初始化的时候能够统一初始化各个实例的变量,换做函数来说,要弄出同样的变量那么在初始化 ...

  2. Raspberry Pi 3 --- Kernel Building and Run in A New Version Kernal

    ABSTRACT There are two main methods for building the kernel. You can build locally on a Raspberry Pi ...

  3. JavaScript之继承(原型链)

    JavaScript之继承(原型链) 我们知道继承是oo语言中不可缺少的一部分,对于JavaScript也是如此.一般的继承有两种方式:其一,接口继承,只继承方法的签名:其二,实现继承,继承实际的方法 ...

  4. uC/OS-II时间(OS_time)块

    /*************************************************************************************************** ...

  5. Java 运行环境的安装、配置与运行

    (一)SDK 的下载与安装 1. 下载SDK 为了建立基于SDK 的Java 运行环境,需要先下载Sun 的免费SDK 软件包.SDK 包含了一整套开发工具,其中包含对编程最有用的是Java 编译器. ...

  6. MyEclipse取消自动跳到Console窗口

    在Myeclipse中当全屏查看其它文件时,如果控制台有东西输出,就会弹出控制台窗口,如何取消? 方法1: -->右键在console窗口中点Preferences, -->将Show w ...

  7. xpath php

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < ...

  8. Java同步synchronized与死锁

    多个线程要操作同一资源时就有可能出现资源的同步问题. 同步就是指多个操作在同一个时间段内只能有一个线程进行,其他线程要等待此线程完成之后才可以继续执行. 解决资源共享的同步操作,可以使用同步代码块和同 ...

  9. iOS注册,找回密码时用到的获取验证码

    #import "ViewController.h" #import "NSTimer+BlocksKit.h" @interface ViewControll ...

  10. 2.servlet的会话机制session

    session的说明: 1.session是服务端技术,存放在服务器 2.一个用户浏览器对应一个session域对象,一对一的对应关系 3.session的默认生命周期是30min,可以通过web.x ...