You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Konstantina Stamopoulou <st...@intracom.gr> on 2001/11/07 15:12:50 UTC

Logic:iterate tag problem

Hello,
This might be an easy task but it has taken me a lot of time and I cannot find the solution. This is the situation:

I'm using a datasource in stuts-config to connect to my database.
Then, from within an Action class I'm trying to retrieve the data and display them in a .jsp using the iterate tag.
The code that I'm using to retrieve the data is the following:

String query="SELECT * FROM Employee";
 String[] Column;
 String[] Row;
 DbBean db=new DbBean();
 int i;
try{
   HttpSession session = request.getSession(true);   
   javax.sql.DataSource datasource= servlet.findDataSource(null);
   java.sql.Connection dbCon=datasource.getConnection();  /*create a statement from the connection */
   Statement state=dbCon.createStatement();
   ResultSet result=state.executeQuery(query);
          ResultSetMetaData metadata=result.getMetaData();
          int col=metadata.getColumnCount();
          
          /* A hashtable that holds the data for each employee*/
          
          database databas=new database();
   
   /*retrieve the name of columns*/
   
      Column=new String[col];
      for (i=0;i<col;i++){
      Column[i]=metadata.getColumnName(i+1);
  }
    
  Row=new String[col]; 
  // Get the data from each row
  int j=0;  
      while (result.next()){
       for (i=0;i<=col-1;i++){
       Row[i]=result.getString(metadata.getColumnName(i+1));
    }
    for (i=0;i<=col-1;i++){
    db.setEmployeeName(Row[i]);
    db.setEmployeePicLoc(Row[i+1]);
    i++;
   }
      databas.addData(db); 
   } 
   
   if  (session.getAttribute("bean")== null){
    session.setAttribute("bean",db);
    }
                   
   if (session.getAttribute("databas")== null){
    session.setAttribute("databas",databas);
   }
   

DbBean and database are the beans that I use to get the data. 


public final class database implements Serializable
{
  Vector data =new Vector();
  
    public DbBean[] getdata() {

 (data) {
     DbBean results[] = new DbBean[data.size()];
     Enumeration emplo = data.elements();
     int n = 0;
     while (emplo.hasMoreElements()) {
  results[n++] = (DbBean) emplo.nextElement();
     }
     return (results);
 }

}

 public void addData(DbBean bean){
  data.add(bean);
}


In the .jsp file I use the following

<logic:iterate name="databas" property="data" id="bean">
  <tr>
    <td align="left">
      <bean:write name="bean" property="EmployeeName" filter="true"/>
    </td>
    <td align="left">
      <bean:write name="bean" property="EmployPicLoc" filter="true"/>
    </td>
    <td align="center">
    </td>
  </tr>
</logic:iterate>

Unfortunately I don't get any data in the display. 
Can someone tell me what I'm doing wrong?
I tried to use a database servlet in web.xml but I get the error : cannot load servlet:database:null. 
  
Many thanx,
Konstantina