Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple columns returned to <option>

Status
Not open for further replies.

timmbo

Programmer
Feb 22, 2001
167
US
Hi all,
Using jsp return a sql statement with multiple columns. Would like to display them (unitid, storageLen) in a <select>. How do I go about doing this???
Here is my code loading the sql statement to variables.

prepStmt = conn.prepareStatement(&quot;Select ent.incustdashid, ent.unitid, unit.storageLen &quot; +
&quot;From entitlements ent, company_service_unit unit &quot; +
&quot;Where ent.companyID = unit.companyID &quot; +
&quot;And ent.svcNum = unit.svcNum &quot; +
&quot;And ent.unitID = unit.unitID &quot; +
&quot;And ent.unitType = unit.unitType &quot; +
&quot;And s_usid = '900100p' &quot; +
&quot;And opid = 'admin2' &quot; +
&quot;Order By unitid&quot;);

ResultSet resultSet = prepStmt.executeQuery();
while(resultSet.next()) {
yaccounts.addElement(resultSet.getString(&quot;unitid&quot;));
ystorageLen.addElement(resultSet.getString(&quot;storageLen&quot;));
public Vector getyAccounts() {
return yaccounts;
}
public Vector getystorageLen() {
return ystorageLen;
}
Here is my code for the <select>:
<select name=&quot;AvailAccounts&quot; multiple size=&quot;10&quot; style=&quot;width:240px;&quot; width=&quot;235&quot;>
<%
for(int i=0;i<accountView.getnAccounts().size();i++) {
%>
<option><%= &quot;[&quot; + accountView.getnAccounts().elementAt(i) + &quot;]&quot; + &quot; &quot; + &quot;[&quot; + &quot;Bai Information will be placed here...&quot; + &quot;]&quot; %></option>
<%
}
%>
</select>
I am getting the accounts ok, but I need to add the column storageLen in with accounts.
Any help would be appreciated.

Thanks.
 
I may be wrong but from what I get from your post is that you consider the multiple attribute of the select tag in HTML to give you multiple columns. This is not the case, it lets you select more than just one column at any given time.

If in fact you want a select box with:
account in one column, and storagelen in another you will need to either have two select boxes side by side or just put the contents of both values in the same option tag such as:
<select>
<option>account storagelen</option>
</select>


-- IN SAME SELECT BOX --
[tt]
Code:
  // you should grab vector one time... in loop you are
  // doing more work than preferable
  Vector accounts   = accountView.getyAccounts();
  Vector storagelen = accountView.getystorageLen();

  out.println(&quot;<select>&quot;);
  for(int b=0; b<accounts.size(); b++) {
    out.print(&quot;<option>&quot; + account.elementAt(i));
    out.print(&quot; &quot; + storagelen.elemntAt(i) + &quot;</option>\n&quot;);
  }
  out.println(&quot;</select>&quot;);
[/tt]


-- IN TWO SELECT BOXES --
[tt]
Code:
  // you should grab vector one time... in loop you are
  // doing more work than preferable
  Vector accounts   = accountView.getyAccounts();
  Vector storagelen = accountView.getystorageLen();

  out.println(&quot;<select>&quot;);
  for(int i=0; i<accounts.size(); i++) {
    out.print(&quot;<option>&quot; + account.elementAt(i)) + &quot;</option>\n&quot;);
  }
  out.println(&quot;</select>&quot;);

  out.println(&quot;<select>&quot;);
  for(int j=0; j<accounts.size(); j++) {
    out.print(&quot;<option>&quot; + storagelen.elementAt(i)) + &quot;</option>\n&quot;);
  }
  out.println(&quot;</select>&quot;);
[/tt]
I hope this helped! ;-)
- Casey Winans
 
Once again thanks Casey for your help.

Yes, I want the two columns in the same select box. Your example will work great.

I'm very new to all this and plugging along. Have a good one.

Thanks,
Tim
 
Hi Casey,

Excuse me for seeming thick. Could you elaborate on where you stated &quot;my loop is doing more work than preferable.&quot;

Where would I place --
Vector accounts = accountView.getyAccounts();
Vector storagelen = accountView.getstorageLen();

What would I eliminate??

Any advice, suggestions and help appreciated.

Tim
 
Put the Vector declaration before your loop.
You were calling the class method that returned these Vectors every time the loop iterated... which had to keep grabbing the vector (gets expensive after several iterations).

You wouldn't eliminate a whole lot but your code is more efficient.

Later. I hope this helped! ;-)
- Casey Winans
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top