I wouldn't, I would still put the 3 closes in the finally clause; it's clean, simple 
and guaranteed to execute.  Also, an SQLException can be thrown by more than just a 
connection close failure.  Adding the Exception catch block is okay..


-------------------------------------
I would take it a step further..
i.e.
public void init()    throws ServletException
{ //start of init
   try
   { //start try block..
     Context env = (Context) new
     InitialContext().lookup("java:comp/env");
     pool = (DataSource) env.lookup("jdbc/test");
     if (pool == null)   throw new ServletException("`jdbc/test' is an
unknown DataSource");
   }
   catch (NamingException e)
  { //the DataSource lookup probably failed  so say something back to the
use
     java.lang.System.out.println("Cannot lookup DSN on
jdbc/test..Aborting");
     throw new ServletException(e);
   } //end catch
   finally
  {
    throw new ServletException();
   }
} //end init

public void doGet(HttpServletRequest req,HttpServletResponse res)    throws
IOException, ServletException
{
  res.setContentType("text/html");
  PrintWriter out = res.getWriter();

  Connection conn = null;
   try
  {
    conn = pool.getConnection();
    Statement stmt = conn.createStatement();
   //at this point we know we have a good stmt or not
    try
    {
     ResultSet rs = stmt.executeQuery("select NAME, PRICE from BROOMS");
     out.println("Brooms:<br>");
     while (rs.next())
     { //spin thru all the records..String then integer
       out.print(rs.getString(1));
       out.print(" ");
       out.print(rs.getInt(2));
       out.println("<br>");
     } //end while
     rs.close();
     stmt.close();
   }
   catch(Exception e) //more than likely is is a SQLException but could be
NullPointer or anything else..
  { //we know something bad happened to resultset or the statement so close
them
      rs.close();   //Resultset closed
     stmt.close(); //statement closed
  } //end Exception
  if (conn!=null) conn.close(); //the connection is now closed...
 } catch (SQLException e)
{
    java.lang.System.out.println("Connection close caused SQLException
cause="+e.getMessage());
     throw new ServletException(e);
 }
 finally
{ //catch all for all Exceptions
 java.lang.System.out.println("Connection code caused general Exception
returning back now..");
 throw new ServletException();
}
} //end doGet
Regards,
Martin
----- Original Message -----
From: "William W. Nelson" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, February 03, 2003 12:29 PM
Subject: Re: Closing a ResultSet, was "Re: odd oracle error"


> rs.close() and stmt.close() should be in the finally block.
>
>
> --------------------------------------------------------------------------

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".

Some relevant archives, FAQs and Forums on JSPs can be found at:

 http://java.sun.com/products/jsp
 http://archives.java.sun.com/jsp-interest.html
 http://forums.java.sun.com
 http://www.jspinsider.com

Reply via email to