I am using Tomcat as a server to work on a servlet that need to access a database and I need to use CachedRowSet, but I cannot seem to connect to the database correctly.

Currently I have

public class FaqListingServlet extends HttpServlet 
{
  
   private CachedRowSetImpl cachedRowSet;
   // set up database connection and create SQL statement
  
  public void init( ServletConfig config ) throws ServletException
   {
      // attempt database connection and create Statement
      try 
      {
      	Class.forName("databaseDriver");
      	//CachedRowSetImpl cachedRowSet = new CachedRowSetImpl();
      	cachedRowSet.setUrl("databaseName");
      	cachedRowSet.setUsername("username");
      	cachedRowSet.setPassword("password");
      	cachedRowSet.setCommand("SELECT * FROM topics");
      	cachedRowSet.execute();
        
        
      } // end try
      // for any exception throw an UnavailableException to 
      // indicate that the servlet is not currently available
      catch ( Exception exception ) 
      {
         exception.printStackTrace();
         throw new UnavailableException( exception.getMessage() );
      }// end catch
   }  // end method init

where the "databaseDriver", "databaseName", "password" etc. are all declared in an xml file. Currently though it always dumps an exception because it never successfully makes the connection. I am assuming it is my implementation of the CachedRowSet that is incorrect so any suggestions would be appreciated.

Also does anyone know why tomcat would suddenly not display the tomcat homepage? I get a 404 error, but if I add the folders (e.g. localhost:8080/examples)everything displays as it should. It has been a long week already...

are you sure that is loading your xml file properly?

for testing purposes try to hardcode it, then move it to the xml file

Class.forName("com.mysql.jdbc.Driver");
//continue with the other fields

also, after that if you still get an error, what is it?

well I hardcoded it and now it loads the database, but now my getPost method is crashing. Yeah!

you need help with it then? or you are good?

I could still use some help. Now my error seems to be related to the area when I attempt to assign the crs.getMetaData() to a ResultSet. Am I using incompatible types or is it the way I declared RowSetCacheImpl?

protected void doGet( HttpServletRequest request,
      HttpServletResponse response )
         throws ServletException, IOException
   {
      response.setContentType( "text/html" ); 
      PrintWriter out = response.getWriter();

      // start XHTML document
      out.println( "<?xml version = \"1.0\"?>" );

      out.printf( "%s%s%s", "<!DOCTYPE html PUBLIC", 
         " \"-//W3C//DTD XHTML 1.0 Strict//EN\"",
         " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" );

      out.println( 
         "<html xmlns = \"http://www.w3.org/1999/xhtml\">" );

      // head section of document
      out.println( "<head>" );  
      
      try 
      {
       	  // get 
      	  	
        	 out.println( "<title>Thank you!</title>" );
        	 out.println( "</head>" );  
         
        	 out.println( "<body>" );  
        	 out.println( "<p>Thank you for participating." );
        	 out.println( "<br />Results:</p><pre>" );
         
        	 // process results
        	 ResultSetMetaData metaData = crs.getMetaData();
        	 int numberOfCol = metaData.getColumnCount();
        	 
......

Still could use help. Here are the tomcat logs as well:

omcat log follows:
Nov 13, 2008 12:03:11 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet faqservlet threw exception
java.lang.NullPointerException
at com.deitel.jhtp6.servlets.FaqListingServlet.doGet(FaqListingServlet.java:92)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:613)

are you sure crs isn't null? where is it getting assigned?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.