title: PostgreSQL connection in Java Servlet to Retrieve Information from the DB. Getting Error
body: <p>I am having difficulty with getting this work. I can connect to the database without problem, however I can not make it show me the html page. It does not run.</p>
  
  <pre><code>import java.io.*;
  import java.sql.*;
  import java.text.*;
  import java.util.*;
  import javax.servlet.*;
  import javax.servlet.http.*;
  
  public class ShowBedrock extends HttpServlet 
  {
      public String getServletInfo()
      {
         return "Servlet connects to PostgreSQL database and displays result of a SELECT";
      }
  
      private Connection dbcon;  // Connection for scope of ShowBedrock
  
      // "init" sets up a database connection
      public void init(ServletConfig config) throws ServletException
      {
          String loginUser = "postgres";
          String loginPasswd = "supersecret";
          String loginUrl = "jdbc:postgresql://localhost/bedrock";
  
          // Load the PostgreSQL driver
          try 
          {
                Class.forName("org.postgresql.Driver");
                dbcon = DriverManager.getConnection(loginUrl, loginUser, loginPasswd);
          }
          catch (ClassNotFoundException ex)
          {
                 System.err.println("ClassNotFoundException: " + ex.getMessage());
                 throw new ServletException("Class not found Error");
          }
          catch (SQLException ex)
          {
                 System.err.println("SQLException: " + ex.getMessage());
          }
      }
  
      // Use http GET
  
      public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws IOException, ServletException
      {
          response.setContentType("text/html");    // Response mime type
  
          // Output stream to STDOUT
          PrintWriter out = response.getWriter();
  
          out.println("&lt;HTML&gt;&lt;Head&gt;&lt;Title&gt;Bedrock&lt;/Title&gt;&lt;/Head&gt;");
          out.println("&lt;Body&gt;&lt;H1&gt;Bedrock&lt;/H1&gt;");
  
          try
          {
                  // Declare our statement
                  Statement statement = dbcon.createStatement();
  
                  String query = "SELECT name, dept, ";
                  query +=       "       jobtitle ";
                  query +=       "FROM   employee ";
  
                  // Perform the query
                  ResultSet rs = statement.executeQuery(query);
  
                  out.println("&lt;table border&gt;");
  
                  // Iterate through each row of rs
                  while (rs.next())
                  {
                     String m_name = rs.getString("name");
                     String m_dept = rs.getString("dept");
                     String m_jobtitle = rs.getString("jobtitle");
                     out.println("&lt;tr&gt;" + 
                                 "&lt;td&gt;" + m_name + "&lt;/td&gt;" +
                                 "&lt;td&gt;" + m_dept + "&lt;/td&gt;" +
                                 "&lt;td&gt;" + m_jobtitle + "&lt;/td&gt;" +
                                 "&lt;/tr&gt;");
                  }
  
                  out.println("&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;");
                  statement.close();
          }
          catch(Exception ex)
          {
                  out.println("&lt;HTML&gt;" +
                              "&lt;Head&gt;&lt;Title&gt;" +
                              "Bedrock: Error" +
                              "&lt;/Title&gt;&lt;/Head&gt;\n&lt;Body&gt;" +
                              "&lt;P&gt;SQL error in doGet: " +
                              ex.getMessage() + "&lt;/P&gt;&lt;/Body&gt;&lt;/HTML&gt;");
                  return;
          }
          out.close();
      }
  }
  </code></pre>
  
  <p>I get the following error any body knows why?:</p>
  
  <pre><code>javax.servlet.ServletException: Class not found Error
      ShowBedrock.init(ShowBedrock.java:42)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
      org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
      org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
      org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      java.lang.Thread.run(Unknown Source)
  
  
  note The full stack trace of the root cause is available in the Apache Tomcat/6.0.35 logs.
  </code></pre>