Ajay Ejantkar wrote:



> I am trying to run the following servlet.
>
> *************************************************************************
>

You must especify the driver Class, and the correct parameters to etstabilish 
conection.

[]´s


Daniel A.


> config.getInitParameter("driverClassName");
>         String url = config.getInitParameter("url");
>         String username = config.getInitParameter("username");
>         String password = config.getInitParameter("password");
>
>         // Use those init params to establish a connection to the
> database
>         // If anything goes wrong, log it, wrap the exception and
> re-throw it
>         try {
>             Class.forName(driverClassName);
>             db = DriverManager.getConnection(url, username, password);
>         }
>         catch (Exception e) {
>             log("Can't create DB connection", e);
>             throw new ServletException("Query: can't initialize: " +
> e.getMessage(), e);
>         }
>     }
>
>     /** Close the database connection when the servlet is unloaded */
>     public void destroy() {
>         try{ db.close(); }          // Try to close the connection
>         catch (SQLException e) {}   // Ignore errors; at least we
> tried!
>     }
>
>     public void doGet(HttpServletRequest request, HttpServletResponse
> response) throws IOException, ServletException
>     {
>         response.setContentType("text/html");   // We'returnoutputting
> HTML
>         PrintWriter out = response.getWriter(); // Where to output it
> to
>
>         // Output document header and a form for entering SQL queries
>         // When the form is submitted, this servlet is reloaded
>         out.println("<HEAD><TITLE>DB Query</TITLE></HEAD>\n" +
>                     "<BODY BGCOLOR=white><H1>DB Query</H1>\n" +
>                     "<FORM><B>Query:</B><INPUT NAME='q'>" +
>                     "<INPUT TYPE=submit></FORM>");
>
>         // See if a query was specified in this request.
>         String query = request.getParameter("q");
>         if (query != null) {
>             // display the query text as a page heading
>             out.println("<H1>" + query + "</H1>");
>
>             // Now try to execute the query and display the results in
> a table
>             Statement statement = null; // An object to execute the
> query
>             try {
>                 // Create a statement to use
>                 statement = db.createStatement();
>                 // Use it to execute the specified query, and get the
> result set
>                 ResultSet results = statement.executeQuery(query);
>                 // Ask for extra information about the results
>                 ResultSetMetaData metadata = results.getMetaData();
>                 // How many columns are there in the results?
>                 int numcols = metadata.getColumnCount();
>
>                 // Begin a table, and output a header row of column
> names
>                 out.println("<TABLE BORDER=2><TR>");
>                 for(int i = 0; i < numcols; i++)
>                     out.print("<TH>" + metadata.getColumnLabel(i+1) +
> "</TH>");
>                 out.println("</TR>");
>
>                 // Now loop through the "rows of the result set
>                 while(results.next()){
>                     // For each row, display the values for each column
>                     out.print("<TR>");
>                     for(int i = 0; i < numcols; i++)
>                         out.print("<TH>" + results.getObject(i+1) +
> "</TD>");
>                     out.println("</TR>");
>                 }
>                 out.println("</TABLE>");  // end the table.
>
>             }
>             catch (SQLException e) {
>                 // If anything goes wrong (usually SQL error) display
> the
>                 // error to the user so they can correct it.
>                 out.println("SQL Error: " + e.getMessage());
>             }
>             finally { // Whatever happens, always close the Statement
> object
>                 try { statement.close();}
>                 catch (Exception e) {}
>              }
>         }
>
>         // Finally, end the HTML output
>         out.println("</BODY>");
>     }
> }
>
> *************************************************************************
>
> When I load the servlet I get the following error:
>
> Error: 500
> Location: /myapp/query
> Internal Servlet Error:
>
> javax.servlet.ServletException: Query: can't initialize: null
>         at Query.init(Query.java:31)
>         at javax.servlet.GenericServlet.init(GenericServlet.java:258)
>         at
> org.apache.tomcat.core.ServletWrapper.doInit(ServletWrapper.java:317)
>         at org.apache.tomcat.core.Handler.init(Handler.java:215)
>         at org.apache.tomcat.core.ServletWrapper.init(ServletWrapper.java:296)
>         at org.apache.tomcat.core.Handler.service(Handler.java:254)
>         at
> org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
>         at
> org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)
>         at
> org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
>         at
> 
>org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:210)
>         at
> org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
>         at
> org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
>         at java.lang.Thread.run(Thread.java:484)
>
> Root cause:
> java.lang.NullPointerException
>         at java.lang.Class.forName0(Native Method)
>         at java.lang.Class.forName(Class.java:120)
>         at Query.init(Query.java:26)
>         at javax.servlet.GenericServlet.init(GenericServlet.java:258)
>         at
> org.apache.tomcat.core.ServletWrapper.doInit(ServletWrapper.java:317)
>         at org.apache.tomcat.core.Handler.init(Handler.java:215)
>         at org.apache.tomcat.core.ServletWrapper.init(ServletWrapper.java:296)
>         at org.apache.tomcat.core.Handler.service(Handler.java:254)
>         at
> org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
>         at
> org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)
>         at
> org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
>         at
> 
>org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:210)
>         at
> org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
>         at
> org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
>         at java.lang.Thread.run(Thread.java:484)
>
> ************************************************************************
>
> I have put the location of my jdbc driver in my classpath before I
> start up the tomcat server. I have also tried to put the zip file with
> the drivers in myapps/web-inf/lib/ directory. It still does not seem to
> be able to locate the jdbc driver. Following is my web.xml file :
>
> ************************************************************************
> <!DOCTYPE web-app
>     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
>     "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd";>
>
> <web-app>
>
>     <display-name>Hello, World Application</display-name>
>     <description>
>         This is a simple web application with a source code organization
>         based on the recommendations of the Application Developer's Guide.
>     </description>
>
>     <servlet>
>         <servlet-name>HelloServlet</servlet-name>
>         <servlet-class>Hello</servlet-class>
>     </servlet>
>
>     <servlet-mapping>
>         <servlet-name>HelloServlet</servlet-name>
>         <url-pattern>/hello</url-pattern>
>     </servlet-mapping>
>
>     <servlet>
>         <servlet-name>MonkeyServlet</servlet-name>
>         <servlet-class>Monkey</servlet-class>
>     </servlet>
>
>     <servlet-mapping>
>         <servlet-name>MonkeyServlet</servlet-name>
>         <url-pattern>/monkey</url-pattern>
>     </servlet-mapping>
>
>     <servlet>
>         <servlet-name>ListManagerServlet</servlet-name>
>         <servlet-class>ListManagerServlet</servlet-class>
>             <init-param>
>                 <param-name>addressfile</param-name>
>                 <param-value>addressfile.txt</param-value>
>             </init-param>
>     </servlet>
>
>     <servlet-mapping>
>         <servlet-name>ListManagerServlet</servlet-name>
>         <url-pattern>/list</url-pattern>
>     </servlet-mapping>
>
>     <servlet>
>         <servlet-name>Query</servlet-name>
>         <servlet-class>Query</servlet-class>
>             <init-param>
>                 <param-name>driverClassName</param-name>
>                 <param-value>"com.sybase.jdbc.SybDriver"</param-value>
>                 <param-name>url</param-name>
>                 <param-value>"jdbc:sybase:Tds:localhost:2638"</param-value>
>                 <param-name>username</param-name>
>                 <param-value>"DBA"</param-value>
>                 <param-name>password</param-name>
>                 <param-value>"SQL"</param-value>
>             </init-param>
>     </servlet>
>
>     <servlet-mapping>
>         <servlet-name>Query</servlet-name>
>         <url-pattern>/query</url-pattern>
>     </servlet-mapping>
>
> </web-app>
> *************************************************************************
>
> Any help on this would be greatly appreciated.
>
> Ajay
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Auctions - buy the things you want at great prices
> http://auctions.yahoo.com/



Reply via email to