public Vector selectAll() {
// TODO Auto-generated method stub
    Connection conn =  DBmanager.getManager().getConnecton();
    PreparedStatement ps =null ;
    String sql = "select * from dep";
    ResultSet rs =null ;
    try {
     Vector<DepForm> vt =new Vector<DepForm>();
ps=conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
DepForm df =new DepForm();
df.setDepid(rs.getString(1));
df.setDepname(rs.getString(2));
vt.add(df);
}
return vt ;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
try {
if (rs!=null) {
rs.close();
}
if (ps!=null) {
ps.close();
}
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

return null;
}
这种关闭不好呀。
try {
if (rs!=null) {
rs.close();
}
if (ps!=null) {
ps.close();
}
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
万一有异常的话,都关不了的。
有啥好的解决方法吗?

解决方案 »

  1.   

    try {

    } catch (Exception e) {
    e.printStackTrace();
    }finally
    {
    /*
     * 最牛的关闭数据库的方法呀。^_^ 。o(∩_∩)o...
     * */
    try {
    if (rs!=null) {
    rs.close();
    }

    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally
    {
    try {
    if (ps!=null) {
    ps.close();
    }

    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    try {
    if (conn!=null) {
    conn.close();
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    }
    这样如何呀?
    一定很不错吧。