2008. 7. 26. 16:29
JDBC CallableStatment 예제
2008. 7. 26. 16:29 in 3. Implementation/DATABASE
1. PL/SQL (Procedural Language/SQL)
oracle에서 지원하는 프로그래밍 언어의 특성을 수용한 SQL의 확장이며, PL/SQL Block 내에서 SQL의 DML문과 SELECT문, 그리고 절차형 언어(IF, LOOP) 등을 사용하여 절차적 프로그래밍을 가능하게 하는 강력한 트랜잭션 언어이다.
2. Calc_Tax_Function.java
create or replace function calc_tax
(capital in number)
return number
is
tax number;
begin
tax:=capital * 0.05;
return (tax);
end;
\
(capital in number)
return number
is
tax number;
begin
tax:=capital * 0.05;
return (tax);
end;
\
<Calc_Tax_Function.java
01 import java.sql.*;
02
03 public class Calc_Tax_Function {
04 Connection con = null;
05 CallableStatement ctmt = null;
06
07 public static void main (String args []) {
08 Calc_Tax_Function ctf = new Calc_Tax_Function();
09 if(args.length < 1){
10 System.out.println
11 ("calc_tax FUNCTION에 전달할 값을 입력하세요.\n"
12 + "예를 들어, 1000의 값에 5% 세금을 계산하려면\n"
13 + "java Calc_Tax_Function 1000 을 입력하세요.");
14 return;
15 }
16 int capital = Integer.parseInt(args[0]);
17
18 try{
19 ctf.connect();
20 ctf.callFunction(capital);
21 ctf.close();
22 }catch(SQLException e){
23 e.printStackTrace();
24 }finally{
25 try{
26 if(ctf.ctmt != null) ctf.ctmt.close();
27 if(ctf.con != null) ctf.con.close();
28 }catch(SQLException e){}
29 }
30 }
31
32 public void connect() throws SQLException {
33 try{
34 Class.forName("oracle.jdbc.driver.OracleDriver");
35 con = DriverManager.getConnection
36 ("jdbc:oracle:thin:@127.0.0.1:1521:ora9",
37 "jdbc00","jdbc00");
38 System.out.println("데이터베이스 연결되었습니다.");
39 }catch(ClassNotFoundException e){
40 System.out.println("JDBC Driver load fail!!!");
41 }
42 }
43
44 public void close() throws SQLException {
45 con.close();
46 System.out.println("\n데이터베이스 연결해제되었습니다.");
47 }
48
49 public void callFunction(int capital) throws SQLException {
50 String query = "{? = call calc_tax(?)}";
51 ctmt = con.prepareCall(query);
52 ctmt.setInt(2, capital);
53 ctmt.registerOutParameter(1,Types.DOUBLE);
54 ctmt.execute();
55 double tax = ctmt.getDouble(1);
56 System.out.println
57 ("calc_tax 호출이 완료되었습니다.");
58 System.out.println
59 ("입력한 원금 " + capital + " 에 대한 세금 " + tax);
60 ctmt.close();
61 }
62 }
02
03 public class Calc_Tax_Function {
04 Connection con = null;
05 CallableStatement ctmt = null;
06
07 public static void main (String args []) {
08 Calc_Tax_Function ctf = new Calc_Tax_Function();
09 if(args.length < 1){
10 System.out.println
11 ("calc_tax FUNCTION에 전달할 값을 입력하세요.\n"
12 + "예를 들어, 1000의 값에 5% 세금을 계산하려면\n"
13 + "java Calc_Tax_Function 1000 을 입력하세요.");
14 return;
15 }
16 int capital = Integer.parseInt(args[0]);
17
18 try{
19 ctf.connect();
20 ctf.callFunction(capital);
21 ctf.close();
22 }catch(SQLException e){
23 e.printStackTrace();
24 }finally{
25 try{
26 if(ctf.ctmt != null) ctf.ctmt.close();
27 if(ctf.con != null) ctf.con.close();
28 }catch(SQLException e){}
29 }
30 }
31
32 public void connect() throws SQLException {
33 try{
34 Class.forName("oracle.jdbc.driver.OracleDriver");
35 con = DriverManager.getConnection
36 ("jdbc:oracle:thin:@127.0.0.1:1521:ora9",
37 "jdbc00","jdbc00");
38 System.out.println("데이터베이스 연결되었습니다.");
39 }catch(ClassNotFoundException e){
40 System.out.println("JDBC Driver load fail!!!");
41 }
42 }
43
44 public void close() throws SQLException {
45 con.close();
46 System.out.println("\n데이터베이스 연결해제되었습니다.");
47 }
48
49 public void callFunction(int capital) throws SQLException {
50 String query = "{? = call calc_tax(?)}";
51 ctmt = con.prepareCall(query);
52 ctmt.setInt(2, capital);
53 ctmt.registerOutParameter(1,Types.DOUBLE);
54 ctmt.execute();
55 double tax = ctmt.getDouble(1);
56 System.out.println
57 ("calc_tax 호출이 완료되었습니다.");
58 System.out.println
59 ("입력한 원금 " + capital + " 에 대한 세금 " + tax);
60 ctmt.close();
61 }
62 }
3. Calc_Tax_Procedure.java
create or replace procedure calc_tax_p
(capital in number, tax out number)
is
begin
tax:=capital * 0.05;
end;
\
(capital in number, tax out number)
is
begin
tax:=capital * 0.05;
end;
\
< Calc_Tax_Procedure.java >
01 import java.sql.*;
02
03 public class Calc_Tax_Procedure {
04 Connection con = null;
05 CallableStatement ctmt = null;
06
07 public static void main (String args []) {
08 Calc_Tax_Procedure ctp = new Calc_Tax_Procedure();
09 if(args.length < 1){
10 System.out.println
11 ("calc_tax_p PROCEDURE에 전달할 값을 입력하세요.\n"
12 + "예를 들어, 1000의 값에 5% 세금을 계산하려면\n"
13 + "java Calc_Tax_Procedure 1000 을 입력하세요.");
14 return;
15 }
16 int capital = Integer.parseInt(args[0]);
17
18 try{
19 ctp.connect();
20 ctp.callProcedure(capital);
21 ctp.close();
22 }catch(SQLException e){
23 e.printStackTrace();
24 }finally{
25 try{
26 if(ctp.ctmt != null) ctp.ctmt.close();
27 if(ctp.con != null) ctp.con.close();
28 }catch(SQLException e){}
29 }
30 }
31
32 public void connect() throws SQLException {
33 try{
34 Class.forName("oracle.jdbc.driver.OracleDriver");
35 con = DriverManager.getConnection
36 ("jdbc:oracle:thin:@127.0.0.1:1521:ora9",
37 "jdbc00","jdbc00");
38 System.out.println("데이터베이스 연결되었습니다.");
39 }catch(ClassNotFoundException e){
40 System.out.println("JDBC Driver load fail!!!");
41 }
42 }
43
44 public void close() throws SQLException {
45 con.close();
46 System.out.println("\n데이터베이스 연결해제되었습니다.");
47 }
48
49 public void callProcedure(int capital) throws SQLException {
50 String query = "{call calc_tax_p(?, ?)}";
51 ctmt = con.prepareCall(query);
52 ctmt.setInt(1, capital);
53 ctmt.registerOutParameter(2,Types.DOUBLE);
54 ctmt.execute();
55 double tax = ctmt.getDouble(2);
56 System.out.println
57 ("calc_tax_p 호출이 완료되었습니다.");
58 System.out.println
59 ("입력한 원금 " + capital + " 에 대한 세금 " + tax);
60 ctmt.close();
61 }
62 }
02
03 public class Calc_Tax_Procedure {
04 Connection con = null;
05 CallableStatement ctmt = null;
06
07 public static void main (String args []) {
08 Calc_Tax_Procedure ctp = new Calc_Tax_Procedure();
09 if(args.length < 1){
10 System.out.println
11 ("calc_tax_p PROCEDURE에 전달할 값을 입력하세요.\n"
12 + "예를 들어, 1000의 값에 5% 세금을 계산하려면\n"
13 + "java Calc_Tax_Procedure 1000 을 입력하세요.");
14 return;
15 }
16 int capital = Integer.parseInt(args[0]);
17
18 try{
19 ctp.connect();
20 ctp.callProcedure(capital);
21 ctp.close();
22 }catch(SQLException e){
23 e.printStackTrace();
24 }finally{
25 try{
26 if(ctp.ctmt != null) ctp.ctmt.close();
27 if(ctp.con != null) ctp.con.close();
28 }catch(SQLException e){}
29 }
30 }
31
32 public void connect() throws SQLException {
33 try{
34 Class.forName("oracle.jdbc.driver.OracleDriver");
35 con = DriverManager.getConnection
36 ("jdbc:oracle:thin:@127.0.0.1:1521:ora9",
37 "jdbc00","jdbc00");
38 System.out.println("데이터베이스 연결되었습니다.");
39 }catch(ClassNotFoundException e){
40 System.out.println("JDBC Driver load fail!!!");
41 }
42 }
43
44 public void close() throws SQLException {
45 con.close();
46 System.out.println("\n데이터베이스 연결해제되었습니다.");
47 }
48
49 public void callProcedure(int capital) throws SQLException {
50 String query = "{call calc_tax_p(?, ?)}";
51 ctmt = con.prepareCall(query);
52 ctmt.setInt(1, capital);
53 ctmt.registerOutParameter(2,Types.DOUBLE);
54 ctmt.execute();
55 double tax = ctmt.getDouble(2);
56 System.out.println
57 ("calc_tax_p 호출이 완료되었습니다.");
58 System.out.println
59 ("입력한 원금 " + capital + " 에 대한 세금 " + tax);
60 ctmt.close();
61 }
62 }