jerryjerry123
发贴: 58
积分: 12
|
于 2005-05-12 10:06
在日常的开发中, 我发现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()); } }
jerryjerry123 edited on 2005-05-12 10:10
|