设为首页 加入收藏

TOP

Java 7 优雅的自动资源管理
2014-11-24 07:23:49 来源: 作者: 【 】 浏览:1
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获得本地字体列表

评论

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

·Java 学习线路图是怎 (2025-12-25 15:19:15)
·关于 Java 学习,有 (2025-12-25 15:19:12)
·有没有Java swing教 (2025-12-25 15:19:09)
·Start, Stop, and Di (2025-12-25 14:50:57)
·C语言入门教程:零基 (2025-12-25 14:50:54)