jdbc调用存储过程和函数

1.调用存储过程

public class CallOracleProc {
    public static void main(String[] args) throws Exception{
        String sql = "{call get_rax(?,?)}";
        Connection conn = JdbcUtil.getConnection();
        CallableStatement cstmt = conn.prepareCall(sql);
        //为第一个?号设置值,从1开始
        cstmt.setInt(1,7000);
        //为第二个?注册输出类型
        cstmt.registerOutParameter(2,Types.INTEGER);
        //执行调用过程
        cstmt.execute();
        //接收过程的返回值,即第二个?号
        int rax = cstmt.getInt(2);
        //显示
        System.out.println("7000元需要上交"+rax+"元税收");
        cstmt.close();
        conn.close();
    }
}

2.调用函数

public class CallOracleFunc {
    public static void main(String[] args) throws Exception{
        String sql = "{ ? = call findEmpNameAndJobAndSal(?,?,?) }";
        Connection conn = JdbcUtil.getConnection();
        CallableStatement cstmt = conn.prepareCall(sql);
        
        //为第一个?注册输出类型
        cstmt.registerOutParameter(1,Types.VARCHAR);
        //为第二个?注入值
        cstmt.setInt(2,7788);
        //为第三个?注册输出类型
        cstmt.registerOutParameter(3,Types.VARCHAR);
        //为第四个?注册输出类型
        cstmt.registerOutParameter(4,Types.INTEGER);
        //执行函数调用
        cstmt.execute();
        //分别获取1,3,4占位符的值
        String ename = cstmt.getString(1);
        String job = cstmt.getString(3);
        int sal = cstmt.getInt(4);
        //显示
        System.out.println("7788--"+ename+"--"+job+"--"+sal);
        cstmt.close();
        conn.close();
    }
}
原文地址:https://www.cnblogs.com/juaner767/p/5770141.html