前提:在数据库中已创建了存储函数 queryEmpIncome
上一篇:储存函数的创建
用到的包为:ojdbc14-9.0.2.0.0.jar
关键代码:
import java.sql.CallableStatement;
import java.sql.Connection;
import org.junit.Test;
import oracle.jdbc.OracleTypes;
import util.JDBCUtil;
public class TestFunction {
//储存函数: 此函数已定义在数据库中 FUNCTION queryEmpIncome(eno in number)
//语法: {?= call <procedure-name>[(<arg1>,<arg2>, ...)]}
@Test
public void test1(){
Connection conn = null;
CallableStatement call = null;
String sql = "{? = call queryEmpIncome(?)}";
try {
conn = JDBCUtil.getConnection();
call = conn.prepareCall(sql);
//对于输出参数,声明
call.registerOutParameter(1, OracleTypes.NUMBER);
//对于输入参数,赋值
call.setInt(2,7788);
//指定调用
call.execute();
int income = call.getInt(1);
System.out.println("年收入: " + income);
} catch (Exception e) {
e.printStackTrace();
}finally{
//释放资源
JDBCUtil.release(conn, call, null);
}
}
}