kjg589
新新人类
帖子
31
精华
0
无忧币 5063
积分 135
阅读权限 20
|
发表于:2008-4-7 14:28
标题:通用的JDBC的try…catch…finally模板
<上一帖 |
下一帖>
在日常的开发中, 我发现JDBC操作的Exception处理方法可总结为下面模板, 希望对大家有用.
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
// Get connection from DriverManager or from DataSource
conn = DriverManager.getConnection("connection string", "username", "pwd");
stmt = conn.prepareStatement("SELECT count( * ) FROM user_objects order by object_name");
rs = stmt.executeQuery();
if (rs.next())
{
……
……
}
rs.close();
} catch (Exception e)
{
System.out.println("[Exception] - " + e.toString());
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (Exception fe)
{
System.out.println("[Exception] - " + fe.toString());
}
}
|
 网络工程师到底该不该去考CCIE认证? |
|