一、任务提出

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数据库的存储过程的更多相关文章

  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. Linux进程关闭和后台运行解析

    1.问题背景 Java是跨平台的,大部分程序也都是在Linux服务器上运行的.但是很多朋友其实对服务器了解并不多,对相关知识也是一知半解.很多概念可能知道,但是并不十分清楚,仅仅是基本运用.可能很多新 ...

  2. Yocto开发笔记之《Tip-应用程序无法在目标机运行》(QQ交流群:519230208)

    官方app编译环境编译的应用程序可以在开发板自带的系统中运行,自己编译并烧写了系统,却提示: root@imx6ulevk:/root# ./VTTerminal -sh: ./VTTerminal: ...

  3. HBase Shell 常用命令及例子

    下面我们看看HBase Shell的一些基本操作命令,我列出了几个常用的HBase Shell命令,如下: 名称 命令表达式 创建表 create '表名称', '列名称1','列名称2','列名称N ...

  4. easyUI validatebox设置值和获取值,以及属性和方法

    一:表单元素使用easyui时,textbox和validatebox设置值和获取值的方式不一样[转] 1.为text-box设置值只能使用id选择器选择表单元素,只能使用textbox(" ...

  5. Eratosthenes筛选法构造1-n 素数表

    筛选法:对于不超过n的每个非负整数p,删除2p,3p,4p...当处理完所有数之后,还没没删除的就是素数. 代码中进行了相应的优化. 本代码功能,输入一个数,输出从1-该数之间的素数.功能待完善,可将 ...

  6. thinkphp 3.2响应头 x-powered-by 修改

    起初是看到千图网的登录链接 查看到的 自己做的网站也看了下 修改的办法就是TP3.2.2 的框架里 具体路径是D:\www\ThinkPHP\Library\Think\View.class.php ...

  7. phpmyadmin 链接远程mysql

    这个只是自己的笔记 新手 不记下来以后又忘记了~ 在这以前已经给mysql设置了可以远程连接的账户 版本 phpMyAdmin-4.2.11-all-languages 解压到D盘下www   本地环 ...

  8. OC-方法的声明和实现、匿名对象

    方法声明: 方法调用: *冒号也是方法名的一部分 *同一个类中不允许两个对象方法同名 练习 给Car类设计一个方法,用来和其他车比较车速,如果快返回1,慢返回-1,相同返回0 #import < ...

  9. ActionScript学习笔记

    ActionScript学习笔记 ActionScript中预定义的数据类型:Boolean.int.Number.String.uint 其中,int.Number.uint是处理数字的.int用来 ...

  10. Yii2.0中文开发向导——Yii2中多表关联查询(join、joinwith)(转)

    我们用实例来说明这一部分 表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order          (id  order_ ...