0

如果发生 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。有谁能够帮助我?

4

3 回答 3

2

只需使用内置的容器管理的异常处理工具。您需要将捕获的异常重新抛出为ServletException并将错误页面定义<error-page>web.xml.

您只需要相应地更改您的数据库访问逻辑。它远非理智、安全和高效。异常处理也很奇怪。鉴于您使用的是已弃用的 MySQL JDBC 驱动程序类名称,您似乎已经从完全过时的资源中删除了它。

这是一个基本的启动示例:

public final class Database {
    
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost/java";
    private static final String USER = "root";
    private static final String PASS = "12345";

    static {      
        try {
            Class.forName(DRIVER);
        }
        catch (ClassNotFoundException e) {
            throw new ExceptionInInitializerError("The JDBC driver is missing in classpath!", e);
        }
    }

    private Database() {
        // Don't allow construction.
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER, PASS);
    }

}

最后在servlet中使用和处理如下:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    Connection connection = null;
    // ...

    try{
        connection = Database.getConnection();
        // ...
    } 
    catch (SQLException e) {
        throw new ServletException("DB fail!", e);
    }
    finally {
        // ...
        if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
    }
}

(最好将 DB 作业包装在一个 DAO 类中,该类又抛出 a SQLException,但这是一个不同的问题)

并声明错误页面web.xml如下:

<error-page>
    <exception-type>java.sql.SQLException</exception-type>
    <location>/WEB-INF/errorpages/database.jsp</location>
</error-page>

您当然也可以使用<location>/index.jsp</location>,但这对最终用户不是很友好,因为这完全令人困惑他为什么回到那里。而是将其作为database.jsp错误页面中的链接以及用户友好的内容

抱歉,与 DB 通信时出现不可恢复的问题。请点击以下链接返回索引页面。

也可以看看:

于 2012-04-19T15:51:24.867 回答
0

这个问题已经回答了,但我会添加一些评论:

  • 我不认为有必要捕捉SQLException,而ClassNotFoundException只是重述RuntimeException实例。如果您打算在您的DBConnection类中引发此异常时什么都不做,只需添加throws到您的构造函数签名中。这样,编译器将检查在使用构造函数时是否添加了 try-catch 块。
  • 无需添加throws RuntimeException到您的构造函数签名。RuntimeException是一个未经检查的异常。
于 2012-04-19T15:52:15.907 回答
-1

将 try-catch 块添加到您的doPost方法中,如果 try 块中发生任何异常,则捕获该异常并重定向到RETURN_PAGE.

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        try{
        DBConnection con = new DBConnection();
        }catch(Exception e){
        response.sendRedirect(RETURN_PAGE);
       }
    }
于 2012-04-19T15:31:13.000 回答