Reputation: 99
I have one jsp page where I am selecting value using multiple option and sending to servlet and trying to store into database i Have selected two values through multiple option an error is coming like this .Net Core Java, java.lang.ArrayIndexOutOfBoundsException: 2 , here is my jsp code
<tr>
<td><label>Skills </label> </td>
<td><select name="skills" size="5" multiple="multiple" tabindex="1">
<option value="Android">Android</option>
<option value=".Net">.Net</option>
<option value="Core Java">Core Java</option>
<option value="Advance Java">Advance Java</option>
<option value="Database">Database</option>
<option value="MySQL">MySQL</option>
<option value="Html/CSS">Html/CSS</option>
<option value="PHP">PHP</option>
<option value="TESTING">TESTING</option>
<option value="Networking">Networking</option>
</select>
</td>
</tr>
and after that i am retrieving value in servlet like this..
String[] skills =request.getParameterValues("skills");
for(p=0;p<skills.length;p++)
{
System.out.println(skills[p]);
}
now storing into database like this....
int i = st.executeUpdate("insert into empinfo(skills) values (+skills[p] + "')");
if (i > 0) {
}
please help me anyone here how to store and retrieve using jsp and servlet
here is my table structure.
CREATE TABLE IF NOT EXISTS `empinfo` (
`eid` varchar(255) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`sex` varchar(50) NOT NULL,
`dob` varchar(255) DEFAULT NULL,
`bloodgroup` varchar(255) DEFAULT NULL,
`fathername` varchar(300) DEFAULT NULL,
`qualification` varchar(255) DEFAULT NULL,
`mailid` varchar(400) DEFAULT NULL,
`contactnum` varchar(200) DEFAULT NULL,
`skills` varchar(1250) NOT NULL,
`temporaryadd` varchar(600) DEFAULT NULL,
`permanentadd` varchar(600) DEFAULT NULL,
`access_type` varchar(150) DEFAULT NULL,
PRIMARY KEY (`eid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Upvotes: 0
Views: 6289
Reputation: 99
String[] skills =request.getParameterValues("skills");
String value="";
for(p=0;p<skills.length;p++)
{
value += skills[p]+",";
}
int i = st.executeUpdate("insert into empinfo(skills) values (+value+ "')");
Upvotes: 1
Reputation: 8207
Try using the prepared statement instead .
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO TABLE(stringArray) VALUES (?)");
Array a = conn.createArrayOf("skills", arr);
pstmt.setArray(1, arr);
Hope this helps
Upvotes: 0