2011-02-10 18 views
4

PreparedStatementを作成して何度か再利用し、それをクリーンアップする正しい方法は何ですか?PreparedStatementインスタンスの再利用の正しい方法は?

Connection conn = null; 
PreparedStatement stmt = null; 

try { 
    conn = getConnection(...); 

    // first use 
    stmt = conn.prepareStatement("some statement ?"); 
    stmt.setString(1, "maybe some param"); 
    if (stmt.execute()) { 
     ... 
    } 

    // second use 
    stmt = conn.prepareStatement("some statement ?"); 
    stmt.setString(1, "maybe some param"); 
    if (stmt.execute()) { 
     ... 
    } 

    // third use. 
    stmt = conn.prepareStatement("some statement"); 
    stmt.execute(); 
} 
finally { 
    if (stmt != null) { 
     try { 
      stmt.close(); 
     } catch (Exception sqlex) { 
      sqlex.printStackTrace(); 
     } 

     stmt = null; 
    } 

    if (conn != null) { 
     try { 
      conn.close(); 
     } catch (Exception sqlex) { 
     sqlex.printStackTrace(); 
     } 

     conn = null; 
    } 
} 

は、我々はそのような「stmtは」オブジェクトを再利用することができ、または我々は(stmt.closeを呼び出す必要があります)各クエリ間:私は、次のパターンを使用していますか?

おかげ

----------更新------------------------

ああOK私の声明はそれぞれ異なるでしょう。これはより正しいパターンですか?

Connection conn = null; 
PreparedStatement stmt = null; 

try { 
    conn = getConnection(...); 

    // first use 
    PreparedStatement stmt1 = null; 
    try { 
     stmt1 = conn.prepareStatement("some statement ?"); 
     stmt1.setString(1, "maybe some param"); 
     if (stmt1.execute()) { 
      ... 
     } 
    } 
    finally { 
     if (stmt1 != null) { 
      try { 
       stmt1.close(); 
      } catch (Exception ex) {} 
     } 
    } 

    // second use 
    PreparedStatement stmt2 = null; 
    try { 
     stmt2 = conn.prepareStatement("some different statement ?"); 
     stmt2.setString(1, "maybe some param"); 
     if (stmt2.execute()) { 
      ... 
     } 
    } 
    finally { 
     if (stmt2 != null) { 
      try { 
       stmt2.close(); 
      } catch (Exception ex) {} 
     } 
    } 

    // third use 
    PreparedStatement stmt3 = null; 
    try { 
     stmt3 = conn.prepareStatement("yet another statement ?"); 
     stmt3.setString(1, "maybe some param"); 
     if (stmt3.execute()) { 
      ... 
     } 
    } 
    finally { 
     if (stmt3 != null) { 
      try { 
       stmt3.close(); 
      } catch (Exception ex) {} 
     } 
    } 
} 
finally { 
    if (conn != null) { 
     try { 
      conn.close(); 
     } catch (Exception sqlex) { 
     sqlex.printStackTrace(); 
     } 

     conn = null; 
    } 
} 

したがって、次の文が実行される前に、それぞれの文は個別に閉じられます。

+0

[PreparedStatementを何度も再利用する](http://stackoverflow.com/questions/2467125/reusing-a-preparedstatement-multiple-times) – BalusC

+1

[継続的に準備されたステートメント - 良い方法](http:///stackoverflow.com/questions/7245406/consecutive-preparedstatement-good-practice)、これはおそらくあなたが探しているものです。 – Miranda

答えて

5

これは別の方法です。一度だけ準備してから再利用する必要があります。

I.E.この:

// second use 
    stmt = conn.prepareStatement("some statement ?"); 
    stmt.setString(1, "maybe some param"); 
    if (stmt.execute()) { 
     ... 
    } 

このなる必要があります。

// second use 
    stmt.setString(1, "maybe some param"); 
    if (stmt.execute()) { 
     ... 
    } 

異なる文であるあなたの第三の使用は、いずれかの新しい変数であるか、または最初にあなたのプリペアドステートメントを閉じる必要があります。 (通常はPreparedStatementsを使用しますが、それらを保持して再利用します)。

関連する問題