标签:
存储过程与存储函数说明:存储函数有返回值!存储过程没有返回值!
指存储在数据库中供所有用户程序调用的子程序叫存储过程、存储函数。
什么时候用存储过程/存储函数
原则:如果只有一个返回值,用存储函数;否则,就用存储过程。
1.创建存储过程
用CREATE PROCEDURE命令建立存储过程。语法如下:
create [or replace] PROCEDURE 过程名[(参数列表)]
AS
变量声明
PLSQL子程序体;
1)存储过程入门:
create or replace
procedure sayHello
as
begin
dbms_output.put_line('hello world!');
end;
cmd命令行执行程序:
2)给员工涨工资
create or replace
procedure addSal(eno in number)
as
begin
update emp2 set sal=sal*1.1 where empno=eno;
dbms_output.put_line('涨工资完毕');
end;
3)带有输出结果的存储过程
create or replace procedure queryEmpSal (eno in number,pname out VARCHAR2,psal out number) as begin select ename ,sal into pname,psal from emp2 where empno=eno; end;测试上述代码:
我们通过“从数据库Scott断开连接”这句话 可以判断数据库已经正常关闭-->因此数据库已经帮我们自动提交了事务!
2.创建存储函数
函数(Function)为一命名的存储程序,可带参数,并返回一计算值。函数和过程的结构类似,但必须有一个RETURN子句,用于返回函数值。函数说明要指定函数名、结果值的类型,以及参数类型等。
语法如下:
CREATE[OR REPLACE] FUNCTION 函数名(参数列表)
RETURN 函数值类型
AS
变量声明
PLSQL子程序体;
1)存储函数入门:create or replace function querySal(eno in number) return number as vsal emp2.sal%type; begin select sal into vsal from emp2 where empno=eno; return vsal; end;测试上述代码:
2)带有输出参数的存储函数
create or replace function queryFuncEmpSal (eno in number,pname out VARCHAR2,psal out number) return number as begin select ename ,sal into pname,psal from emp2 where empno=eno; return 1; end;================================================================================================
通过jdbc调用存储过程和存储函数
1)搭建环境
导入oracle的jar包:E:\app\Administrator\product\11.2.0\dbhome_1\jdbc\lib
2)写工具类JDBCUtils
package cn.itcast.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtils {
private static String jdbcUrl="jdbc:oracle:thin:@localhost:1521:orcl";
private static String driverClass="oracle.jdbc.OracleDriver";
private static String username="scott";
private static String password="169500";
/**
* 获取数据库连接
*
*/
public static Connection getConnection(){
try {
Class.forName(driverClass);//注册驱动
return DriverManager.getConnection(jdbcUrl, username, password);
} catch (Exception e) {
throw new RuntimeException();
}
}
/**
* 关闭资源
* public
*/
public static void closeResource(Connection conn,Statement state,ResultSet rs){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
conn=null;
}
}
if(state!=null){
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
state=null;
}
}
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
rs=null;
}
}
}
}
2)测试存储过程:
package cn.itcast.test;
import java.sql.CallableStatement;
import java.sql.Connection;
import oracle.jdbc.internal.OracleTypes;
import org.junit.Test;
import cn.itcast.util.JDBCUtils;
public class TestProcedure {
Connection con=null;
CallableStatement call=null;
@Test
public void test() throws Exception{
String sql="{call addSal(?)}";
con=JDBCUtils.getConnection();
call=con.prepareCall(sql);
call.setObject(1, 7369);
//设置参数
call.execute();//执行
//显示结果
JDBCUtils.closeResource(con, call, null);
}
@Test
public void test1() throws Exception{
String sql="{call queryEmpSal(?,?,?)}";
con=JDBCUtils.getConnection();
call=con.prepareCall(sql);
//设置输入参数
call.setObject(1, 7369);
//设置输出参数
call.registerOutParameter(2, OracleTypes.VARCHAR);
call.registerOutParameter(3, OracleTypes.NUMBER);
call.execute();//执行
//显示结果
String name = (String) call.getObject(2);
Double value = call.getDouble(3);
System.out.println(name+":"+value);
JDBCUtils.closeResource(con, call, null);
}
}
单元测试test1()
3)测试存储函数-->关于sql的写法去jdk中的
package cn.itcast.test;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import org.junit.Test;
import oracle.jdbc.internal.OracleTypes;
import cn.itcast.util.JDBCUtils;
public class TestFunction {
@Test
public void test() throws Exception{
Connection conn=null;
CallableStatement call=null;
String sql="{?=call querySal(?)}";
conn=JDBCUtils.getConnection();
call=conn.prepareCall(sql);
call.setObject(2,7934);
call.registerOutParameter(1, OracleTypes.NUMBER);
//执行结果
call.execute();
//获取返回结果
Object object = call.getObject(1);
System.out.println(object);
}
}
注意:关于sql的写法去jdk中的:
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/u014010769/article/details/47699745