Sorry guys, missed the statement line out in my eagerness to cut the
code down (thanks Tim). Corrected snippet below.

If I went to a connection pool, had all my variables inside the doGet
would this be thread safe ? If so then that is what I need, I dont need
session state etc.. as user validation is carried out against the
database at each request of a report.

Steve

assume try/catch blocks etc are in place within the code:

public class RepServlet extends HttpServlet
{
   private Connection con   = null;
   private String DBDriver  = "COM.ibm.db2.jdbc.app.DB2Driver";
   private String database  = "jdbc:db2:aDatabase";
   int product;
   private String user      = "user";     // all users connect through
   private String pwd       = "password"; // same global id + password
   private String SQLQuery1 = "select * from aTable a" +
                              "where  a.prod_no = ";
   private String SQLQuery2 = "with ur";


public void init ( ServletConfig config ) throws ServletException
{
   super.init ( config );
   try
   {
      Class.forName ( DBDriver );
      con = DriverManager.getConnection ( database, user, pwd );
   }
   catch ( ClassNotFoundException e )
   {
     throw new UnavailableException ( this, "Couldn't load driver" );
   }
   catch ( SQLException e )
   {
    throw new UnavailableException ( this, "Couldn't get connection" );
   }
}


public void doGet ( HttpServletRequest req, HttpServletResponse res )
                               throws ServletException, IOException
{
   res.setContentType ( "text/html" );
   ServletOutputStream out = res.getOutputStream ();
   StringBuffer buf = new StringBuffer ();

   getQueryString ( req ); // method in servlet not shown, gets product
   validateUser ( id ) // validation of user method, not shown

   Statement stmt = con.createStatement ();

   if ( stmt.execute (  ( SQLQuery1 + product + SQLQuery2 ) ) )
   {
         ResultSet rs = stmt.getResultSet ();
         ResultSetMetaData rsmd = rs.getMetaData ();
         while ( rs.next () )
         {
            //use results to produce html output
         }
   }
   else
   {
         buf.append ( "<B>Hmmmm......</B> " );
   }
   res.setContentLength ( buf.length () );
   out.println ( buf.toString () );
   out.flush ();
   out.close ();
   // close connection code....
}
}

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to