JAVA操作ORACLE数据库的存储过程
一、任务提出
JAVA操作oracle11g存储过程实验需要完成以下几个实例:
1.调用没有返回参数的过程(插入记录、更新记录)
2.有返回参数的过程
3.返回列表的过程
4.返回带分页的列表的过程。
二、建立表和相应的存储过程
create table student (sno int ,sname varchar2(20),sage int);
- --创建存储过程testa1
- create or replace procedure testa1(para1 in int,para2 in varchar2,para3 in int)
- is
- begin
- insert into student(sno,sname,sage) values(para1,para2,para3);
- commit;
- end;
- /
- --创建存储过程testa2
- create or replace procedure testa2(para1 in int,para2 in int)
- is
- begin
- update student set sage=para2 where sno=para1;
- commit;
- end;
- /
- --创建有返回参数的存储过程testb
- create or replace procedure testb(para1 in int ,para2 out varchar2,para3 out int)
- is
- begin
- select sname,sage into para2,para3 from student where sno=para1;
- end;
- --创建返回集合的存储过程:
- --在oracle中,如果要返回集合必须是返回游标,不能是一张二维表。所以,要先建立包。
- create or replace package testpack
- is
- type test_cursor is ref cursor;
- end testpack;
- /
- create or replace procedure testc(p_cursor out testpack.test_cursor)
- is
- begin
- open p_cursor for select * from student;
- end;
- /
- --实现分页的存储过程
- ---ps 每页几个记录,cs第几页
- create or replace procedure testd(ps int ,cs int ,p_cursor out testpack.test_cursor)
- is
- begin
- open p_cursor for
- select * from (select student.*,rownum rn from student) where rn>ps*(cs-1) and rn<=ps*cs;
- end;
- /
三、java操作调用上述存储过程
- package com.oaj;
- import java.sql.*;
- import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
- public class Test {
- String driver="oracle.jdbc.driver.OracleDriver";
- String strUrl="jdbc:oracle:thin:@localhost:1521:orcl";
- ResultSet rs=null;
- Connection conn=null;
- CallableStatement cstmt=null;
- public static void main(String[] args)
- {
- new Test().testPageSet(3,1);
- }
- public void testPageSet(int recordPerPage,int currentPage)
- {
- try
- {
- Class.forName(driver);
- conn=DriverManager.getConnection(strUrl,"scott","scott");
- cstmt=conn.prepareCall("{ call scott.testd(?,?,?)}");
- cstmt.registerOutParameter(3, oracle.jdbc.OracleTypes.CURSOR);//指定是oracle里规定的类型
- cstmt.setInt(1,recordPerPage);
- cstmt.setInt(2, currentPage);
- cstmt.execute();
- rs=(ResultSet)cstmt.getObject(3);
- while(rs.next())
- {
- System.out.print("学号是:"+rs.getInt(1)+"的学生的名字是:"+rs.getString(2)+",年龄是:"+rs.getInt(3)+"\r\n");
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- finally
- {
- try
- {
- if(cstmt!=null)
- cstmt.close();
- if(conn!=null)
- {
- conn.close();
- conn=null;
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- }
- public void testOutResult()
- {
- try
- {
- Class.forName(driver);
- conn=DriverManager.getConnection(strUrl,"scott","scott");
- cstmt=conn.prepareCall("{ call scott.testc(?)}");
- cstmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);//指定是oracle里规定的类型
- cstmt.execute();
- rs=(ResultSet)cstmt.getObject(1);
- while(rs.next())
- {
- System.out.print("学号是:"+rs.getInt(1)+"的学生的名字是:"+rs.getString(2)+",年龄是:"+rs.getInt(3)+"\r\n");
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- finally
- {
- try
- {
- if(cstmt!=null)
- cstmt.close();
- if(conn!=null)
- {
- conn.close();
- conn=null;
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- }
- public void testOutParameter(int inputSno)
- {
- try
- {
- Class.forName(driver);
- conn=DriverManager.getConnection(strUrl,"scott","scott");
- cstmt=conn.prepareCall("{ call scott.testb(?,?,?)}");
- cstmt.setInt(1, inputSno);
- cstmt.registerOutParameter(2, Types.VARCHAR);
- cstmt.registerOutParameter(3, Types.INTEGER);
- cstmt.execute();
- String name=cstmt.getString(2);
- int age=cstmt.getInt(3);
- System.out.print("学号是:"+inputSno+"的学生的名字是:"+name+",年龄是:"+age);
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- finally
- {
- try
- {
- if(cstmt!=null)
- cstmt.close();
- if(conn!=null)
- {
- conn.close();
- conn=null;
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- }
- public void testNoOutParameterUpdate(int inputeSno,int inputSage)
- {
- try
- {
- Class.forName(driver);
- conn=DriverManager.getConnection(strUrl,"scott","scott");
- cstmt=conn.prepareCall("{ call scott.testa2(?,?)}");
- cstmt.setInt(1, inputeSno);
- cstmt.setInt(2, inputSage);
- cstmt.execute();
- System.out.println("执行成功!");
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- finally
- {
- try
- {
- if(cstmt!=null)
- cstmt.close();
- if(conn!=null)
- {
- conn.close();
- conn=null;
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- }
- public void testNoOutParameterInsert(int a,String b,int c)
- {
- try
- {
- Class.forName(driver);
- conn=DriverManager.getConnection(strUrl,"scott","scott");
- cstmt=conn.prepareCall("{ call scott.testa1(?,?,?)}");
- cstmt.setInt(1, a);
- cstmt.setString(2, b);
- cstmt.setInt(3, c);
- cstmt.execute();
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- finally
- {
- try
- {
- if(cstmt!=null)
- cstmt.close();
- if(conn!=null)
- {
- conn.close();
- conn=null;
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- }
- }
JAVA操作ORACLE数据库的存储过程的更多相关文章
- Java操作Oracle数据库以及调用存储过程
操作Oracle数据库 publicclass DBConnection { //jdbc:oracle:thin:@localhost:1521:orcl publicstaticf ...
- Java 操作Oracle数据库
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...
- JAVA操作Oracle数据库中的事务
实验1: create table yggz(code int, salary number(7,2)); insert into yggz values(1, 1000); insert into ...
- Java java jdbc thin远程连接并操作Oracle数据库
JAVA jdbc thin远程连接并操作Oracle数据库 by:授客 QQ:1033553122 测试环境 数据库:linux 下Oracle_11g_R2 编码工具:Eclipse 编码平台:W ...
- loadrunner 脚本开发-调用java jar文件远程操作Oracle数据库测试
调用java jar文件远程操作Oracle数据库测试 by:授客 QQ:1033553122 测试环境 数据库:linux 下Oracle_11g_R2 Loadrunner:11 备注:想学ora ...
- dos命令行连接操作ORACLE数据库
C:\Adminstrator> sqlplus "/as sysdba" 查看是否连接到数据库 SQL> select status from v$instance; ...
- java 操作Oracle 批量入库的问题
java 操作Oracle 批量入库的问题 先说下我运行的环境: Windows7 64位操作系统 (四核)Intel i5-2300 CPU @2.80GHz 内存4G 硬盘1T Jdk1.6+My ...
- 在.NetCore(C#)中使用ODP.NET Core+Dapper操作Oracle数据库
前言 虽然一直在说"去IOE化",但是在国企和政府,Oracle的历史包袱实在太重了,甚至很多业务逻辑都是写在Oracle的各种存储过程里面实现的-- 我们的系统主要的技术栈是Dj ...
- Java操作Oracle
public class DBCon { // 数据库驱动对象 public static final String DRIVER = "oracle.jdbc.driver.OracleD ...
随机推荐
- python04 面向对象编程02
为啥要用类而不用函数呢 记住两个原则: 减少重复代码 代码会经常变更 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 ...
- JavaScript之继承(原型链)
JavaScript之继承(原型链) 我们知道继承是oo语言中不可缺少的一部分,对于JavaScript也是如此.一般的继承有两种方式:其一,接口继承,只继承方法的签名:其二,实现继承,继承实际的方法 ...
- uC/OS-II时间(OS_time)块
/*************************************************************************************************** ...
- Java 运行环境的安装、配置与运行
(一)SDK 的下载与安装 1. 下载SDK 为了建立基于SDK 的Java 运行环境,需要先下载Sun 的免费SDK 软件包.SDK 包含了一整套开发工具,其中包含对编程最有用的是Java 编译器. ...
- MyEclipse取消自动跳到Console窗口
在Myeclipse中当全屏查看其它文件时,如果控制台有东西输出,就会弹出控制台窗口,如何取消? 方法1: -->右键在console窗口中点Preferences, -->将Show w ...
- xpath php
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < ...
- Java同步synchronized与死锁
多个线程要操作同一资源时就有可能出现资源的同步问题. 同步就是指多个操作在同一个时间段内只能有一个线程进行,其他线程要等待此线程完成之后才可以继续执行. 解决资源共享的同步操作,可以使用同步代码块和同 ...
- iOS注册,找回密码时用到的获取验证码
#import "ViewController.h" #import "NSTimer+BlocksKit.h" @interface ViewControll ...
- 2.servlet的会话机制session
session的说明: 1.session是服务端技术,存放在服务器 2.一个用户浏览器对应一个session域对象,一对一的对应关系 3.session的默认生命周期是30min,可以通过web.x ...