2014-10-02 2 views
3

PostgreSQL 데이터베이스에서 저장 프로 시저를 실행하기 위해 Java 준비 문을 사용하고 있습니다. 이와 같이 :결과 세트가없는 Java 준비된 명령문?

String sql = "select testFkt(?,?,?,?,?)"; 
    try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { 
     preparedStatement.setInt(1, a 
     preparedStatement.setInt(2, b); 
     preparedStatement.setInt(3, c); 
     preparedStatement.setByte(4, d); 
     preparedStatement.setString(5, "test"); 
     try (ResultSet result = preparedStatement.executeQuery()) { 

     } 
    } 

저장 프로 시저가 결과를 반환하지만 결과에 관심이 없습니다.

그래도 ResultSet (및 시도해 사용)을 사용해야합니까, 아니면 preparedStatement.executeQuery()을 사용할 수 있습니까?

저장 프로 시저가 결과를 반환하고 내가 ResultSet을 사용하지 않을 때이 스트림이 닫히지 않기 때문에 열리는 것과 같은 스트림이 있다는 것에 두려움이 있습니다.

+1

때때로 (데이터베이스 및 특성에 따라 다름) 스토어드 프로 시저), 실제로 ** 모든 ** 작업을 실제로 수행하기 위해 스토어드 프로 시저에 대한 ** 전체 ** 결과 세트를 읽어야합니다. –

답변

1

질문을 이해하면 PreparedStatmenet.execute() 대신에 ResultSet을 사용할 수 있습니다. 인

는 그것을 생성 한 정책 객체가 폐쇄 될 때이

try (ResultSet result = preparedStatement.executeQuery()) { 
} 

preparedStatement.execute(); 
0

예, 여전히 결과 집합을 닫아야합니다.

1

에이 ResultSet 객체 자동 폐쇄 변경. ResultSet Stream을 닫으려면 명령문을 닫을 수 있습니다.

자세한 내용

에 대한 방문 http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html하지만 귀하의 요구 사항은 자세한 내용

샘플 아래에 대한 JDBC 호출 가능 계산서 API

방문 http://docs.oracle.com/javase/7/docs/api/java/sql/CallableStatement.html를 사용할 수있는 저장 프로 시저를 실행하는 경우 :

Connection dbConnection = null;   
CallableStatement callableStatement = null; 
String storedProc = "{call storedProc (?,?)}"; 
    try{ 
     dbConnection = getDBConnection(); 
     callableStatement = dbConnection.prepareCall(storedProc); 
     callableStatement.setInt(1, 1); 
     callableStatement.registerOutParameter(2, java.sql.Types.DATE); 
     callableStatement.executeUpdate();          
    } catch(SQLException e) { 
     //handle exception 
    } finally {    
     if (callableStatement != null) {   
      callableStatement.close();   
     }    
     if (dbConnection != null) { 
      dbConnection.close();   
     }   
} 
관련 문제