-1

I know that setString permits to insert a value in specified position, now I want get checkbox values from the jsp page in order to pass it to the database.

I defined the variable of checkbox as a String array since it may handle one or more values.

This is how I defined it the variable in the class:

public String[] rep;

This is how my servlet shall retrieve this parameter in the doPost method:

String[] rep = request.getParameterValues("rep");

and this is a line from my DAO class from the preparedStatement query:

st.setString(3, exam.rep);

but that is shown this error: The method setString(int, String) in the type PreparedStatement is not applicable for the arguments (int, String[])

the whole query

public static void  add(Exam exam) throws ClassNotFoundException, SQLException {
    Connection cnx;
    cnx = Connect.getConnection();
    String req = "insert into examen values (?,?,?)";
    PreparedStatement st = cnx.prepareStatement(req);
    st.setString(1, exam.titre);
    st.setString(2, exam.question);
    st.setString(3, exam.rep);
    st.executeUpdate();
}
CodeBoyCode
  • 2,227
  • 12
  • 29
  • If you want to place all the entries of the array into the same parameter, you can convert it to a `List` and then call the list's `substring` method, which will nicely format the array for you. – Ayrton Feb 27 '19 at 13:41
  • Should all values be stored in the same column ? Could you add you query as well as your table definition with sample values ? – Arnaud Feb 27 '19 at 13:41
  • yes, all selected items should be stored in the same column –  Feb 27 '19 at 13:43
  • the tabledefinition has 3 fields, title, quiz and response –  Feb 27 '19 at 13:45
  • What the error says is pretty clear, you do not give the right type of parameter to the method. You should pass a String and not an array of string. – vincrichaud Feb 27 '19 at 13:47

1 Answers1

0

It would help if you can show us your SQL query. Prepared statements take a single value, not an array. As you define the first param as the index of the ? in your query to replace with corresponding second param. Something like this might suit your needs;

String stmt = "INSERT INTO abc (some_field) VALUES (?);";
PreparedStatement ps = conn.prepareStatement(stmt);
String formatted = String.join(",", items);
ps.setString(3, formatted);

If you want to store an array value using your prepared statement then you should format your string in some way that you can convert it back into an array when reading from your DB.

Adam
  • 1,724
  • 13
  • 16
  • check the query above, i added it! –  Feb 27 '19 at 13:51
  • The query isn't your problem. Your problem is not supplying the required params to the function. – Adam Feb 27 '19 at 13:53
  • so guide me, how shall I do to retrieve the checkbox values –  Feb 27 '19 at 13:55
  • thanks @Adam, that's working however it inserts "on" as values for example if I check 2 answers it gives "on,on" which is far from the checkboxes names I want to retreive the selected checkboxes names ! –  Feb 27 '19 at 15:21