JDBC调用存储过程

时间:2021-04-13 04:26:31

1.oracle   Package包名EHIS.JH_INTERFACE_PKG

请求参数    P_LEDTYPE        Number,         LED类型 1是女科,2是男科
X_REF    OUT SYS_REFCURSOR     返回的数据集合

 

2

package pojo;

import oracle.jdbc.OracleTypes;

import java.sql.*;
import java.util.ArrayList;

public class JdbcProcedureTest {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        ArrayList<ClinicJhlish> list=new ArrayList<ClinicJhlish>();

        String driver = "oracle.jdbc.driver.OracleDriver";

        /*String url = "jdbc:mysql://127.0.0.1:3306/ump?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE";*/

        String url = "jdbc:oracle:thin:@//192.168.6.240:1521/hissdbs";
        String user = "Oracle";
        String password = "root123";
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, user, password);

        String sql = "{call EHIS.JH_INTERFACE_PKG.QUERY_CLINICJHLIST(?,?)}";/*EHIS.JH_INTERFACE_PKG 包名*/
        CallableStatement stmt = conn.prepareCall(sql);//jdbc执行过程//调用格式 {call 存储过程名(参数)}

        stmt.setObject(1,1);//索引1,第1个id LED类型
        /*stmt.setObject(3, "3");*/

        /*stmt.registerOutParameter(1, Types.VARCHAR);*/
        /*返回类型例子
        stmt.registerOutParameter(4, java.sql.Types.VARCHAR);   //注册返回类型(sql类型),输出参是(vresult out varchar2)
        Object objRtn = stmt.getObject(4);      //得到返回值*/
        stmt.registerOutParameter(2, OracleTypes.CURSOR);//输出参数的话要注册 oracle返回集合 X_REF    OUT SYS_REFCURSOR   返回的数据集合
        stmt.execute();//注册后要更新
        ResultSet resultSet = (ResultSet) stmt.getObject(2);//这个是索引的意思
        while (resultSet.next()){
            ClinicJhlish cj=new ClinicJhlish();
            cj.setCALLID(resultSet.getString("CALLID"));
            cj.setROOMID(resultSet.getString("ROOMID"));
            System.out.println(resultSet.getString("CALLID")+":"+resultSet.getString("ROOMID"+""));
            list.add(cj);
        }

    }

}

 3

package com.jckb.procedure;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;

public class MainTest  {
    /**
     * 存储过程plsql
        create or replace procedure findEmpInfo(pno in number,pname out varchar2,psal out number)
        as
        begin
          select ename,sal into pname,psal from emp where empno=pno;
        end;
        /
     * */
    public static void main(String[] args) {
        //1、定义变量
        String driverClass = "oracle.jdbc.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String user = "scott";
        String password = "tiger";
        try{
        Class.forName(driverClass);
        //2、获取连接对象
        Connection connection = DriverManager.getConnection(url, user, password);
        //3、创建执行存储过程的语句对象
        String sql = "{call findEmpInfo(?,?,?)}";
        CallableStatement callableStatement = connection.prepareCall(sql);
        //4、设置参数
        callableStatement.setInt(1, 7902);
        callableStatement.registerOutParameter(2, oracle.jdbc.OracleTypes.VARCHAR);
        callableStatement.registerOutParameter(3, oracle.jdbc.OracleTypes.NUMBER);
        //5、执行
        callableStatement.execute();
        //6、获取数据
        String ename = callableStatement.getString(2);
        double sal = callableStatement.getDouble(3);
        System.out.println("姓名:"+ename+" 员工工资:"+sal);
        //7、释放资源
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

 

4

JDBC调用存储过程