Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing multiple selections to database 1

Status
Not open for further replies.

oaklandar

Technical User
Feb 12, 2004
246
US
I have a page that populates an Access 2000 database. It works great but when I try and put in more than one value for the select multiple options it only passes one value to my database. Please advise how I can get this to work where it will pass all option values that are selected?

HTML Form page:
Code:
State:
<select name="State" multiple size="4">
<option value="Arizona">Arizona</option>
<option value="Colorado">Colorado</option>
<option value="Idaho">Idaho</option>
<option value="Oregon">Oregon</option>
</select>
<br>
Comments:  <input type="text" name="Comments">

JSP action page:
Code:
//Database connection etc here...
String myQuery = "Insert into TableOne (State,Comments) VALUES (?,?)";
statement = connection.prepareStatement(myQuery);
statement.setString(1,request.getParameter("State"));
statement.setString(2,request.getParameter("Comments"));
//I also tried this and it didnt populate the database:
String myState[] = request.getParameterValues("State");
for(int i = 0;i < myState.length;i++)
{
	statement.setString(3,request.getParameter(myState[i]));
}
statement.executeUpdate();


If for example I selected three states it should populate the database like this:
Arizona, Colorado, Idaho

Please advise because none of my books show how I can do this.
 
You need to loop the array of "States", and build up a string :

Code:
String[] state = request.getParameterValues("State");
	
if (state != null) {
	String params = "";
	for(int i = 0;i < state.length;i++) {
		params += state[i] +",";
	}	
	params = params.substring(0, params.length() -1);

	
	String myQuery = "Insert into TableOne (State,Comments) VALUES ("?,?)";
	statement = connection.prepareStatement(myQuery);
	
	statement.setString(1,params);
	statement.setString(2,request.getParameter("Comments"));
	
}

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top