This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes JSP and the fly likes ListBox Display Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "ListBox Display" Watch "ListBox Display" New topic
Author

ListBox Display

m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
Hi All,
I have a jsp page that has 2 listboxes the first one is a list for the user to choose from and works fine. The second one is a list from a database. I have taken the resultset and put it into an array but I keep getting the last element in the array. Can someone take a look at my code and tell me what I am doing wrong? I am just not seeing it. Thanks!

ArrayList language = new ArrayList();
String lang = "";
List list = new ArrayList();
String s = " ";
try{

ResultSet rs = statement.executeQuery("Select partid, language from partDB.test "
+ "where partid = '" + pid + "' ");
while (rs.next()) {

language.add(rs.getString("language"));

for (int i = 0; i < language.size(); i++) {
String LString = language.get(i).toString();
lang = lang + LString.trim() ;
}

s = (rs.getString("language")); list.toArray(new String[list.size()]);


} }


<td><select size="10"
id="selectedOptions"
name="Selected Language(s)">
<option value='<%=s%>'><%=s%></option>
</select>
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you're going to perform the database call from the JSP, why not just build the string while you're looping through it?



Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
Hadn't considered it, I will give it a try..
m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
Hi Ben,
I tried the little code snippet you sent but it doesn't seem to like the option line, can you tell if my syntax is correct?


StringBuffer options = new StringBuffer();
ResultSet rs = statement.executeQuery("Select partid, language from partDB.test "
+ "where partid = '" + pid + "' ");
while (rs.next()) {
options.append("<option value=\"" + rs.getString("language") + "\"> + rs.getString("language") + </option>\n ");
}
%>
<select name="NAME"> <%=options.toString()%>
</select>


Thanks!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The close option tag is not quoted:
..rs.getString("language") + "</option>\n ");


PS: if you use the Code Button when posting in this forum, it will be easier for others to read your code (indents will be preserved).
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410



Missing a quote here too
m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
Thanks so much Ben, it is appearing correctly and so much easier then the extra step of the array. Sorry about the code format, I will remember to use the code button next time.
m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
Our posts crossed, yes, I discovered the missing quote to.. Thanks again!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: ListBox Display
 
Similar Threads
Passing parameters from jsp to servlet
dependent select list options
Error compiling JSP
select element as parameter for query
jsp