0

I have a sql statement that pulls infomration about tagnum's for individual pidm's. Every pidm can have multiple tagnum's so I am dropping the information into an arraylist. I want to display the contents of this arraylist in a dropdown box on a html page.

Here is the code for the arraylist:

       <table style="border:transparent" style="width:100%">
            <tr>
             <td style ="width: 300px;">
             <select style="width:150px;"tabindex="5" name="Tag">
                 <option></option>
<%} rscheck.close();
     ResultSet rsTagCheck = stmt.executeQuery("SELECT PARKING.XKRPRMT.XKRPRMT_PIDM, PARKING.XKRPRMT.XKRPRMT_STATUS, PARKING.XKRPRMT.XKRPRMT_EXPIRE_YR, PARKING.XKRPRMT.XKRPRMT_TAG FROM PARKING.XKRPRMT WHERE XKRPRMT_PIDM ='" + BannerID + "'");

     while (rsTagCheck.next()){
            ArrayList<String> myTag = new ArrayList<String>();
            myTag.add(rsTagCheck.getString("XKRPRMT_TAG"));         
%>              
                  <option><%= myTag.get(0) %></option>
             </select>
             </td>

I can get the first element to show in the drop down box, but anything after that show an outofbounds exception. I want to know how to display ALL of the information in the arraylist.

@Pointy I did that and all I got was this:

alt text

It put the first one in there, but the rest would not populate!!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
gary A.K.A. G4
  • 1,013
  • 8
  • 23
  • 36

2 Answers2

2

There's no reason to create the array list at all.

while (rsTagCheck.next()) {
  %>
  <option><%= rsTagCheck.getString("XKRPRMT_TAG") %></option>
  <%
}

edit — of course in practice you should be careful about what those strings might contain. If the strings come from some sort of user input, you shouldn't be just dumping them unwashed into the HTML. That's a whole other subject however.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • I tried that and I posted the image in the question. It didn't populate past the first tagNum!!! The rest diplayed outside of the dropdown box. – gary A.K.A. G4 Oct 08 '10 at 14:47
  • 1
    @gary: You should print the `` only *after* the `while` loop, not inside. Further, your JSP/JDBC code as it is in the question is recipe for future trouble. I strongly recommend to put everything aside and get yourself through a *decent* JSP/Servlet/JDBC book/tutorial. – BalusC Oct 08 '10 at 16:25
  • @BalusC is correct - scriptlet code like that is a terribly outdated way of writing JSP pages. – Pointy Oct 08 '10 at 20:54
1

Don't use scriptlets, use jstl tags and in this case

<c:forEach var="myTag" items="${rsTagCheck}">

and

<c:out value="${myTag.getString('XKRPRMT_TAG')}" />

Actually looking again at your code, I would not put the db query in a scriptlet! DB access should not be done here, pass the resultsets to jsp from servlet, and loop through data using jstl.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • 1
    To expand on this answer, here's a [link](http://stackoverflow.com/questions/3177733/howto-avoid-java-code-in-jsp-files). – BalusC Oct 08 '10 at 16:26