Search code examples
mysqlsqlstringjspc-strings

Setting up multiple parameter Values in a Sql query


Im trying to pass Multiple Parameter values from one jsp page to another jsp page. How do i pass these values in a Sql query.

First page:

  <th>Revenue</th>
  <select name="Student" multiple >
  <option value="CST">CST</option>
  <option value="EST">EST</option>
  <option value="MST">MST</option>
  <option value="MDT">MDT</option>
  <option value="PST">PST</option>`

Second Page:

String[] t2=request.getParameterValues("Student");

if(t2!=null){
    for( i=0;i<t2.length;i++){
        out.println(t2[i]);
    }
}

String QueryString1 = "Select * from School where Student in ('t2[i]')";

Solution

  • Careful with SQL injection.

    You can use your in () with this :

    String str_in = new String("")
    for( i=0;i<t2.length;i++){
        str_in += "'" + t2[i]+"',";
    }
    str_in = str_in.replaceAll(",$", "");
    
    String QueryString1 = "Select * from School where Student in ("+str_in+")"