当Statement,ResultSet抛出错误之后,他是否会自动关闭?还是要手动关闭?
第一种写法
public void getit(Connection con,String id)throws Exception
{
String sql="select * from .. where id="+id;
Statement sm = conn.createStatement();
ResultSet rs = sm.executeQuery(sql);
if(rs.next){
System.out.print(rs.getString(2));
}
if(rs!=null)
rs.close();
rs=null;
if(sm!=null)
sm.close();
sm=null;
}
第二种写法
public void getit(Connection con,String id)throws Exception
{
Statement sm =null;
ResultSet rs=null;
String sql="select * from .. where id="+id;
try{
sm = conn.createStatement();
rs = sm.executeQuery(sql);
if(rs.next){
System.out.print(rs.getString(2));
}
}catch(Exception e){
throw new Exception(e);
}finally{
if(rs!=null)
rs.close();
rs=null;
if(sm!=null)
sm.close();
sm=null;
}
}
第二种写法比较安全,但是他会影响性能。