오라클 디비 연동하여 프로시져호출 예제
프로시저는 복잡한 sql 명령을 함수처럼 사용하기 위하여 작성됨
import java.util.*;
import java.sql.*;
public class Exam10 {
public static void main(String[] ar){
try{
Class.forName("oracle.jdbc.driver.OracleDriver"); // 오라클 드라이버
}catch(Exception e){
System.err.println(e);
System.exit(1);
}
Connection conn = null;
CallableStatement cstmt = null;
String url = "jdbc:oracle:thin:@localhost:1521:XE";
String id = "System";
String pass= "1423";
String query = null;
try{
conn = DriverManager.getConnection(url,id,pass);
}catch(Exception e){
System.err.println(e);
System.exit(1);
}
// ------------- Databases 접속 ----------------//
Scanner sc = new Scanner(System.in);
System.out.println("누구의 나이를 알고 싶나요?");
String name = sc.next();
query = "{call myproc(?,?) }"; // 프로시저 호출 단계
try{
cstmt = conn.prepareCall(query);
cstmt.setString(1, name);
cstmt.registerOutParameter(2, Types.INTEGER); //Types 를 이용하여 out 객체 타입 결정해줌
cstmt.executeQuery();
int age = cstmt.getInt(2);
System.out.println("나이는 : " + age);
cstmt.close();
conn.close();
}catch(SQLException e){
System.err.println(e);
}
}
}
프로시저 내용
create or replace procedure myproc(n in varchar2, a out int) Is begin
select age into a from Person where name = n;
end