如果发生 SQLException,我一直在尝试寻找一种重定向到 HTML 文件的方法。我给你举个例子。
我的连接类是这样的:
public class DBConection{
Connection con = null;
public DBConnection() throws RuntimeException{
try {
Class.forName("org.gjt.mm.mysql.Driver");
String user = "root";
String pass = "12345";
String db = "java";
con = DriverManager.getConnection("jdbc:mysql://localhost/" + db, user, pass);
}catch (ClassNotFoundException ex) {
throw new RuntimeException();
}catch (SQLException ex) {
throw new RuntimeException();
}
}
}
我从 Servlet 类中调用它,
public class ElectionsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String RETURN_PAGE = "index.jsp";
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
DBConnection con = new DBConnection();
response.sendRedirect(RETURN_PAGE);
}
}
如果在 DBConnection 期间发生错误,我想将其重定向到我的 RETURN_PAGE。有谁能够帮助我?