ResultSet多次引用
ResultSet多次引用,提示Resource leak: 'rs' is not closed at this location,这是为什么?
请老师解答一下!!!!!
public void demo2() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM goods");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
float price = rs.getFloat("price");
String desp = rs.getString("desp");
System.out.println(id + " " + name + " " + price + " " + desp);
}
rs = stmt.executeQuery("SELECT * FROM goods WHERE name='冰箱'");
if (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
float price = rs.getFloat("price");
String desp = rs.getString("desp");
System.out.println(id + " " + name + " " + price + " " + desp);
}
int i = stmt.executeUpdate("UPDATE goods SET price=5000 WHERE name='手机' ");
if (i > 0) {
System.out.println("修改成功!");
} else {
System.out.println("修改失败!");
}
i = stmt.executeUpdate("DELETE FROM goods WHERE name='洗衣机'");
if (i > 0) {
System.out.println("删除成功!");
} else {
System.out.println("删除失败!");
}
rs = stmt.executeQuery("SELECT * FROM goods ORDER BY price ASC");
while(rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
float price = rs.getFloat("price");
String desp = rs.getString("desp");
System.out.println(id + " " + name + " " + price + " " + desp);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.release(rs, stmt, conn);
}
}
正在回答 回答被采纳积分+1
错误提示很明显,说的是rs资源泄露,不在此位置进行关闭,建议你在每个while循环后调用JDBCUtils的release方法来释放资源试试,祝学习愉快~
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星