版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sl1992/article/details/85010455
使用Java语言调用存储过程与存储函数
1.JDBC工具类
<!-- https://mvnrepository.com/artifact/com.github.noraui/ojdbc8 -->
<dependency>
<groupId>com.github.noraui</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency>
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCUtils {
private static String url = "jdbc:oracle:thin:@1.1.1.1:1521:cdb1";
private static String driverClass = "oracle.jdbc.OracleDriver";
private static String userName = "c##solang";
private static String password = "solang";
/**
* @return 获取数据库连接
*/
public static Connection getConnection() {
try {
Class.forName(driverClass);
return DriverManager.getConnection(url, userName, password);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 释放数据库连接
* @param conn
* @param st
* @param rs
*/
public static void close(Connection conn, Statement st, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
try {
if (st != null) {
st.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
2.JDBC调用存储过程
例,输入员工编号,输出员工名称和薪资
create or replace procedure queryEmpNameSal(in_empno in number,
out_ename out varchar2,
out_sal out number)
as
begin
select ename,sal into out_ename,out_sal from emp where empno = in_empno;
end;
调用存储过程{call <procedure-name>[(<arg1>, <arg2>, ...)]}
,一个问号代表一个参数,从1开始。
import java.sql.CallableStatement;
import java.sql.Connection;
import oracle.jdbc.OracleTypes;
import org.junit.Test;
@Test
public void testProcedure() throws Exception {
Connection conn = null;
CallableStatement call = null;
String sql = "{ call queryEmpNameSal(?,?,?) }";
try {
conn = JDBCUtils.getConnection();
/** 调用存储过程使用CallableStatement */
call = conn.prepareCall(sql);
/** 设置输入参数 */
call.setInt(1, 7369);
/** 设置输出参数 */
call.registerOutParameter(2, OracleTypes.VARCHAR);
call.registerOutParameter(3, OracleTypes.NUMBER);
/** 执行 */
call.execute();
/** 获取返回的结果(out输出参数) */
String outEname = call.getString(2);
Double outSal = call.getDouble(3);
System.out.println(outEname + "的工资是:" + outSal);
} finally {
/** 释放资源 */
JDBCUtils.close(conn, call, null);
}
}
SMITH的工资是:800.0
3.JDBC调用存储函数
例,查询某职工全年的总收入
create or replace function queryEmpSalary(in_empno in number)
return number
as
pSal number; -- 定义变量保存员工的工资
pComm number; -- 定义变量保存员工的奖金
begin
select sal,comm into pSal,pComm from emp where empno = in_empno;
return pSal * 12 + nvl(pComm,0);
end;
调用存储函数{ ?= call <function-name>[(<arg1>, <arg2>, ...)]}
,一个问号代表一个参数,从1开始,第一个参数表示返回值。
import java.sql.CallableStatement;
import java.sql.Connection;
import oracle.jdbc.OracleTypes;
import org.junit.Test;
@Test
public void testFunction() throws Exception {
Connection conn = null;
CallableStatement call = null;
String sql = "{ ?= call queryEmpSalary(?) }";
try {
conn = JDBCUtils.getConnection();
/** 调用存储过程使用CallableStatement */
call = conn.prepareCall(sql);
/** 设置输入参数 */
call.setInt(2, 7369);
/** 返回值 */
call.registerOutParameter(1, OracleTypes.NUMBER);
/** 执行 */
call.execute();
/** 获取返回值 */
Double totalSal = call.getDouble(1);
System.out.println("全年的总工资是:" + totalSal);
} finally {
/** 释放资源 */
JDBCUtils.close(conn, call, null);
}
}
全年的总工资是:9600.0