0

I've been trying to search for a way to redirect to an HTML file if a SQLException occurs. I'm going to give you an example.

I have my connection class something like this:

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();
        }
    }
}

and I'm calling it from a Servlet class,

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);
    }
}   

If an error ocurres during the DBConnection I want to redirect it to my RETURN_PAGE. Can anybody help me?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Tomarto
  • 2,755
  • 6
  • 27
  • 37

3 Answers3

2

Just use the builtin container managed exception handling facilities. You need to rethrow the caught exception as ServletException and define the error page as <error-page> in web.xml.

You only need to change your DB access logic accordingly. It's far from sane, safe and efficient. The exception handling is also strange. You seem to have ripped this from a completely outdated resource given the fact that you're using the deprecated MySQL JDBC driver class name.

Here's a basic kickoff example:

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);
    }

}

Finally use and handle it in the servlet as follows:

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) {}
    }
}

(better would be to wrap the DB job in a DAO class which in turn throws a SQLException, but that's a different problem)

And declare the error page in web.xml as follows:

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

You can of course also use <location>/index.jsp</location> but that's not very friendly to the enduser as it's completely confusing why he returns back to there. Rather put that as a link in the database.jsp error page along with something user friendly like

Sorry, an unrecoverable problem has occurred while communicating with the DB. Please click the following link to return to the index page.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

The question is already answered, but I'll add a few comments:

  • I don't see the need of catching SQLException and ClassNotFoundException only to rethow RuntimeException instances. If you're planning to do nothing when this exception raises in your DBConnection class, just add throws to your constructor signature. With that, the compiler will check that a try-catch block be added when using your constructor.
  • There's no need of adding throws RuntimeException to your constructor signature. RuntimeException is a non-checked exception.
Carlos Gavidia-Calderon
  • 7,145
  • 9
  • 34
  • 59
-1

Add try-catch block to your doPost method and if any exception occurs in try block, catch that exception and redirect to RETURN_PAGE.

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        try{
        DBConnection con = new DBConnection();
        }catch(Exception e){
        response.sendRedirect(RETURN_PAGE);
       }
    }
Ramesh Kotha
  • 8,266
  • 17
  • 66
  • 90