title: Retrieve DB data with Java servlet and HTML form
body: <p>I created a simple form and a small servlet to execute a SQL statement from browser. When I click "Submit Query" button on the form, then the returinig page shows nothing retrieved from the DB. It only shows "Database Results" ( tag part) on the browser. Please advice me what is wrong with my code.</p>
  
  <p>SQLTestForm.java</p>
  
  <pre><code>import java.io.*;
  import javax.servlet.*;
  import javax.servlet.http.*;
  import java.sql.*;
  
  public class SQLTestForm extends HttpServlet {
      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
          String title = "Results";
          String docType = 
              "&lt;!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\"\n";
              out.print(docType + "&lt;HTML&gt;\n" + "&lt;HEAD&gt;&lt;TITLE&gt;" + title + "&lt;/TITLE&gt;&lt;/HEAD&gt;\n" + "&lt;BODY&gt;" + "&lt;H1&gt;Database Results&lt;/H1&gt;\n");
  
          String driver = request.getParameter("driver");
          String url = request.getParameter("url");
          String username = request.getParameter("username");
          String password = request.getParameter("password");
          String qry = request.getParameter("query");
          showTable(driver, url, username, password, qry, out);
          out.println("&lt;/BODY&gt;&lt;/HTML&gt;");
      }
  
    public void showTable(String driver, String url, String username, String password, String qry, PrintWriter out) {
      try {
      Class.forName(driver);
      Connection conn = DriverManager.getConnection(url, username, password);
      DatabaseMetaData dbMetaData = conn.getMetaData();
      out.println("&lt;UL&gt;");
      String productName = dbMetaData.getDatabaseProductName();
      String productVersion = dbMetaData.getDatabaseProductVersion();
      out.println(" &lt;LI&gt;&lt;B&gt;Database:&lt;/B&gt; " + productName + " &lt;LI&gt;&lt;B&gt;Version:&lt;/B&gt; " + productVersion + "&lt;/UL&gt;");
      Statement stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery(qry);
      out.println("&lt;TABLE BORDER=1&gt;");
      ResultSetMetaData rm = rs.getMetaData();
      int cnum = rm.getColumnCount();
      out.println("&lt;TR&gt;");
          for(int i=1; i &lt;= cnum; i++) {
              out.print("&lt;TH&gt;" + rm.getColumnName(i));
          }
          out.println();
          while(rs.next()) {
              out.println("&lt;TR&gt;");
                  for(int i=1; i &lt;= cnum; i++) {
                      out.print("&lt;TD&gt;" + rs.getString(i));
                  }
              out.println();
          }
          out.println("&lt;/TABLE&gt;");
          conn.close();
      } catch (ClassNotFoundException cnfe) {
          System.err.println("Error loading driver: " + cnfe);
      } catch (SQLException se) {
          System.err.println("Error connecting: " + se);
      } catch(Exception e) {
          System.err.println("Error with input: " + e);   }  } }
  </code></pre>
  
  <p>SQLTestForm.html</p>
  
  <pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"&gt;
  &lt;HTML&gt;
   &lt;HEAD&gt;
    &lt;TITLE&gt;SQL Test Form&lt;/TITLE&gt;
   &lt;/HEAD&gt;
   &lt;BODY&gt;
    &lt;H2&gt;Query Input:&lt;/H2&gt;
    &lt;FORM ACTION="/SQLTestForm" METHOD="POST"&gt;
    &lt;TABLE&gt;
     &lt;TR&gt;&lt;TD&gt;Driver:
     &lt;TD&gt;&lt;INPUT TYPE="TEXT" NAME="driver" VALUE="com.jdbc.mysql.Driver" SIZE="45"&gt;
     &lt;TR&gt;&lt;TD&gt;URL:
     &lt;TD&gt;&lt;INPUT TYPE="TEXT" NAME="url"
     VALUE="jdbc:mysql://localhost:3306/test" SIZE="45"&gt;
     &lt;TR&gt;&lt;TD&gt;Username:
     &lt;TD&gt;&lt;INPUT TYPE="TEXT" NAME="username"&gt;
     &lt;TR&gt;&lt;TD&gt;Password:
     &lt;TD&gt;&lt;INPUT TYPE="PASSWORD" NAME="password"&gt;
     &lt;TR&gt;&lt;TD VALIGN="TOP"&gt;Query:
     &lt;TD&gt;&lt;TEXTAREA ROWS="5" COLS="35" NAME="query"&gt;&lt;/TEXTAREA&gt;
     &lt;TR&gt;&lt;TD COLSPAN="2" ALIGN="CENTER"&gt;&lt;INPUT TYPE="SUBMIT"&gt;
    &lt;/TABLE&gt;
    &lt;/FORM&gt;
   &lt;/BODY&gt;
  &lt;/HTML&gt;
  </code></pre>