oracle存儲過程jdbc調用


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();
        }
    }
}

 


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM