title: Printing MySQL Query Results to JSP from Java Servlet
body: <p>Right now I am facing the following two problems with this script.</p>
  
  <p>1) When I run a command like <code>SELECT * FROM table;</code> it only returns the column names. When I run a command like <code>SELECT columnName FROM table;</code>, it displays the output perfectly. What could be causing this error?</p>
  
  <p>2) When I run an <code>INSERT</code> or <code>DELETE</code> command, I get my feedback message saying the database was updated, but when checking the database, no changes have been made. Can anyone see what's wrong with that clause in my source code?</p>
  
  <p>Below are my .java and .jsp files respectively:</p>
  
  <p>databaseServlet.java</p>
  
  <pre><code>import javax.servlet.*;
  import javax.servlet.http.*;
  import java.io.*;
  import java.sql.*;
  import java.util.ArrayList;
  
  @SuppressWarnings("serial")
  
  public class databaseServlet extends HttpServlet {
      private Connection conn;
      private Statement statement;
  
      public void init(ServletConfig config) throws ServletException {
          try {
              Class.forName(config.getInitParameter("databaseDriver"));
              conn = DriverManager.getConnection(
                      config.getInitParameter("databaseName"),
                      config.getInitParameter("username"),
                      config.getInitParameter("password"));
              statement = conn.createStatement();
          }
          catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          ArrayList&lt;String[]&gt; out = new ArrayList&lt;String[]&gt;();
          ArrayList&lt;String&gt; columns = new ArrayList&lt;String&gt;();
  
          String query = request.getParameter("query");
  
          if (query.toString().toLowerCase().contains("select")) {
              //SELECT Queries
              try {
                  ResultSet resultSet = statement.executeQuery(query.toString());
                  ResultSetMetaData metaData = resultSet.getMetaData();
                  int numberOfColumns = metaData.getColumnCount();
                  for(int i = 1; i&lt;= numberOfColumns; i++){
                      columns.add(metaData.getColumnName(i));
                  }
  
                  while (resultSet.next()){
                      String[] row = new String[numberOfColumns];
                      for (int i = 0; i &lt; numberOfColumns; i++){
                          row[i] = (String) resultSet.getObject(i+1);
                      }
                      out.add(row);
                   }
              }
              catch (Exception f) {
                  f.printStackTrace();
              }
          }
          else if (query.toString().toLowerCase().contains("delete") || query.toString().toLowerCase().contains("insert")) {
              //DELETE and INSERT commands
              try {
                  conn.prepareStatement(query.toString()).executeUpdate(query.toString());
                  columns.add("\t\t Database has been updated!");
              }
              catch (Exception l){
                  l.printStackTrace();
              }
          }
          else {
              //Not a valid response
              columns.add("\t\t Not a valid command or query!");
          }
          request.setAttribute("query", query);
          request.setAttribute("resultSize",  out.size());
          request.setAttribute("queryResults", out);
          request.setAttribute("queryColumns", columns);
          RequestDispatcher dispatcher = request.getRequestDispatcher("/dbServlet.jsp");
          dispatcher.forward(request,  response);
      }
  }
  </code></pre>
  
  <p>dbServlet.jsp</p>
  
  <pre><code>&lt;?xml version = "1.0"?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
  
  &lt;!-- dbServlet.html --&gt;
  &lt;html xmlns = "http://www.w3.org/1999/xhtml"&gt;
  &lt;%@ page import="java.util.ArrayList" %&gt;
  &lt;head&gt;
      &lt;title&gt;MySQL Servlet&lt;/title&gt;
      &lt;style type="text/css"&gt;
          table{border: 1px solid #FFF;}
          td{border: 1px dashed #FFF; padding: 3px; background-color: #5A5F64;}
          body{background-color: #182229; text-align: center; color: #FFF;}
          h1{font-family:"Arial", sans-serif, Helvetica;}
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h1&gt;MySQL Servlet User-Interface&lt;/h1&gt;
      &lt;div style="width:500px; margin-right:auto; margin-left: auto; background-color:#131B21; padding: 20px;"&gt;
          &lt;h3&gt;Enter an SQL Command!&lt;/h3&gt;
          &lt;form action = "/database/database" method = "post"&gt;
          &lt;p&gt;
              &lt;textarea name = "query" cols="28" rows="5"&gt;&lt;/textarea&gt;
          &lt;/p&gt;
          &lt;p&gt;
              &lt;input type = "submit" value = "Run MySQL Servlet" /&gt;
              &lt;input type = "reset" value = "Clear Command" /&gt;
          &lt;/p&gt;
          &lt;/form&gt;
          &lt;hr&gt;
          &lt;TABLE id="results" align="center"&gt;
              &lt;%
                  ArrayList&lt;String&gt; columns = (ArrayList&lt;String&gt;)request.getAttribute("queryColumns");
                  ArrayList&lt;String[]&gt; results = (ArrayList&lt;String[]&gt;)request.getAttribute("queryResults"); 
                  out.println("&lt;TR align='left'&gt;");
                  if(columns != null &amp;&amp; !columns.isEmpty()){
                      for(String columnName: columns ){
                         out.println("&lt;TD&gt;"+columnName+"&lt;/TD&gt;");
                      }
                  }
                  out.println("&lt;/TR&gt;");
                  //print data
                  if(results != null &amp;&amp; !results.isEmpty()){
                      for(String[] rowData: results){
                         out.println("&lt;TR align='left'&gt;");
                         for(String data: rowData){
                            out.println("&lt;TD&gt;"+data+"&lt;/TD&gt;");
                         }
                         out.println("&lt;/TR&gt;");
                      }
                  }
              %&gt;
          &lt;/TABLE&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>