Solved

How to write my list box items to a table?

Posted on 2008-06-10
15
276 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
Comment Utility
>>request.getParameter("GROUPS");

should be

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

Expert Comment

by:CEHJ
Comment Utility
and

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

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

Expert Comment

by:mukundha_expert
Comment Utility
just to add, the return is a String[] and not String
0
 
LVL 1

Author Comment

by:g118481
Comment Utility
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
Comment Utility
@CEHJ - ur comment was not there wen i put my comments :)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
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
Comment Utility
String items = java.util.Arrays.toString(theArray);
0
 
LVL 1

Author Comment

by:g118481
Comment Utility
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
Comment Utility
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
Comment Utility
CEHJ,

You last suggestion works PERFECT!!!

Thank you for your time and efforts.
0
 
LVL 1

Author Closing Comment

by:g118481
Comment Utility
Great Solution!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

772 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

12 Experts available now in Live!

Get 1:1 Help Now