62,614
社区成员
发帖
与我相关
我的任务
分享
public void test(String a) {
Connection c = null;
// init connection
System.out.println(a.length()); //如果a为null,将导致数据库连接c不能关闭
try {
c.close();
} catch (SQLException e) {
}
}
public void test2(String a) {
Connection c = null;
// init connection
try {
System.out.println(a.length()); //这样肯定能关闭数据苦连接
} finally {
try {
c.close();
} catch (SQLException e) {
}
}
}
public void test3(String a) {
Connection c = null;
// init connection
if (a != null) //这个比test2要好,但是每次调用都判断是不是为null,非常的麻烦。
System.out.println(a.length());
try {
c.close();
} catch (SQLException e) {
}
}