在日常的开发中, 我发现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());
}
}
通用的JDBC的try…catch…finally
最新推荐文章于 2015-09-22 20:05:17 发布
本文提供了一种在Java中使用JDBC进行数据库操作时的异常处理模板。该模板通过使用try-catch-finally结构来确保资源正确关闭,并在发生异常时能够捕获并输出异常信息。
1359

被折叠的 条评论
为什么被折叠?



