SQL Server 함수 호출

사용 가능

CallableStatement

JDBC에는 훨씬 더 복잡한 시나리오를 위한 또 다른 인터페이스가 있습니다. 이것은 PreparedStatement 에서 상속되며 CallableStatement 라고 합니다 .

데이터베이스의 저장 프로시저를 호출(호출)하는 데 사용됩니다. 이러한 호출의 특징은 ResultSet 결과 외에도 매개변수를 이러한 저장 프로시저에 전달할 수 있다는 것입니다.

새로운 것이 무엇입니까? PreparedStatement 에는 ResultSet 결과 도 있으며 여기에 매개변수를 전달할 수도 있습니다. 예, 맞습니다. 하지만 저장 프로시저의 특징은 매개 변수를 통해 데이터를 수신할 수 있을 뿐만 아니라 반환할 수도 있다는 것입니다.

저장 프로시저는 IN , OUTINOUT 매개변수 로 호출됩니다 . 하나 이상의 ResultSet 객체를 반환합니다 . Connection.prepareCall() 메서드는 CallableStatement 객체를 만드는 데 사용됩니다 .

여기에서 매개변수 a, b 및 c를 허용하는 저장 프로시저 ADD가 있다고 가정합니다. 이 절차는 a와 b를 더하고 더한 결과를 변수 c에 배치합니다.

호출을 시도할 코드를 작성해 보겠습니다.

// Connect to the server
Connection connection = DriverManager.getConnection("jdbc:as400://mySystem");

// Create a CallableStatement object. It does preprocessing
// calling a stored procedure. Question marks
// indicate where the input parameters should be substituted, and where the output ones
// The first two parameters are input,
// and the third one is a day off.
CallableStatement statement = connection.prepareCall("CALL MYLIBRARY.ADD (?, ?, ?)");

// Setting up input parameters. Passing 123 and 234 to the procedure
statement.setInt (1, 123);
statement.setInt (2, 234);

// Registering the output parameter type
statement.registerOutParameter (3, Types.INTEGER);

// Run stored procedure
statement.execute();

// Get the value of the output parameter
int sum = statement.getInt(3);

// Close CallableStatement and Connection
statement.close();
connection.close();

작업은 PreparedStatement 와 거의 비슷 하지만 뉘앙스가 있습니다. ADD 함수는 세 번째 매개변수에 더한 결과를 반환합니다. CallableStatement 객체 만이 이것에 대해 아무것도 모릅니다. 따라서 registerOutParameter() 메서드 를 호출하여 이를 명시적으로 알립니다 .

registerOutParameter(parameter number, Parameter type)

그런 다음 execute() 메서드를 통해 프로시저를 호출한 다음 getInt() 메서드를 사용하여 세 번째 매개 변수에서 데이터를 읽을 수 있습니다 .

일괄 처리 요청

실제 프로젝트에서는 동일한 유형의 쿼리를 많이 만들어야 하는 상황이 자주 발생합니다(이 경우 가장 일반적인 쿼리는 PreparedStatement ). 예를 들어 수십 또는 수백 개의 레코드를 삽입해야 합니다.

각 요청을 개별적으로 실행하면 시간이 많이 걸리고 애플리케이션의 성능이 저하됩니다. 이를 방지하기 위해 일괄 삽입 모드를 사용할 수 있습니다. 요청과 함께 일부 버퍼를 축적한 다음 즉시 실행한다는 사실에 있습니다.

다음은 예제 코드입니다.

PreparedStatement stmt = con.prepareStatement(
        	"INSERT INTO jc_contact (first_name, last_name, phone, email) VALUES (?, ?, ?, ?)");

for (int i = 0; i < 10; i++) {
	// Fill in the request parameters
	stmt.setString(1, "FirstName_" + i);
    stmt.setString(2, "LastNAme_" + i);
    stmt.setString(3, "phone_" + i);
    stmt.setString(4, "email_" + i);
	// The request is not executed, but fits into the buffer,
	// which is then executed immediately for all commands
	stmt.addBatch();
}
// Execute all requests at once
int[] results = stmt.executeBatch();

execute() 메서드 로 쿼리를 실행하는 대신 addBatch() 메서드 로 일괄 처리합니다 .

그런 다음 수백 개의 요청이 있을 때 executeBatch() 명령을 호출하여 한 번에 서버로 보낼 수 있습니다 .

건강한. executeBatch() 메서드는 정수 배열인 int[]를 반환합니다. 이 배열의 각 셀에는 해당 쿼리에서 수정한 행 수를 나타내는 숫자가 포함되어 있습니다. 배치에서 요청 번호 3이 5개 ​​행을 변경한 경우 배열의 세 번째 셀에는 숫자 5가 포함됩니다.

코멘트
  • 인기
  • 신규
  • 이전
코멘트를 남기려면 로그인 해야 합니다
이 페이지에는 아직 코멘트가 없습니다