Hi,
I have been attempting to get this resultSet for almost 2 days but still can't get it done.
Basically, I am trying to get the tutor_id and subject from a table which contains these 2 columns - tutor_id and subject_name.
User will enter a form and so I will get the subject thru parametervalues.
So, when I know the subjects the user wants, I will have to retrieve the corresponding subjects.
At first, I tried to use HashMap<Integer, ArrayList<String>> but after failing so many times, I decided to try something simple first as follows:
Here's the code snippet;
String sql1 = "select tutor_id, subject_name from tutor_subject where subject_name in (" + builder.deleteCharAt(builder.length() - 1) + ")"; PreparedStatement ps2 = connection.prepareStatement(sql1); for (Iterator <String> iterator = subjs.iterator(); iterator.hasNext();) { int tutor_id = tutor.getTutor_id(); ps2.setInt(1, tutor_id); // int parameterIndex = 1; String subject = (String) iterator.next(); ps2.setString(2, subject); // parameterIndex++; } ps2.executeQuery(); while (rs.next()) { System.out.println( "tutor ID=" + rs.getInt("tutor_id") + ", subjectName=" + rs.getString("subject_name")); }
I tried to learn from this tutorial : https://www.javaguides.net/2018/10/j...h-list-of.html
but I am not sure where I had gone wrong.
The latest error is :
org.postgresql.util.PSQLException: The column index is out of range: 3, number of columns: 2.
at org.postgresql.core.v3.SimpleParameterList.bind(Si mpleParameterList.java:65)
at org.postgresql.core.v3.SimpleParameterList.setStri ngParameter(SimpleParameterList.java:128)
at org.postgresql.jdbc.PgPreparedStatement.bindString (PgPreparedStatement.java:1023)
at org.postgresql.jdbc.PgPreparedStatement.setString( PgPreparedStatement.java:344)
at org.postgresql.jdbc.PgPreparedStatement.setString( PgPreparedStatement.java:328)
at Controller.searchController.doPost(searchControlle r.java:102)
Hope someone can point out the error.