为什么throws exception不行 而try catch行
一段连接数据库的方法
throws的:
public static Connection getConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", user, password);
return con;
}
报错
------------------------------------------
try catch的 :
public static Connection getConnection() {
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn ;
}
通过
何解 什么原理