當我看到大家給我帖子的回復,我很高興!我希望能給大家提醒,在寫程序中時候出現的“漏洞”
這是致命的“打擊”,那現在我發表一個比較完善的異常處理的程序,以便大家能參考。
內容如下:
public class TestJdbc {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xx", "scott", "tiger");
stmt = conn.createStatement();
String sql ="select * from dept2";
rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getString("dname"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(rs !=null){
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}