title: How to show data in a web page from DB?
body: <p>For the last three days I have been trying to develop a web based CRUD application which does all the CRUD operations. Given the complexity of the code, I decided to show the data first then I can move on to further CRUD operations. </p>
  
  <p>For this application I posted several questions in here. From the Answers I came this far. </p>
  
  <p>Here is my full source code. It's not showing any errors. But I'm not able to get the output.</p>
  
  <p>I have two class in my server side code. For me, its looks like good. But it needs something more to come alive. This is a great forum, please tell me, how can I overcome this.</p>
  
  <p><strong>1) EmpApp.java</strong>
  Just consider the getAllEmployee() method, omit the rest.</p>
  
  <pre><code> package test;
   import java.sql.Connection;
   import java.sql.DriverManager;
   import java.sql.ResultSet;
   import java.sql.SQLException;
   import java.sql.Statement;
   import java.util.Collection;
   import java.util.HashSet;
   import java.util.Set;
   import com.mysql.jdbc.PreparedStatement;
   public abstract class EmpApp implements Set&lt;Employee&gt;
  {
  public Set&lt;EmpApp&gt; empobj = new HashSet&lt;EmpApp&gt;();
  private Set&lt;Employee&gt; persons = new HashSet&lt;Employee&gt;();
  
  public boolean add(Employee employee) {
      return persons.add(employee);
    }
  
  public EmpApp(){
  
  }
  
  public Set&lt;EmpApp&gt; getAllEmp()
  {
      return empobj;
  }
  public void addEmployee(Employee employee) throws SQLException, ClassNotFoundException
  {
      int tempID = employee.getId();
      String tname = employee.getName();
      int tage = employee.getAge();
      String tdept = employee.getDept();
      PreparedStatement pstmt;
      Class.forName("com.mysql.jdbc.Driver");
      String url ="jdbc:mysql://localhost:3306/general";
      Connection con = DriverManager.getConnection(url,"root", "1234");
      System.out.println("URL: " + url);
      System.out.println("Connection: " + con);
      pstmt = (PreparedStatement) con.prepareStatement("INSERT INTO PERSON SET ID=?, NAME=?, AGE=?, DEPT=?");
      pstmt.setInt(1, tempID);
      pstmt.setString(2, tname);
      pstmt.setInt(3, tempID);
      pstmt.setString(4, tdept);
      pstmt.executeUpdate();
  }
  
  public void updateEmployee(Employee employee) throws SQLException, ClassNotFoundException
  {
      int tempID = employee.getId();
      String tname = employee.getName();
      int tage = employee.getAge();
      String tdept = employee.getDept();
      PreparedStatement pstmt;
      Class.forName("com.mysql.jdbc.Driver");
      String url ="jdbc:mysql://localhost:3306/general";
      Connection con = DriverManager.getConnection(url,"root", "1234");
      System.out.println("URL: " + url);
      System.out.println("Connection: " + con);
      pstmt = (PreparedStatement) con.createStatement();
      pstmt = (PreparedStatement) con.prepareStatement("UPDATE PERSON SET NAME=?, AGE=?, DEPT=? WHERE ID=?");
      pstmt.setInt(2, tage);
      pstmt.setString(1, tname);
      pstmt.setString(3, tdept);
      pstmt.setInt(4, tempID);
      pstmt.executeUpdate();
  }
  
  public void deleteEmployee(Employee employee) throws ClassNotFoundException, SQLException
  {
      int eid = employee.getId(); 
      PreparedStatement pstmt;
      Class.forName("com.mysql.jdbc.Driver");
      String url ="jdbc:mysql://localhost:3306/general";
      Connection con = DriverManager.getConnection(url,"root", "1234");
      System.out.println("URL: " + url);
      System.out.println("Connection: " + con);
      pstmt = (PreparedStatement) con.createStatement();
      pstmt = (PreparedStatement) con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
      pstmt.executeUpdate("DELETE FROM PERSON WHERE ID="+eid);
  
  }
  @SuppressWarnings("unchecked")
  private Set&lt;EmpApp&gt; getAllEmployee() throws ClassNotFoundException, SQLException
  {
      Employee employee = new Employee();
      Statement stmt;
      ResultSet rs;
      Class.forName("com.mysql.jdbc.Driver");
      String url ="jdbc:mysql://localhost:3306/general";
      Connection con = DriverManager.getConnection(url,"root", "1234");
      System.out.println("URL: " + url);
      System.out.println("Connection: " + con);
      stmt = con.createStatement();
      stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
      rs = stmt.executeQuery("SELECT * " + "from person ORDER BY ID");
      while(rs.next())
      {
          String name = rs.getString(2);
          employee.setName(name);
          int id = rs.getInt(1);
          employee.setId(id);
          String dept = rs.getString(4);
          employee.setDept(dept);
          int age = rs.getInt(3);
          employee.setAge(age);
          empobj.addAll((Collection&lt;? extends EmpApp&gt;) employee);
      }
      return empobj;
  }
  
   }
  </code></pre>
  
  <hr>
  
  <p><strong>2) Employee.java</strong></p>
  
  <pre><code>package test;
  public class Employee 
  {   
  private Set&lt;Employee&gt; employee = new HashSet&lt;Employee&gt;();
  public Employee() {
          // TODO Auto-generated constructor stub
  }
  private String name;
  private String dept;
  private int age;
  private int id;
  
  public int getId(){
      return id;
  }
  
  public void setId(int id){
      this.id = id;
  }
  
  public String getName(){
      return name;
  }
  
  public void setName(String name){
      this.name = Security.escapeHtml(name);;
  }
  
  public int getAge(){
      return age;
  }
  
  public void setAge(int age){
      this.age = age;
  }
  
  public String getDept(){
      return dept;   
  }
  
  public void setDept(String dept){
      this.dept = Security.escapeHtml(dept);
  }
  
  public boolean equals(Object obj){
      if (obj == null)
      {
          return false;
      }
  
      if (obj == this)
      {
          return true;
      }
  
      if (!this.getClass().equals(obj.getClass()))
      {
          return false;
      }
  
      Employee that = (Employee) obj;
  
      if (this.id != that.id)
      {
          return false;
      }
  
      return true;
  }
  public String toString(){
      return "Employee[id=" + id + ",name=" + name + "]";
  }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>The index.html code:</strong></p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
    &lt;title&gt;Employee Data&lt;/title&gt;
    &lt;script type='text/javascript' src='dwr/engine.js'&gt; &lt;/script&gt; 
    &lt;script type='text/javascript' src='dwr/util.js'&gt; &lt;/script&gt; 
    &lt;script type='text/javascript' src='dwr/interface/EmpApp.js'&gt; &lt;/script&gt; 
    &lt;script&gt;
    function init() {
    showTable();
  }
  
  var empCache = { };
  var viewed = -1;
  
  function showTable() {
    EmpApp.getAllEmployee(function(empobj) {
      dwr.util.removeAllRows("EmpRow", { filter:function(tr) {
        return (tr.id != "pattern");
      }});
      var employee, eid;
  
      for (var i = 0; i &lt; empobj.length; i++) {
        employee = empobj[i];
            eid = empobj.id;
        dwr.util.cloneNode("pattern", { idSuffix:id });
        dwr.util.setValue("ID" + id, employee.id);
        dwr.util.setValue("NAME" + id, employee.name);
        dwr.util.setValue("AGE" + id, employee.age);
        dwr.util.setValue("DEPT" + id, employee.dept);
        $("pattern" + id).style.display = "table-row";
        empCache[id] = employee;
      }
    });
  }
  
  
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body onload="init();"&gt;
  &lt;div id="tabContents"&gt;
  
    &lt;div id="demoDiv"&gt;
  
      &lt;h3&gt;Employee&lt;/h3&gt;
      &lt;table border="1" class="rowed grey"&gt;
        &lt;thead&gt;
          &lt;tr&gt;
            &lt;th&gt;ID&lt;/th&gt;
            &lt;th&gt;NAME&lt;/th&gt;
            &lt;th&gt;AGE&lt;/th&gt;
            &lt;th&gt;DEPT&lt;/th&gt;
          &lt;/tr&gt;
        &lt;/thead&gt;
        &lt;tbody id="EmpRow"&gt;
          &lt;tr id="pattern" style="display:none;"&gt;
          &lt;td&gt;
              &lt;span id="ID"&gt;Name&lt;/span&gt;&lt;br/&gt; 
              &lt;span id="NAME"&gt;Name&lt;/span&gt;&lt;br/&gt; 
              &lt;span id="AGE"&gt;Age&lt;/span&gt; 
            &lt;/td&gt; 
        &lt;td&gt;&lt;span id="DEPT"&gt;Dept&lt;/span&gt;&lt;/td&gt;
          &lt;/tr&gt;
        &lt;/tbody&gt;
      &lt;/table&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>Of course its Asynchronous web application. I am using DWR here.
  The dwr.xml file</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE dwr PUBLIC  
  "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"  
  "http://getahead.org/dwr/dwr20.dtd"&gt;  
  
  &lt;dwr&gt;
    &lt;allow&gt;
      &lt;create creator="new" javascript="EmpApp" scope="script"&gt;
        &lt;param name="class" value="test.EmpApp"/&gt;
        &lt;include method="GetAllEmployee"/&gt;
      &lt;/create&gt;
      &lt;convert match="test.EmpApp" converter="bean"/&gt;
    &lt;/allow&gt;
  &lt;/dwr&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>And the Final web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
  &lt;display-name&gt;LastDisplay&lt;/display-name&gt;
     &lt;servlet&gt;
      &lt;servlet-name&gt;dwr-invoker&lt;/servlet-name&gt;
      &lt;servlet-class&gt;uk.ltd.getahead.dwr.DWRServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;debug&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;dwr-invoker&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/dwr/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  &lt;/web-app&gt;
  </code></pre>