好吧,总是教我这种从数据库打开和关闭连接的方式,然后我搜索得越来越多,因为这对我的应用程序的性能非常重要。
这是我的班级联系
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();
}
}