0

I am looking at a code written way back in time. It looks like this

public String MaxID() throws SQLException {

    Connection conn = null;
    Statement stmt = null;
    ResultSet rset = null;

    try {
    DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
    conn = DriverManager.getConnection(get_db_url(), get_db_id(), get_db_pw());
    stmt = conn.createStatement();
    String sql = null;
    sql = "select MAX(id)+1 AS count from tbl1";
    rset = stmt.executeQuery (sql);
    rset.next();         
    String newId = rset.getString("count");
    if (newId == null)
        newId = "1";     
        return newId;
    } catch (SQLException e) {

        return ("<P> SQL error: <PRE> " + e + " </PRE> </P>\n");

    } finally {
        if (rset!= null) rset.close();
        if (stmt!= null) stmt.close();
        if (conn!= null) conn.close();
    }

}

When I try to run it I receive the following output:

An error occurred at line: 146 in the jsp file: /functions.jsp Unhandled exception type SQLException 143: ResultSet rset = null; 144: 145: 146: DriverManager.registerDriver(new org.gjt.mm.mysql.Driver()); 147: conn = DriverManager.getConnection(get_db_url(), get_db_id(), get_db_pw()); 148: stmt = conn.createStatement(); 149: String sql = null;

And I can not understand why. It sems to be handled in a proper way (according to what I have read in the docs)

rhitz
  • 1,892
  • 2
  • 21
  • 26
  • are you using ' <%' Java code to be here in jsp '%>' tag ?? – rhitz Aug 01 '15 at 05:21
  • Haven't you noticed that the code listed in the error message has nothing to do with MaxID()? You're calling several methods (DriverManager.registerDriver() for example) which can throw an SQLException, but are not catching it. First thing to do: read http://docs.oracle.com/javase/tutorial/essential/exceptions/. Second thing to do: read http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files. – JB Nizet Aug 01 '15 at 05:26

0 Answers0