Loading a driver from servlet. How to vs. in a JSP?

Discussions

Web tier: servlets, JSP, Web frameworks: Loading a driver from servlet. How to vs. in a JSP?

  1. Hi,

    Any ideas why this code to load the MM.MySQL driver and connect to a MySQL DB works fine from a JSP, but reports the error below when trying to load from a servlet?

    Class.forName("org.gjt.mm.mysql.Driver");
    java.sql.Connection connection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/sparrowhawk?user=admin&password=test");
    java.sql.Statement statement = connection.createStatement();
    String sql = "select 1 from people where email = '" + request.getParameter("passwordField") + "';" ;
    java.sql.ResultSet rows = statement.executeQuery(sql);

    The error stack from the servlet container (Tomcat 4) is:

    C:\Program Files\tomcat\webapps\jdw\WEB-INF\classes>javac login.java
    login.java:27: unreported exception java.lang.ClassNotFoundException; must be caught or declared to
            Class.forName("org.gjt.mm.mysql.Driver");
                 ^
    login.java:28: unreported exception java.sql.SQLException; must be caught or declared to be thrown
            java.sql.Connection connection = java.sql.DriverManager.getConnection("jdbc:mysql://localho
    dmin&password=test");
                                                                   ^
    login.java:29: unreported exception java.sql.SQLException; must be caught or declared to be thrown
            java.sql.Statement statement = connection.createStatement();
                                                     ^
    login.java:31: unreported exception java.sql.SQLException; must be caught or declared to be thrown
            java.sql.ResultSet rows = statement.executeQuery(sql);
                                               ^
    login.java:33: unreported exception java.sql.SQLException; must be caught or declared to be thrown
            rows.last();
                ^
    login.java:34: unreported exception java.sql.SQLException; must be caught or declared to be thrown
            int numRows = rows.getRow();
                              ^
    login.java:46: unreported exception java.sql.SQLException; must be caught or declared to be thrown
            statement.close();
                     ^
    login.java:47: unreported exception java.sql.SQLException; must be caught or declared to be thrown
            connection.close();
                      ^
    8 errors

    Thanks,

    Mike

  2. If you set the error page to a jsp, it will take you to error page when an exception is being thrown. whereas you need to take care of the exception handling explicitly in the servlet code. Another option is to make the doGet or doPost method throw Exception and need not do the exception handling.
  3. So are you saying the error in the servlet (which was actually from javac while comiling it) was becuase I don't have exception code there in case there is an exception, not actually becuase there was an exception?

    I'm afraid I'm still confused (but thatnks for your reply).

    Mike
  4. That's exactly what he's saying.

    When the container is compiling the JSP page into a servlet, it wraps your code with some proprietary code.
    One of these additions made by the JSP compiler is to include a try-catch block around your code and the logic for forwarding to the defined error page in case an exception is thrown by your code.

    When you're writing a servlet, there is no other compilation than the javac compiler, which complains about you not catching an exception that can be thrown.
  5. Thanks for the clarification. I found this out the hard way by hunting through some other code with tray/catch included...

    What rules are there about when try/catch are required, and when you can get by without them? The only documentation I've seen about it was pretty loose...

    Thanks for all the help everyone,

    Mike