java调用存储过程和函数
以对表test进行增,删,改,查进行说明:
1.新建表test
create table TEST
(
TID NUMBER not null,
TNAME VARCHAR2(32),
TCODE VARCHAR2(32),
CREATEDATE DATE
)
alter table TEST
add constraint PK_TEST_ID primary key (TID)
2.数据库中存储过程的脚本:
create or replace package test_package is
procedure test_add(pid number,pcode varchar2,pname varchar2,pcreatedate date);
procedure test_update(pid number,pname varchar2);
procedure getNameAndCodeById(pid number,pcode out varchar2,pname out varchar2);
function getNameById(pid number) return varchar2;
end test_package;
create or replace package body test_package is
/*
向表中增加一条数据
*/
procedure test_add(pid number,pcode varchar2,pname varchar2,pcreatedate date)is
begin
insert into test values(pid,pcode,pname,pcreatedate);
commit;
end;
/*
将表中tid为pid的数据的tname更新为pname
*/
procedure test_update(pid number,pname varchar2)is
begin
update test set tname=pname where tid=pid;
commit;
end;
/*
根据ID查找对应的code和name
*/
procedure getNameAndCodeById(pid number,pcode out varchar2,pname out varchar2)is
begin
select t.tcode,t.tname into pcode,pname from test t where t.tid=pid;
end;
/*
根据ID查找对应的name
*/
function getNameById(pid number) return varchar2 is
returnValue varchar2(100) :='';
begin
select t.tname into returnValue from test t where t.tid=pid;
return returnValue;
end;
end test_package;
3.在java程序中调用存储过程
A.通过存储过程向表中添加一条数据
public void add() throws Exception
{
Connection con=new Condb().getConnection();
String sql="{call test_package.test_add(?,?,?,?)}";
// sql也可以写为 "begin test_package.test_add(?,?,?,?);end;"
CallableStatement stmt=con.prepareCall(sql);
stmt.setInt(1, 1);
stmt.setString(2, "test");
stmt.setString(3, "test");
//注意日期类型的处理
stmt.setDate(4, new Date(new java.util.Date().getTime()));
stmt.execute();
stmt.close();
con.close();
}
B.通过给定的tid更新这条数据的tname字段
public void update() throws Exception
{
Connection con=new Condb().getConnection();
String sql="{call test_package.test_update(?,?)}";
CallableStatement stmt=con.prepareCall(sql);
stmt.setInt(1, 1);
stmt.setString(2, "update");
stmt.execute();
stmt.close();
con.close();
}
C.调用存储过程根据给定的tid返回tcode和tname
public void getNameAndCodeById() throws Exception
{
String tcode="";
String tname="";
Connection con=new Condb().getConnection();
String sql="{call test_package.getNameAndCodeById(?,?,?)}";
CallableStatement stmt=con.prepareCall(sql);
stmt.setInt(1, 1);
stmt.registerOutParameter(2, Types.VARCHAR);
stmt.registerOutParameter(3, Types.VARCHAR);
stmt.execute();
tname=stmt.getString(2);
tcode=stmt.getString(3);
System.out.println("tname="+tname+",tcode="+tcode);
stmt.close();
con.close();
}
D.通过调用函数根据给定的tid返回tname
public void getNameById() throws Exception
{
String tname="";
Connection con=new Condb().getConnection();
String sql="select test_package.getNameById(?) from dual";
CallableStatement stmt=con.prepareCall(sql);
stmt.setInt(1, 1);
ResultSet rs=stmt.executeQuery();
while(rs.next())
{
tname=rs.getString(1);
}
System.out.println("name="+tname);
stmt.close();
con.close();
}
java调用存储过程和函数的更多相关文章
- 【转】java调用存储过程和函数
一.概述 如果想要执行存储过程,我们应该使用 CallableStatement 接口. CallableStatement 接口继承自PreparedStatement 接口.所以CallableS ...
- Java 调用存储过程、函数
一.Java调用存储Oracle存储过程 测试用表: --创建用户表 create table USERINFO ( username ) not null, password ) not null ...
- JDBC第二篇--【PreparedStatment、批处理、处理二进制、自动主键、调用存储过程、函数】
这是我JDBC的第一篇 http://blog.csdn.net/hon_3y/article/details/53535798 1.PreparedStatement对象 PreparedState ...
- JDBC【PreparedStatment、批处理、处理二进制、自动主键、调用存储过程、函数】
1.PreparedStatement对象 PreparedStatement对象继承Statement对象,它比Statement对象更强大,使用起来更简单 Statement对象编译SQL语句时, ...
- JDBC(13)—JDBC调用存储过程和函数
步骤: JDBC调用存储过程和函数 步骤: ①:通过Connection对象的prepareCall()方法创建一个CallableStatement对象的实例,在使用Connection对象的pre ...
- 转:EF调用存储过程、函数
EF调用存储过程.函数 2014-04-02 09:12:20| 分类: ORM框架|举报|字号 订阅 一.ef4.1 codeFirst 修改表结构 增加字段等 EF code ...
- clob字段的值插入和查询N种方法【包括java调用存储过程传入clob参数】
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import jav ...
- MySQL学习笔记:调用存储过程或函数报1418错误
问题 MySQL开启bin-log后,调用存储过程或者函数以及触发器时,会出现错误号为1418的错误: ERROR 1418 (HY000): This function has none of DE ...
- Oracle数据库基本操作 (五) —— 使用java调用存储过程
一.环境准备 登录Oracle数据库scott账号,利用emp进行操作. 1.创建 proc_getyearsal 存储过程 -- 获取指定员工年薪 create or replace procedu ...
随机推荐
- 转载:Character data is represented incorrectly when the code page of the client computer differs from the code page of the database in SQL Server 2005
https://support.microsoft.com/en-us/kb/904803 Character data is represented incorrectly when the cod ...
- 转:关于视频H264编解码的应用实现
转:http://blog.csdn.net/scalerzhangjie/article/details/8273410 项目要用到视频编解码,最近半个月都在搞,说实话真是走了很多弯路,浪费了很多时 ...
- loadrunner协议的选择
1. 任何高级协议的底层都是用Winsocket通信 2. 不管你系统中有多少个服务器,lr录制的始终是客户端与第一个服务器之间的通信内容, 客户端用IE访问的一般都选http协议(对于常见的,b ...
- ElasticSearch 高可用分布式集群搭建,与PHP多线程测试
方案: 使用HAproxy:当其中一台ElasticSearch Master宕掉时,ElasticSearch集群会自动将运行正常的节点提升为Master,但HAproxy不会将失败的请求重新分发到 ...
- django 学习-12 Django表单 初步
1.先创建项目和应用 django.admin.py startproject cs cd cs django.admin.py startapp blog 2.vim setti ...
- 在IIS上发布项目后浏览时报的错:Unable to make the session state request to the session state server
错误描述: Unable to make the session state request to the session state server. Please ensure that the A ...
- MySQLdb模块安装-win环境
原帖地址:http://blog.csdn.net/wklken/article/details/7253245 使用python访问mysql,需要一系列安装 linux下MySQLdb安装见 P ...
- Part 97 Performance of a multithreaded program
class Program { static void Main(string[] args) { Stopwatch s = new Stopwatch(); s.Start(); EvenNumb ...
- iOS开发那些事-iOS6苹果地图实用开发
在iOS 6之后,不再使用谷歌地图了,而是使用苹果自己的地图,但是API编程接口没有太大的变化.开发人员不需要再学习很多新东西就能开发地图应用,这是负责任的做法.因此本节介绍的内容也同样适用于iOS5 ...
- javascript之正则表达式总结
了解RegExp类型: ECMAScript通过RegExp类型来支持正则表达式. var expression=/pattern/flags; 正则表达式的模式(pattern)部分: 可以是任何简 ...