当我看到大家给我帖子的回复,我很高兴!我希望能给大家提醒,在写程序中时候出现的“漏洞”
这是致命的“打击”,那现在我发表一个比较完善的异常处理的程序,以便大家能参考。
内容如下:
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();
}
}
}
}