正常关闭JDBC的多个Statement

    技术2022-05-19  22

    Java代码 

    private PreparedStatement psStmt1;   

    private PreparedStatement psStmt2;   

    private PreparedStatement psStmt3;   

     

    ........................   

    ........................   

     

    /*  关闭代码  */  

    public void cleanup() throws SQLException {   

      SQLException exception = null;   

      if (psStmt1 != null) {   

        try {   

          psStmt1.close();   

        } catch (SQLException e) {   

          exception = e;   

        } finally {   

          psStmt1 = null;   

        }   

      }   

     

      if (psStmt2 != null) {   

        try {   

          psStmt2.close();   

        } catch (SQLException e) {   

          if (exception != null) e.setNextException(exception);   

          exception = e;   

        } finally {   

          psStmt2 = null;   

        }   

      }   

     

      if (psStmt3 != null) {   

        try {   

          psStmt3.close();   

        } catch (SQLException e) {   

          if (exception != null) e.setNextException(exception);   

          exception = e;   

        } finally {   

          psStmt3 = null;   

        }   

      }   

     

      if (exception != null) {   

        throw exception;   

      }   

    }  

     

    private PreparedStatement psStmt1;

    private PreparedStatement psStmt2;

    private PreparedStatement psStmt3;

     

    ........................

    ........................

     

    /*  关闭代码  */

    public void cleanup() throws SQLException {

      SQLException exception = null;

      if (psStmt1 != null) {

        try {

          psStmt1.close();

        } catch (SQLException e) {

          exception = e;

        } finally {

          psStmt1 = null;

        }

      }

     

      if (psStmt2 != null) {

        try {

          psStmt2.close();

        } catch (SQLException e) {

          if (exception != null) e.setNextException(exception);

          exception = e;

        } finally {

          psStmt2 = null;

        }

      }

     

      if (psStmt3 != null) {

        try {

          psStmt3.close();

        } catch (SQLException e) {

          if (exception != null) e.setNextException(exception);

          exception = e;

        } finally {

          psStmt3 = null;

        }

      }

     

      if (exception != null) {

        throw exception;

      }


    最新回复(0)