protected interface RestSet{
public void close();
public boolean next();
public String getString(String s) throws SQLException;
public String getString(int i) throws SQLException;
public int count();
}
protected RestSet _select(final String sql){
try {
return new RestSet(){
Statement __stt=__conn.createStatement();
ResultSet __rs=__stt.executeQuery(sql); public void close(){
try{
if(__rs!=null){
__rs.close();
}
if(__stt!=null){
__stt.close();
}
} catch(SQLException e){
System.out.println("语句或结果集关闭失败");
e.printStackTrace();
}
} public boolean next(){
try {
return __rs.next();
} catch (SQLException e) {
//不处理
}
return false;
}
public String getString(String s) throws SQLException{
return __rs.getString(s);
} public String getString(int i) throws SQLException{
return __rs.getString(i);
}
public int count(){
try {
__rs.last();
int r=__rs.getRow();
__rs.beforeFirst();
return r;
} catch (SQLException e) {
//不处理
}
return 0;
}
};
} catch (SQLException e) {
System.out.println(sql);
e.printStackTrace();
}
return null;
}
我写数据库操作的时候对于Statement和Resultset的释放比较头疼,想写一个这样的好用的方法。但是呢,调用close时貌似资源不释放啊,执行没有错误,在eclipse下debug提示求值期间出错,但是执行的时候没有异常,但是就是内存暴涨,求大牛们发表意见。
解决方案 »
- Java使用.cer文件中的公钥对密码加密
- 小妹求一简单的关于list的
- 100求osworkflow完整的例子 要可以运行地
- tomcat多域名的问题
- jfreechart配置问题
- KPI统计问题
- 为什么str.replaceAll("location in ('HK')","location not in ('HK','TP')");不能替换
- 学习J2EE之前要有哪些预备知识啊?
- 有人具体在项目中用J2EE吗?有没有使用JAAS这种安全认证授权体系阿?(跟贴有分!)
- XML入门问题:这两段XML有什么区别?
- 高分求“消息接受和发送”解决思路及DWR的Ajax反推疑问。
- fusioncharts获取xml数据源路劲问题?
public int select(Reset rs){
int count = rs.count();
rs.close();
return cout;
}
调用时就可以使用匿名类:
实例select方法对应的类。
实例.select(
new Reset(){
对应方法的重写等等。
}
);