LZ 你捕获的是 ClassNotFoundException ,SQLException两个异常,你抛出的是Excepiton当然会报错
你想抛出的话 也要这样
public static Connection getConnection() throws ClassNotFoundException,SQLException{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", user, password);
return con;
}
public static Connection getConnection() throws Exception
定义了一个方法throws Exception,意味着这个方法内部的一切异常,都由调用者负责捕获并处理 而不是说方法定义了一个throws就不用再对异常进行处理了-0-
也就是说 可以try catch 也可以写throws ClassNotFoundException,SQLException 两个异常 然后在调用者那 再写ClassNotFoundException, SQLException