-----Original Message----- From: Rudi Doku [mailto:[EMAIL PROTECTED]] Sent: 19 March 2002 01:13 To: sameer.kumar Subject: RE: Using JDBC via Servlet : connects on second try only !!
<load-on-startup> indicates the order in which each servlet should be loaded. Lower Positive vales are loaded first. if the value is Negative or unspecified, the container can load the servlet at any time during startup. 1. Make your connection and statement private variables of your class. 2. Try moving the code which creates a database connection and statement to init() method. This method is where the servlet life begins! it is called immediately after the servlet is instantiated!! By doing this, tomcat will always load your servlet first and create a connection and a statement object immediately. This way, You'll already have created a connection object and a statement object when the doPost() or doget() methods are invoked. Good Luck!! Rudi -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: 19 March 2002 19:47 To: [EMAIL PROTECTED] Subject: RE: Using JDBC via Servlet : connects on second try only !! Hi Rudi ! Thanks for ur reply. I tried the suggested solution but am still getting the same behaviour. Can u suggest some other solution ? And can u please also tell me the significant of '1' between the <load-on-startup> and </load-on-startup> tags. Thanks and Regards Sameer -----Original Message----- From: Rudi Doku [mailto:[EMAIL PROTECTED]] Sent: 18 March 2002 10:43 To: [EMAIL PROTECTED] Subject: Re: Using JDBC via Servlet : connects on second try only !! Why don't you add the following line "<load-on-startup>1</load-on-startup> " to the web.xml file so the jdbcServlet gets loaded when Tomcat is started? <servlet> <servlet-name>jdbcServlet </servlet-name> <servlet-class>com.x.y.jdbcServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> -----Original Message----- From: A mailing list for discussion about Sun Microsystem's Java Servlet API Technology. [mailto:[EMAIL PROTECTED]]On Behalf Of Kumar Sameer Sent: 19 March 2002 03:18 To: [EMAIL PROTECTED] Subject: Using JDBC via Servlet : connects on second try only !! Hi ! I am trying to use jdbc connectivity in a servlet. The program has compiled but runs OK only on second and subsequent invocation. The code is : import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class jdbcServlet extends HttpServlet { private Connection connection; private Statement statement; private String login, password,result; private ResultSet selectDesc; private PrintWriter out; public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); out = res.getWriter(); login = req.getParameter("login"); password = req.getParameter("password"); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(java.lang.ClassNotFoundException ex) { out.println("ClassNotFoundException : ErrorMessage : " + ex.getMessage() + "<p>"); } catch(NullPointerException ex) { out.println("NullPointerException: ErrorMessage : " + ex.getMessage() + "<p>"); } try { connection = DriverManager.getConnection("jdbc:odbc:test",login,password); } catch(SQLException ex) { out.println("SQLException: ErrorMessage : login failed login = "+ login + " password = " + password + "<p>"); out.println("ErrorMessage : " + ex.getMessage() + "<p>"); } catch(NullPointerException ex) { out.println("NullPointerException : ErrorMessage : login failed login = "+ login + " password = " + password + "<p>"); out.println("ErrorMessage : " + ex.getMessage() +"<p>"); } try { statement = connection.createStatement(); } catch(SQLException ex) { out.println("SQLException: ErrorMessage : " + ex.getMessage() + "<p>"); } catch(NullPointerException ex) { out.println("NullPointerException: ErrorMessage : " + ex.getMessage() + "<p>"); } try { selectDesc = statement.executeQuery("SELECT column_name FROM table_name where rownum = 1"); selectDesc.next(); result = selectDesc.getString("description"); } catch(SQLException ex) { out.println("SQLException : ErrorMessage : " + ex.getMessage() + "<p>"); } catch(NullPointerException ex) { out.println("NullPointerException : ErrorMessage : " + ex.getMessage() + "<p>"); } out.println("<HTML>"); out.println("<HEAD><TITLE>jabc example </TITLE><HEAD>"); out.println("<BIG> output of Querry : </BIG><P>"); out.println("Output = " + result); } then i typed in http://localhost:8080/SamApplication/servlet/jdbcServlet?login=login&passwor d=password in and pressed <enter> (Note : Output is indented) the output was SQLException: ErrorMessage : login failed login = login password = password ErrorMessage : [Microsoft][ODBC driver for Oracle][Oracle] NullPointerException: ErrorMessage : null NullPointerException : ErrorMessage : null output of Querry : Output = null then i pressed <enter> again the output was : output of Querry : Output = column value which is the expected result !!!!! Can anyone please xplain how is this possible !!?? I have repeted this sequence 5-6 times after stopping tomcat and restarting it. And the behaviour persists. I am using Tomcat 4.0 and jdk 1.4.0 Regards Sameer ___________________________________________________________________________ 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 ___________________________________________________________________________ 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 ___________________________________________________________________________ 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
