IT_Programming/Java

JAVA로 stored Procedure 호출하기 (with mssql 2000)

JJun ™ 2008. 5. 14. 00:10

[CallableStatementTest.java]

 

import java.sql.*;

public class CallableStatementTest
{
 public static void main(String[] args)
 {
  try{
     String _driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver",
        _url = "jdbc:microsoft:sqlserver://127.0.0.1:1432;DatabaseName=Test",
        _user = "****",
        _password = "****";
    
           Class.forName(_driver);
           Connection con = DriverManager.getConnection(_url, _user, _password);
           CallableStatement cs = con.prepareCall("{call myStoredProcedure(?,?,?)}");
           cs.setInt(1, 27); // 세팅할 값
           cs.registerOutParameter(2, java.sql.Types.VARCHAR);
// 받아오는 값들 타입 설정..
           cs.registerOutParameter(3, java.sql.Types.INTEGER); // 받아오는 값들 타입 설정..
           cs.execute(); // 실행

           System.out.println("나이가 27세인 사람의 이름은 '"+ cs.getString(2) + "'이고, 나이는 : '"+ cs.getInt(3)+"'세 입니다.");
           cs.close();
           con.close();
        }catch(Exception e)
        {System.out.println(e);}
  }
}

 

 

 

[Stored Procedure (myStoredProcedure)]

 

CREATE PROCEDURE  myStoredProcedure

   @age  int

,   @na varchar(20)  OUTPUT

,   @ageo int           OUTPUT

AS
SELECT  @na = name, @ageo = age  FROM mytest

Where age = @age

 

 

[DB: Test, Table name: mytest]