关闭结果集rs和statement以后,是否还要关闭数据库连接呢?
public class ItemsDAO {
/*获得所有商品*/
public ArrayList<Items> getAllItems(){
Connection conn=null;
PreparedStatement stmt=null;
String sql="select * from shop";
ResultSet rs=null;
ArrayList<Items> list=new ArrayList<Items>();
try
{
conn=DBHelper.getConnection();
stmt=conn.prepareStatement(sql);
rs=stmt.executeQuery();
while(rs.next())
{
Items item=new Items();
item.setId(rs.getInt("id"));
item.setName(rs.getString("name"));
item.setCity(rs.getString("city"));
item.setNumber(rs.getInt("number"));
item.setPicture(rs.getString("picture"));
item.setPrice(rs.getInt("price"));
list.add(item);
}
return list;
}catch(Exception ex)
{
ex.printStackTrace();
return null;
}finally
{
/*释放结果集*/
if(rs!=null)
{
try
{
rs.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
/*释放statement*/
if(stmt!=null)
{
try
{
stmt.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
/*是需要释放数据库连接呢?老师讲解时,没有提到这一点*/
if(conn!=null)
{
try
{
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
}后面的两个方法getItemsById和getItemsByList里面还要重新建立连接的。每次执行方法之后,要不要关闭呢?