Hi,

To make sure that your database connection is closed
add the finally block, so it would be like this:

try {
        // your code here
        //

        // you close your db connection here
        rs.close();
        stmt.close();
        conn.close();
} catch (Exception e) {

} finally {
        // make sure it's closed
        if (rs != null) rs.close();
        if (stmt != null) stmt.close();
        if (conn != null) conn.close();
}

regards,
Wayan

-----Original Message-----
From: Papo Napolitano [mailto:[EMAIL PROTECTED]]
Sent: Sunday, December 02, 2001 5:55 AM
To: [EMAIL PROTECTED]
Subject: Servlet & Mysql


Hi!

I've written the following Servlet to read an Image from a Mysql Database
and send it to the browser.

public class ImageServlet extends HttpServlet {
 public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
  try {
    InitialContext jndiContext = new InitialContext();
    DataSource ds = (DataSource)jndiContext.lookup("java:/mySQLDS") ;
    Connection conn = ds.getConnection() ;
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT image from docs where idx=" +
request.getParameter("idx"));

   if (rs.next()) {
     response.setContentType("image/gif");
     PrintWriter out = response.getWriter();
    out.println(rs.getString("file" + request.getParameter("idximg")));
    response.flushBuffer();
   }

   rs.close();
   stmt.close();
   conn.close();
   ds = null ;

  } catch(Exception e){
     response.setContentType("text/html");
     PrintWriter out = response.getWriter();
     out.println("Error: "+e);
  }
 }
}


It works fine, but I've seen that it leaves mysql instances opened in my
server... Any idea ???

Cheers,

Papo

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to