0

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

这是我的班级联系

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 类,现在我有一些其他类从我的 Connection 类扩展来为我带来数据库中的数据。

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() 时结果集的错误被关闭并且是逻辑,因为语句必须在之后关闭读取结果集的数据,那么如何在我的类 Connection 或其他地方关闭语句?有什么建议么?请帮我

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

3 回答 3

0

A quick fix to the problem is:

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

this prevents from throwing an error when something goes wrong with the statement in other places of the code ( st is null, st is closed etc.).


A more elegant solution might be creating a helper class with methods that close statements, resultsets etc. and hide exceptions that occur:

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.
}

and then use that helper class in finally blocks in code:

finally {
   DbCloser.closeQuietly( st ); 
}

There is ready-made DbUtils package from Appache Commons that has already implemented such helper methods, just download this library and place it in the class-path, see this links for details:
http://commons.apache.org/proper/commons-dbutils/
http://commons.apache.org/proper/commons-dbutils/apidocs/index.html


And finally I would suggest to place close methods only in finnaly blocks:

  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 );
        .........
    }
于 2013-10-26T07:33:57.600 回答
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";
}
于 2013-10-29T15:07:09.250 回答
0

我用这个

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

}
于 2013-10-29T12:42:45.950 回答