设为首页 加入收藏

TOP

Java 7 优雅的自动资源管理
2014-11-24 07:23:49 来源: 作者: 【 】 浏览:0
Tags:Java 优雅 自动 资源管理

[代码] Java7以前的做法


Connection connection = null;
Statement statement = null;


try{
connection = DriverManager.getConnection(“databaseurl”,”username(opt)”,”password(opt)”);
statement = connection.createStatemnet();
boolean executionStatus= statement.execute(“query”);
}catch(Exception e){
//Block of code for handle the exception
e.printStacktrace();
}finally{
try{
statement.close();
connection.close();
}catch(Exception e){
//block of statements for handle exceptions.
}
}


[代码] Java 7的做法(无需手工释放资源)


Connection connection = null;
Statement statement = null;


try(//请注意这里的小括号,不是大括号
connection =    DriverManager.getConnection(“databaseurl”,”username(opt)”,”password(opt)”);
statement = connection.createStatemnet()
)
{
boolean executionStatus= statement.execute(“query”);
}catch(Exception e){
//block of statements for handles the exceptions
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux Busybox 编译遇到的问题[图.. 下一篇Java获得本地字体列表

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·python数据分析岗的 (2025-12-25 10:02:21)
·python做数据分析需 (2025-12-25 10:02:19)
·成为一个优秀的pytho (2025-12-25 10:02:16)
·Java后端面试实习自 (2025-12-25 09:24:21)
·Java LTS版本有哪些 (2025-12-25 09:24:18)