2

I have a resultSet that has a different number of results every time, and needs to be used multiple times on my page. Currently, I am storing my results in an ArrayList, and was planning to just loop through the arraylist. I dont know how many rows So this is what I have so far:

  while (result.next()) {    
      tmpTerms.add(term = (((result_data = result.getObject("val_internal_code"))==null || result.wasNull())?" ":result_data.toString()));
      tmpTerms.add(desc = (((result_data = result.getObject("val_external_representation"))==null || result.wasNull())?" ":result_data.toString()));
      tmpTerms.add(sorter = (((result_data = result.getObject("sorter"))==null || result.wasNull())?" ":result_data.toString()));
      tmpTerms.add(sDate = (((result_data = result.getObject("sDate"))==null || result.wasNull())?" ":result_data.toString()));
  }

Okay, so when I run this, the system prints: Code:

[2011SP, Spring 2011, 1, 11-15-2010, 2011SU, Summer 2011, 1, 01-15-2011, 2011FL, Fall 2011, 1, 04-01-2011, 2010Q2, CE Qtr 2 2010 Dec - Feb, 2, 08-01-2010, 2011Q3, CE Qtr 3 2011 Mar - May, 2, 11-01-2010, 2011Q4, CE Qtr 4 2011 Jun - Aug, 2, 02-01-2011, 2011Q1, CE Qtr 1 2011 Sep-Nov, 2, 05-01-2011]

I dont know if this is the correct way of doing it or not, but it is working so far, so what I want to do now is use my allTerms arrayList, and separate them into rows, so I want to split every 4 results into a separate row. (So it would be 2011SP, Spring 2011, 1, 11-15-2010 as one row and so on), next, I would need to transfer these results to parts of a HTML page. so for ex:

<table class="t1">
  <tr>
    <td><!--Here I would want to show all rows from the allTerms arrayList with a "sorter" of 1--></td>
  </tr>
</table>
<table class="t2">
  <tr>
    <td><!--Here I would want to show all rows from the allTerms arrayList with a "sorter" of 1 and a term of ....SU (where '....' is the year) --></td>
  </tr>
</table>
Mr Man
  • 1,498
  • 10
  • 33
  • 54
  • You seem to have forgotten to ask a question :) – joostschouten Jun 16 '11 at 16:31
  • "so what I want to do now is use my allTerms arrayList, and separate them into rows, so I want to split every 4 results into a separate row. (So it would be 2011SP, Spring 2011, 1, 11-15-2010 as one row and so on), next, I would need to transfer these results to parts of a HTML page. so for ex:" That is basically my question.....sorry it wasnt in question form. I would just like to figure out how to do that – Mr Man Jun 16 '11 at 16:32
  • oops, my bad. I just read part of it. BalusC seems to have answered it. – joostschouten Jun 16 '11 at 16:37

2 Answers2

9

That's not entirely right. You need to create a Javabean class which represents a single entity (read: a class which contains all column data of a single database row).

E.g.

public class Term {
    private String code;
    private String description;
    private int sorter;
    private Date date;

    // Add/generate getters, setters, equals, hashcode and other boilerplate.
}

And populate it as follows:

List<Term> terms = new ArrayList<Term>();
// ...

while (resultSet.next()) {
    Term term = new Term();
    term.setCode(resultSet.getString("val_internal_code"));
    term.setDescription(resultSet.getString("val_internal_representation"));
    term.setSorter(resultSet.getInt("sorter"));
    term.setDate(resultSet.getDate("sDate"));
    terms.add(term);
}

// ...
request.setAttribute("terms", terms);

Then you can access it nicely using JSTL/EL.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<table class="t1">
  <tr>
    <td>
      <c:forEach items="${terms}" var="term">
        <c:if test="${term.sorter == 1}">
          ${term.code}, ${term.description}, ${term.date}
        </c:if>
      </c:forEach>
    </td>
  </tr>
</table>
<table class="t2">
  <tr>
    <td>
      <c:forEach items="${terms}" var="term">
        <c:if test="${term.sorter == 1 and fn:endsWith(term.code, 'SU')}">
          ${term.code}, ${term.description}, ${term.date}
        </c:if>
      </c:forEach>
    </td>
  </tr>
</table>

See also:


Unrelated to the problem, the way as you traversed the ResultSet didn't give me a strong feeling that the datamodel is properly been designed. Ensure that you're using the right column type for the data it holds. Ensure that you're putting the right non-null and key constraints on the columns. I'd also reconsider the column names and try to be more self-documenting and consistent.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

I'm going to give you a solution that differs from @BalusC excellent one by assuming you have a differing amount of columns in your data.

If you have a different number of columns you can go a route where you use a Key-Value data structure, i.e. a Map of some sort and then store those maps in a List.

ArrayList<HashMap<String, String>> rows = new ArrayList<HashMap<String,String>>();

while(resultSet.next()) {
    HashMap<String, String> term = new HashMap<String, String>();
    term.put("val_internal_code",resultSet.getString("val_internal_code"));
   term.put("val_internal_representation", resultSet.getString("val_internal_representation"));
    term.put("sorter",resultSet.getInt("sorter"));
    term.put("date",resultSet.getDate("sDate"));
    rows.add(term);
}

Then when you go to print your data:

for(HashMap<String, String> row : rows){
   for(String key : row.keySet()){
       System.out.println(row.get(key));
   }
   System.out.println("End of line");
}
dmcnelis
  • 2,913
  • 1
  • 19
  • 28
  • 1
    JSTL `` also eats maps: http://stackoverflow.com/questions/1835683/loop-through-hashmap-java-jsp – BalusC Jun 16 '11 at 16:46