一 概述
本篇介绍用java访问存储过程和访问存储函数
二 开发环境搭建
1、安装oracle时候,会带有jdbc的驱动程序,存放路径如下:
D:\app\lenovo\product\11.2.0\dbhome_1\jdbc\lib
2、拷贝ojdbc6.jar到eclipse的工程目录的lib目录下。
F:\java\workspace\TestOracle\lib
3、在eclipse工程中选中ojdbc6.jar,右键Build Path,将该jar包加到开发环境中。
4、环境搭建后截图如下:
三 创建JDBC工具类
代码如下:
package demo.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.sqlException;
import java.sql.Statement;
publicclassJDBCUtils{
privatestaticString driver ="oracle.jdbc.OracleDriver";
privatestaticString url ="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
privatestaticString user ="scott";
privatestaticString password ="tiger";
//注册数据库的驱动
static
{
try{
Class.forName(driver);
//DriverManager.registerDriver(driver);
}catch(ClassNotFoundException e){
thrownewExceptionInInitializerError(e);
}
}
//获得数据库连接
publicstaticConnection getConnection(){
try{
returnDriverManager.getConnection(url, user, password);
}catch(sqlException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
returnnull;
//释放数据库的资源
publicstaticvoid release(Connection conn,Statement st,ResultSet rs){
if(rs!=null){
rs.close();
}catch(sqlException e){
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
rs =null;
}
}
if(st!=null){
st.close();
st=null;
if(conn !=null){
conn.close();
conn =null;
}
}
四 在应用程序中如何访问存储过程
1、构建测试类:
TestProcedure
package demo.oracle;
//import static org.junit.Assert.*;
import java.sql.CallableStatement;
import java.sql.Connection;
import oracle.jdbc.OracleTypes;
import org.junit.Test;
import demo.utils.JDBCUtils;
publicclassTestProcedure{
/*
create or replace procedure queryempinformation(eno in number,
pename out varchar2,
psal out number,
pjob out varchar2)
* */
@Test
publicvoid testProdedure(){
String sql ="{call queryempinformation(?,?,?)}";
Connection conn =null;
CallableStatement call=null;
try{
//得到一个连接
conn =JDBCUtils.getConnection();
call = conn.prepareCall(sql);
//对于输入参数,赋值
call.setInt(1,7839);
//对于输出参数,申明
call.registerOutParameter(2,OracleTypes.VARCHAR);
call.registerOutParameter(3,OracleTypes.NUMBER);
call.registerOutParameter(4,OracleTypes.VARCHAR);
//执行调用
call.execute();
//取出结果
String name = call.getString(2);
double sal =call.getDouble(3);
String job = call.getString(4);
System.out.println(name+"\t"+sal+"\t"+job);
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils.release(conn, call,null);
}
}
}
2、测试结果:
KING10100.0PRESIDENT
五 在应用程序中如何访问存储函数
1、构建测试类TestFunction
import java.sql.CallableStatement;
import java.sql.Connection;
import oracle.jdbc.OracleTypes;
import org.junit.Test;
import demo.utils.JDBCUtils;
publicclassTestFunction{
/*
create or replace function queryempincoming(eno in number)
return number
* */
@Test
publicvoid testFunction(){
String sql="{call queryempincoming(?)}";
Connection conn =null;
CallableStatement call =null;
try{
//得到数据库连接
conn =JDBCUtils.getConnection();
call = conn.prepareCall(sql);
//对于输出函数,申明
call.registerOutParameter(1,OracleTypes.NUMBER);
//对于输入参数,赋值
call.setInt(2,7839);
//执行调用
call.execute();
//取出年收入结构
double income = call.getDouble(1);
System.out.println("该员工的年收入是:"+income);
}catch(Exception e){
e.printStackTrace();
}finally{
null);
}
}
2、测试结果
KING10100.0PRESIDENT