?
Solved

How to write my list box items to a table?

Posted on 2008-06-10
15
Medium Priority
?
283 Views
Last Modified: 2010-04-21
I have a list box in a form.
When I pass the form to my insert JSP page, only the first item choosen in the list box gets written to the table.

What do I need to change to write each selection that is chosen in the list box, to the GROUPS field in the table?

Here is my list box:
<SELECT name="GROUPS" multiple>
<OPTION value="1 ">WEST</OPTION>
<OPTION value="2 ">NORTH</OPTION>
<OPTION value="3 ">SOUTH</OPTION>
</select>  

Here is my insert statement:
<%@ page import="java.sql.*,javax.sql.*,javax.naming.*"%>
<%@ page import="java.util.*" %>
<%@ page errorPage = "errpage.jsp" %>
<%
String MY_GROUPS = request.getParameter("GROUPS");
%>
<%
{
javax.sql.DataSource ds;
java.sql.Connection conn = null;
Context ic = new InitialContext();
ds = (javax.sql.DataSource) ic.lookup("java:comp/env/jdbc/mydb2");
conn = ds.getConnection();
conn.setAutoCommit(false);
java.sql.Statement stmt = conn.createStatement();      
String sql = ("INSERT INTO MYTABLE(GROUPS)
VALUES('" + MY_GROUPS + "')");
        stmt.executeUpdate(sql);
 stmt.close();
 conn.commit();
 conn.close();
}
%>
0
Comment
Question by:g118481
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 21751550
>>request.getParameter("GROUPS");

should be

request.getParameterValues("GROUPS");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21751577
and

String[] MY_GROUPS = request.getParameter("GROUPS");

(which you'll need to loop through)
0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 21751599
just to add, the return is a String[] and not String
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:g118481
ID: 21751663
I am confused by your answer(s).
Can you edit my code above to further explain.

Thanks
0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 21751736
@CEHJ - ur comment was not there wen i put my comments :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21751841
>>Can you edit my code above to further explain.

That's not possible without knowing how you plan to insert an array into a single column
0
 
LVL 1

Author Comment

by:g118481
ID: 21752014
I did not realize it would be this difficult to write the selections from a list box to a table field.

I thought this would be a simple task.
Currently, I have a text field that the user inputs their selections, delimited by a space.
Since relying on the user to input correct data is prone to incorrect data from the user, I thought by offering a list box this issue would be corrected.
All I want to do is write the selections that the user makes, to the table field.

Is there an example somewhere?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21752233
>>I did not realize it would be this difficult to write the selections from a list box to a table field.

It isn't difficult, but as i said, we don't know what format is needed to write multiple values to a single column (an operation that could mean in itself that something is amiss with your design)
0
 
LVL 1

Author Comment

by:g118481
ID: 21752402
I now can output the values of the list box.
Maybe if I could convert the output to a string, then I could plug it into my insert query.

I think all I need now is a string format that I can plug into my insert query.
Any ideas on how to convert it to a string?

<%
String[] MY_GROUPS = request.getParameterValues("GROUPS");
for (int i=0;i<MY_GROUPS.length;i++) { out.println(MY_GROUPS[i]); }  
%>
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21752543
String items = java.util.Arrays.toString(theArray);
0
 
LVL 1

Author Comment

by:g118481
ID: 21752707
I tried your,  String items = java.util.Arrays.toString(theArray);

But I get this error:
" toString() in java.lang.Object cannot be applied to (java.lang.String[])       String items = java.util.Arrays.toString(MY_GROUPS);  "

I put your suggestion into a test page to see what the output would be.
Here is my test page that receives the form input:

<%@ page import="java.sql.*,javax.sql.*,javax.naming.*"%>
<%@ page import="java.util.*" %>
<%@ page errorPage = "errpage.jsp" %>

<% String[] MY_GROUPS = request.getParameterValues("CM_GROUPS"); %>
<% for (int i=0;i<MY_GROUPS.length;i++) { out.println(MY_GROUPS[i]); } %>

<% String items = java.util.Arrays.toString(MY_GROUPS); %>

<%= items %>
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 21753213
Try the following instead:
StringBuffer sb = new StringBuffer();
for (int i=0;i<MY_GROUPS.length;i++) { 
	out.println(MY_GROUPS[i]); 
	sb.append(MY_GROUPS[i]);
	if (i < (MY_GROUPS.length - 1)) {
		sb.append(",");
	}
}
String items = sb.toString();

Open in new window

0
 
LVL 1

Author Comment

by:g118481
ID: 21753442
CEHJ,

You last suggestion works PERFECT!!!

Thank you for your time and efforts.
0
 
LVL 1

Author Closing Comment

by:g118481
ID: 31465751
Great Solution!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21755207
:-)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question