• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

How to write my list box items to a table?

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
g118481
Asked:
g118481
  • 7
  • 6
  • 2
1 Solution
 
CEHJCommented:
>>request.getParameter("GROUPS");

should be

request.getParameterValues("GROUPS");
0
 
CEHJCommented:
and

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

(which you'll need to loop through)
0
 
mukundha_expertCommented:
just to add, the return is a String[] and not String
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Thanks
0
 
mukundha_expertCommented:
@CEHJ - ur comment was not there wen i put my comments :)
0
 
CEHJCommented:
>>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
 
g118481Author Commented:
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
 
CEHJCommented:
>>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
 
g118481Author Commented:
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
 
CEHJCommented:
String items = java.util.Arrays.toString(theArray);
0
 
g118481Author Commented:
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
 
CEHJCommented:
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
 
g118481Author Commented:
CEHJ,

You last suggestion works PERFECT!!!

Thank you for your time and efforts.
0
 
g118481Author Commented:
Great Solution!
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 7
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now