通用的JDBC的try…catch…finally模板

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());
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章