举个例子吧
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from dept");
while (rs.next()) {
System.out.print(rs.getString("deptno") + " ");
}
} 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();//将异常信息打印出来
}
}
追问引用调用方法是哪一步呢?
这到底是捕捉什么异常啊?我是新手。不好意思!
追答前三行是定义变量,第6行是实例化conn,第8行就是调用它的createStatement()方法。第一处捕获的是驱动类未找到的异常,第二,三处是SQL语句的异常,还有什么不明白的吗?
本回答被提问者采纳