title: Java Servlet to Create and Sort a MySQL DB using HTML Form
body: <p>I have a very beginner question as I'm just learning to work with DBs. I have a very simple HTML form with a textField for a name, an add button to add a name to a MySQL database, and a sort button to sort the names in the database and display them. I almost have it working, however when I hit the "add" or "sort" buttons I get the same messages that appear after a user clicks those buttons along with the sorted list of names. It looks like this when clicking the add button: 
  Please go back and add a name or sort
  Bill Jones 
  David G 
  Debbie Downer 
  Jane Doe </p>
  
  <p>Would someone be able to advise me on what I have to change on my code to make just the list show up by itself when the user clicks sort?  I am not a programmer just someone trying to learn.</p>
  
  <p>Servlet code:</p>
  
  <pre><code>import java.io.IOException;
  import java.io.PrintWriter;
  
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.sql.*;
  
   @WebServlet("/Main")
  public class Main extends javax.servlet.http.HttpServlet implements
      javax.servlet.Servlet {
  static final long serialVersionUID = 1L;
  
  public Main() {
      super();
  }
  
  protected void doGet(HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException     {
      PrintWriter out = response.getWriter();
      String name = request.getParameter("name");
      try {
          Class.forName("com.mysql.jdbc.Driver").newInstance();
  
          Connection con = DriverManager
                  .getConnection("jdbc:mysql://localhost:3306/test?user=root&amp;password=root");
          Statement stmt = con.createStatement();
          try {
  
              //stmt.execute("DROP TABLE simple");
              stmt.execute("CREATE TABLE IF NOT EXISTS simple( name char(30))");
          } catch (Exception e) {
          }
          if (name == "" || name == null) {
              out.print("&lt;h1&gt;Please enter a name.&lt;h1&gt;");
          }
  
          else if (name != null) {
              String s = "Insert into simple values(\'" + name + "\')";
              stmt.execute(s);
              out.print("&lt;h1&gt;Please go back and add a name or sort.&lt;h1&gt;");
          }
  
          ResultSet rs = stmt.executeQuery("Select * FROM simple ORDER BY name");
          while (rs.next())
              out.println("&lt;h1&gt;" + rs.getString(1) + "&lt;/h1&gt;");
  
      } catch (Exception ex) {
          System.out.println(ex);
  
      }
      System.out.println("Program terminated with no error.");
  }
  
  protected void doPost(HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException      {
      // TODO Auto-generated method stub
  }
  
  }
  </code></pre>
  
  <p>HTML code:</p>
  
  <pre><code> &lt;!-- Main.html --&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;DB Sort&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form action="http://localhost:8080/DB_Sorts/Main"&gt;
  &lt;center&gt;
  &lt;br&gt;&lt;br&gt;
  User Name:
  &lt;input name="name" type="text" value=""&gt;
  &lt;br&gt;&lt;br&gt;
  &lt;input name="add" type="Submit" value="Add"&gt;
  &lt;input name="sort" type="Submit"    value="  Sort  "&gt;    
  &lt;/center&gt;
    &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>