title: How to display a data table in a JSP page Struts2
body: <p>So I have been reading on this topic for a couple of days now, and every time I think I understand it I get confused again.</p>
  
  <p>So what I have: A MySQL data base, eclipse, tomcat apache, the beginnings of a GUI using STRUTS2</p>
  
  <p>I am setting up a gui for said database and I want to display one of the data tables in my JSP page. So my problem... well I have a lot of problems but initially, I have a form that is I have an action sending me to the JSP page I want to display my table in. I thought I would use another action to connect to the database and obtain all my information, but I'm not sure if that makes sense anymore, should I just use the same action? </p>
  
  <p>Well... here are bits of my code:</p>
  
  <p>Action to get the data:</p>
  
  <pre><code>package net.upsmon.struts2.action;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Properties;
  
  import com.opensymphony.xwork2.ActionSupport;
  
  public class Table extends ActionSupport  {
  
  List tableRows=new ArrayList();
  public String execute() throws InstantiationException, IllegalAccessException, ClassNotFoundException{
      List tableInfo=new ArrayList();
      Properties props = new Properties();
      props.setProperty("user", "user");
      props.setProperty("password", "pass");
      props.setProperty("databaseName", "db"); 
      String dbUrl="jdbc:mysql://db:3306/db";
      String ret = ERROR;
      Connection myCon = null;
      try{
          Class.forName ("com.mysql.jdbc.Driver").newInstance();
          myCon = DriverManager.getConnection("jdbc:mysql://dburl:3306/mqmonitordb",props);
          String dbQuery = "SELECT * FROM table";
          PreparedStatement ps = myCon.prepareStatement(dbQuery);
          ResultSet result=ps.executeQuery(dbQuery);
          while(result.next()){
  
              tableInfo.add(result.getString("raisedAlertId"));
              tableInfo.add(result.getString("alertHostIP"));
              tableInfo.add(result.getString("portNumber"));
              tableInfo.add(result.getString("qMgrName"));
              tableInfo.add(result.getString("alertType"));
              tableInfo.add(result.getString("alertGroupName"));
              tableInfo.add(result.getString("alertEntity"));
              tableInfo.add(result.getString("maintMode"));
              tableInfo.add(result.getString("alertCreatedTime"));
              tableInfo.add(result.getString("alertRaisedTime"));
              tableInfo.add(result.getString("alertAckFlag"));
              tableRows.add(tableInfo); 
              tableInfo.clear();
          }
          //request.setAttribute("tableRows",tableRows);
  
  
      }catch(SQLException sqe){
  
      }catch(Exception e){
  
      }
  
  
      return "yay";
  }
  public List tableRows() {  
      return tableRows();  
  }  
  public void setTableRows(List tableRows) {  
      this.tableRows = tableRows;  
  }
  
  }
  </code></pre>
  
  <p>JSP page:</p>
  
  <pre><code>&lt;%@ page contentType="text/html; charset=UTF-8"%&gt;
  &lt;%@ taglib prefix="s" uri="/struts-tags"%&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;link type="text/css" rel="stylesheet" href="stylesheet.css" /&gt;
  &lt;title&gt;Welcome&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
     &lt;h2&gt;
         Welcome 
     &lt;/h2&gt;
  
  
     &lt;table&gt;
         &lt;thead&gt;
             &lt;tr&gt;
                 &lt;th&gt;&lt;/th&gt;
                 &lt;th&gt;column1&lt;/th&gt;
                 &lt;th&gt;column2&lt;/th&gt;
  
  
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;s:iterator status="stat" value="tableRows"&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;s:property value="#stat.index+1"&gt;&lt;/s:property&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;s:property value="%{column1}"&gt;&lt;/s:property&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;s:property value="%{column2}"&gt;&lt;/s:property&gt;&lt;/td&gt;
  
              &lt;/tr&gt;
          &lt;/s:iterator&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  
  
  &lt;s:form action="logout" method="post" margin="auto"&gt;
      &lt;s:submit method="authenticate" key="label.logout" align="center" /&gt;
  &lt;/s:form&gt;
  &lt;s:form action="tolevelselector" method="post" margin="auto"&gt;
      &lt;s:submit method="authenticate" key="label.levelthree" align="center" /&gt;
  &lt;/s:form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Any advice would be great, thanks....</p>