0

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>&nbsp;</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;
4
  • at which line you are getting the exception ? the first part or the second where you are trying to save it to database ? or post the stacktraces
    – Santhosh
    May 19, 2014 at 7:55
  • sir i i am able to see the value on console using System.. that command so i have already printed .net and core java it is coming on console and after that i have already posted that message "java.lang.ArrayIndexOutOfBoundsException: 2 " so it must be in skills[i] when i am storing it may be going out of array index how to resolve this.... May 19, 2014 at 8:20
  • Does System.out.println(skills[p]); prints the desired values ?
    – Santhosh
    May 19, 2014 at 8:27
  • yes it is printing desired values.. May 19, 2014 at 8:29

2 Answers 2

1
  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+ "')");
0
0

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

3
  • sir that i have given here only one field , i have so many field and values are same time all are normal value... only this one i have in array form so i have to go in sequence so plz tel me in that way.. May 19, 2014 at 8:40
  • Can you post your table structure ?
    – Santhosh
    May 19, 2014 at 8:45
  • sir this link is related to my problem only but he is able to stored into database i want to save first so just check it and tell me if you can help... stackoverflow.com/questions/15233466/… May 19, 2014 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.