Solved

How to write my list box items to a table?

Posted on 2008-06-10
15
282 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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

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!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

690 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