1、pl/sql块包括三个部分,定义部分,执行部分,异常处理部分,以下是一个例子:
declare
--定义变量(可选)
v_name varchar2(20);
v_sal number;
begin
--执行部分
select name,sal into v_name,v_sal from user1 where id=2;
dbms_output.put_line('查询到的用户名为:'||v_name);
--异常处理(可选)
exception
when no_data_found then
dbms_output.put_line('查询不到制定的用户');
end;
2、存储过程示例:
(1)创建一个名称为prc_user 的不带参数的存储过程:
create or replace procedure prc_user is
begin
update user1 set sal=11 where id=1;
end;
对应的java代码调用如下:
import java.sql.*; public class DB { public static void main(String[] args) { try { //1.加载驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); //2.建立数据库连接 Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@127.0.0.1:1521:orcl","cxq", "cxq123"); //3.创建CallableStatement CallableStatement cst=con.prepareCall("{call prc_user}"); //4.执行 cst.execute(); //4.关闭 cst.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } } }
(2)创建一个名称为prc_user1 的带输入参数的存储过程:
create or replace procedure prc_updateUserSal(userId IN number,newSal IN number)
is
begin
update user1 set sal=newSal where id=userId;
end;
注意:输入参数IN是可以省略的,默认就是IN。对应的java代码调用如下:
import java.sql.*; public class DB { public static void main(String[] args) { try { //1.加载驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); //2.建立数据库连接 Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@127.0.0.1:1521:orcl","cxq", "cxq123"); //3.创建CallableStatement CallableStatement cst=con.prepareCall("{call prc_updateUserSal(1,3500)}"); /*或者这样 3.创建CallableStatement CallableStatement cst=con.prepareCall("{call prc_updateUserSal(?,?)}"); cst.setInt(1, 1); cst.setInt(2, 3500); */ //4.执行 cst.execute(); //4.关闭 cst.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } } }
(3)创建一个名称为prc_user2 的带输入参数和输出参数的存储过程:
create or replace procedure prc_getUserSal(userId IN number,userSal OUT number) is
begin
select sal into userSal from user1 where id=userId;
end;
对应的java代码调用如下:
3、函数示例:
(1)输入用户的姓名,返回用户的年薪:
create or replace function f_getUserYearSal(userName varchar2) return number
as
yearSal number;
begin
select sal*12 into yearSal from user1 where name=userName;
return yearSal;
end;
在命令行中调用函数如下:
SQL> var yearSal number;
SQL> call f_getUserYearSal('cxq') into:yearSal;
返回结果:
Method called
yearSal
---------
60000
如果没返回就打印变量:
SQL> print yearSal;
会返回结果:
yearSal
---------
60000
对应的java代码调用如下:import java.sql.*; public class Test { public static void main(String[] args) { try { //1.加载驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); //2.建立数据库连接 Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@127.0.0.1:1521:orcl","cxq", "cxq123"); //3.创建PreparedStatement String sqlStr="select f_getUserYearSal(?) from dual"; PreparedStatement psmt=con.prepareStatement(sqlStr); psmt.setString(1, "cxq"); //4.执行查询 ResultSet rs=psmt.executeQuery(); //5.处理结果集 while(rs.next()){ int yearSal=rs.getInt(1); System.out.println("用户cxq的年薪为"+yearSal); } //6.关闭 psmt.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } } }
相关推荐
Oracle PL/SQL实例精解 student模式 数据库建立代码 包括数据库的建立、索引等。样本数据的插入。
oracle pl/sql实例精讲student数据库模式
Oracle PL/SQL实例编程 Oracle PL/SQL实例编程 Oracle PL/SQL实例编程
《Oracle PL/SQL程序设计(第5版)》基于Oracle数据库11g,从PL/SQL编程、PL/SQL程序结构、PL/SQL程序数据、PL/SQL中的SQL、PL/SQL应用构建、高级PL/SQL主题这6个方面详细系统地讨论了PL/SQL以及如何有效地使用它。...
oracle pl sql 实例精解 oracle pl sql 实例精解 oracle pl sql 实例精解
本书是一本逐步分解的,详尽的pl/sql编程教程,使用真实场景的试验、范例和练习来介绍读者所需的pl/sql编程技能,涵盖oracle 11g的最新特性。作者的写作手法源自于在哥伦比亚大学教授pl/sql编程技术的经验,深度...
oracle数据库 sql 和pl/sql实例教程
《Oracle PL/SQL程序设计(第5版)》基于Oracle数据库11g,从PL/SQL编程、PL/SQL程序结构、PL/SQL程序数据、PL/SQL中的SQL、PL/SQL应用构建、高级PL/SQL主题这6个方面详细系统地讨论了PL/SQL以及如何有效地使用它。...
Oracle数据库SQL和PL/SQL实例教程 西安电子科技大学出版社
Oracle Database 12c PL/SQL开发指南(第7版)书中示例的PL/SQL源代码
Oracle PL/SQL实例精讲第五版--里面的使用表,课后答案,以及书中的案例,文件里有3个包。 很难受,本来只是想1积分的,居然这么高,没法修改。
本书讲解在Oracle 11g中使用PL/SQL语言的方法,每章都突出了PL/SQL语言的使用技巧,每个关键技术都给出了示例,每个实例都在真实的环境中演练。本书从认识PL/SQL语言起步,到使用PL/SQL语言编程,再到最后掌握优化PL...
通过实例详细讲解Oracle PL/SQL的几个主要特性。很详细易懂。
ORACLE_PL_SQL实例精解 第五部分
Oracle PL/SQL by Example, 4th Edition,全英文759页,附加示例库架构,建库脚本,每章源码(word格式呈现)
ORACLE_PL_SQL实例精解(原书第4版),含书签,共617页
通过实例说明在PL/SQL中,%Found, %notfound 和 EXCEPTION的用法
ORACLE PL_SQL实例精解 第四部分
实例学pl/sql,很独到,很实用,配套资源可上http://www.informit.com/store/product.aspx?isbn=0137144229下载,好好练两遍,pl/sql就成了
高清pdf——ORACLE PLSQL实例精解(第4版) 拿去用不谢.