Java 호출 Oracle 스토리지 프로세스 상세 정보

5600 단어
Java 호출 Oracle 스토리지 프로세스 상세 정보
단계:
1. Oracle 스토리지 프로세스 작성
2. 데이터베이스 작성 연결 도구 클래스 얻기
3. 간단한 응용 프로그램 호출 저장 프로세스를 작성한다.
구현:
1. Oracle 스토리지 프로세스:

/*   */
create table test(
  id varchar2(32),
  name varchar2(32)
);

/*         */  
CREATE OR REPLACE PROCEDURE insert_procedure(
  PARA1 IN VARCHAR2,
  PARA2 IN VARCHAR2
) AS
BEGIN
 INSERT INTO test (id, name) VALUES (PARA1, PARA2);
END insert_procedure;

/*          */
CREATE OR REPLACE PROCEDURE select_procedure(
  para_id IN VARCHAR2,
  name OUT sys_refcursor /*   sys_refcursor   SYS.STANDARD   */
) AS
BEGIN
 OPEN name FOR
  SELECT * FROM test WHERE id = para_id;
END;


2, JDBC 도구 클래스

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {
  public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
  public static final String URL = "jdbc:oracle:thin:@localhost:1521/orcl";
  public static final String USERNAME = "pfm";
  public static final String PASSWORD = "pfm";

  /**
   *                
   */
  static {
    try {
      Class.forName(DRIVER);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }

  /**
   *   Connection
   * 
   * @return
   */
  public static Connection getConnection() {
    Connection conn = null;
    try {
      conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return conn;
  }

  /**
   *   Statement
   * 
   * @return
   */
  public static Statement getStatement() {
    Statement st = null;
    try {
      st = getConnection().createStatement();
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return st;
  }

  /**
   *   ResultSet
   * 
   * @param rs
   */
  public static void closeResultSet(ResultSet rs) {
    if (rs != null) {
      try {
        rs.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }

  /**
   *   Statement
   * 
   * @param st
   */
  public static void closeStatement(Statement st) {
    if (st != null) {
      try {
        st.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }

  /**
   *   Connection
   * 
   * @param conn
   */
  public static void closeConnection(Connection conn) {
    if (conn != null) {
      try {
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }

  /**
   *     
   * 
   * @param rs
   * @param sta
   * @param conn
   */
  public static void closeAll(ResultSet rs, Statement sta, Connection conn) {
    closeResultSet(rs);
    closeStatement(sta);
    closeConnection(conn);
  }

}


3. 저장 프로세스를 호출한다.

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.driver.OracleTypes;

/**
 *         
 * 
 */
public class StoredTest {
  public static void main(String[] args) {
    insert_call();
    //select_call();
  }

  /**
   *            
   */
  public static void insert_call() {
    Connection conn = DBUtil.getConnection();
    PreparedStatement pst = null;

    CallableStatement proc = null; //            
    try {
      proc = conn.prepareCall("{ call insert_procedure(?,?) }");
      proc.setString(1, "1"); //          
      proc.setString(2, "hello call"); //          
      proc.execute();//   

    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      try {
        //   IO 
        proc.close();
        DBUtil.closeAll(null, pst, conn);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  /**
   *            
   */
  public static void select_call() {
    Connection conn = DBUtil.getConnection();

    CallableStatement stmt;
    try {
      stmt = conn.prepareCall("{ call select_procedure(?, ?) }"); //               
      stmt.setString(1, "1");
      stmt.registerOutParameter(2, OracleTypes.CURSOR);
      stmt.execute();
      ResultSet rs = (ResultSet) stmt.getObject(2);
      while (rs.next()) {
        System.out.println(rs.getString("name"));
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      DBUtil.closeConnection(conn);
    }
  }
}


읽어주셔서 감사합니다. 여러분께 도움이 되었으면 좋겠습니다. 본 사이트에 대한 지지에 감사드립니다!

좋은 웹페이지 즐겨찾기