728x90
반응형
FUCTION
CREATE OR REPLACE FUNCTION TEST_FUNC(p_n1 EMP.ENAME%TYPE) RETURN DEPT.DNAME%TYPE IS V_DNAME DEPT.DNAME%TYPE; V_DEPTNO EMP.DEPTNO%TYPE; BEGIN SELECT DEPTNO INTO V_DEPTNO FROM EMP WHERE ENAME = p_n1; SELECT DNAME INTO V_DNAME FROM DEPT WHERE DEPTNO = V_DEPTNO; RETURN V_DNAME; END;
package dbc7; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; public class FunctionCall { public static void main(String[] args) { try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "test", "1111"); CallableStatement cstmt = con.prepareCall("{ ?=call TEST_FUNC(?)}"); cstmt.registerOutParameter(1, Types.VARCHAR); cstmt.setString(2, "TURNER"); // ?에 값 바인딩 cstmt.executeUpdate(); // cstmt.executeQuery(); //사용가능 // cstmt.execute(); //사용가능 System.out.println(cstmt.getString(1)); // 사용한 객체 닫기 PreparedStatement ps = con.prepareStatement("SELECT TEST_FUNC(?) FROM DUAL"); ps.setString(1, "TURNER"); ResultSet rs = ps.executeQuery(); while (rs.next()) { System.out.println("부서명: " + rs.getString(1)); } rs.close(); ps.close(); cstmt.close(); con.close(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
728x90
반응형
'[JDBC]' 카테고리의 다른 글
[JDBC] PROCEDURE (0) | 2024.02.02 |
---|---|
[JDBC] TRANJACTION - COMMIT, ROLLBACK, 원자성 (0) | 2024.02.01 |
[JDBC] ex) [회원정보테이블] (0) | 2024.01.31 |
[JDBC] DAO, DTO (0) | 2024.01.31 |
[JDBC] Ex (0) | 2024.01.31 |