Solved

How to write my list box items to a table?

Posted on 2008-06-10
15
277 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Starting to learn JAVA, 7 60
Java Server Faces parameter pass? 6 39
Error trying to install RTMT Win7 5 41
servlet filter example 37 46
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now