关闭语句java时的Code Logic

时间:2013-10-25 14:32:18

标签: oracle jdbc database-connection resultset

总是教我这种从数据库开放和关闭连接的方式,然后我越来越多地搜索,因为这对我的应用程序的性能非常重要。

这是我的班级连接

public class Connection {

jdbc:oracle:thin:@//xxx.xx.x.xxx:xxxx/xxxxx.xxxxxx.xxxxx.xxx;
protected static Connection cn = null;

protected Connection getCn() {
    return cn;
}

public static void setCn(Connection cn) {
    Connection.cn = cn;
}

public ResultSet select(String sql) throws Exception {
    ResultSet rs = null;
    Statement st = null;
    try {
        st = this.getCn().createStatement();
        rs = st.executeQuery(sql);
    } catch (java.sql.SQLException e) {
        throw e;
    }
    return rs;
}

public void insert(String sql) throws Exception {
    Statement st = null;
    try {
        st = this.getCn().createStatement();
        st.executeUpdate(sql);
    } catch (java.sql.SQLException e) {
        throw e;
    }
}

public Connection connect() throws Exception {        
    try {
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        setCn(DriverManager.getConnection(DBURL, "user", "password"));
    } catch (java.sql.SQLException e) {
        throw e;
    }
    return cn;
}

那是我的Connection Class,现在我有一些其他的类从我的Connection类扩展,以便从DataBase中获取数据。

public String checkMethod() throws Exception {
    ResultSet rs;
    String sql = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    try {
        this.connect();
        rs = this.select(sql);
        while (rs.next()) {
            //some data collect
        }
        rs.close();   //here is my dude because when may i can put the statement.close() line?         
    } catch (Exception e) {
        throw e;
    } finally {
        this.cerrar();
    }
    return "success";
}

我正在使用jsf和oracle,我认为这个片段应该在我的类Connection之后但是生成我并且当我执行方法rs.next()并且是逻辑因为语句必须是逻辑时结果的错误被关闭在读取dataSet的数据后关闭,那么如何在我的类Connection或其他地方关闭语句?有什么建议?请帮帮我

finally {
        if (st != null) {
            st.close();
        }
}

3 个答案:

答案 0 :(得分:0)

问题的快速解决方法是:

finally {
   try{ st.close(); }catch( Exception ex ) { /* do nothing*/ }
}

这可以防止在代码的其他位置出现错误时出现错误(st为null,st为关闭等)。


更优雅的解决方案可能是使用关闭语句,结果集等的方法创建一个帮助器类,并隐藏发生的异常:

class DbCloser{
  static void closeQuietly( Statement st ){
     try{
       st.close();
     } catch( Exception ex ){
        /* do nothing */
     }
  }

  static void closeQuietly( ResultSet rs ){
     try{
       rs.close();
     } catch( Exception ex ){
        /* do nothing */
     }
  }
  // .... etc.
}

然后在代码中的finally块中使用该辅助类:

finally {
   DbCloser.closeQuietly( st ); 
}

Appache Commons中有现成的DbUtils软件包已经实现了这样的帮助方法,只需下载此库并将其放在类路径中,详细信息请参见此链接:
http://commons.apache.org/proper/commons-dbutils/
http://commons.apache.org/proper/commons-dbutils/apidocs/index.html


最后,我建议仅在close块中放置finnaly方法:

  try {
       ......
        rs = this.select(sql);
       .......
       ...........
        // Do not close the statement here ......
        // rs.close();   //here is my dude because when may i can put the statement.close() line?         
    } catch (Exception e) {
        throw e;
    } finally {
        // ..... always close it here !!!
        DbUtils.closeQuietly( st );
        .........
    }

答案 1 :(得分:0)

我用这个

finally {
   try{ 
     if(st!=null){
        st.close();
        st=null;
     }
   }catch( Exception ex ) 
   {
     /* you can log this*/ 
   }

}

答案 2 :(得分:0)

最后,我解决了这个问题:

public String checkMethod() throws Exception {
    ResultSet rs;
    String sql = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    try {
        this.connect();
        rs = this.select(sql);
        while (rs.next()) {
            //some data collect
        }
        rs.close(); 
        rs.getStatement().close(); 'This works for me =) 
    } catch (Exception e) {
        throw e;
    } finally {
        this.cerrar();
    }
    return "success";
}