Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how do I do a resultSet for ArrayList<String> and In clause?

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how do I do a resultSet for ArrayList<String> and In clause?

    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.

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    276
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: how do I do a resultSet for ArrayList<String> and In clause?

    You don't need to use setInt and setString since you have no parameters to pass with.

    String sql1 = "select tutor_id, subject_name from tutor_subject where subject_name in ("
                    + builder.deleteCharAt(builder.length() - 1) + ")";
     
            PreparedStatement ps2 = connection.prepareStatement(sql1);
     
            ResultSet rs = ps2.executeQuery();
            while (rs.next()) {
                System.out.println("tutor ID=" + rs.getInt("tutor_id") + ", subjectName=" + rs.getString("subject_name"));
            }
    Last edited by John Joe; July 15th, 2019 at 12:15 PM.
    Whatever you are, be a good one

Similar Threads

  1. [SOLVED] Unknown column in 'where clause' in Java MySQL
    By jaydac12 in forum JDBC & Databases
    Replies: 2
    Last Post: March 8th, 2014, 10:57 PM
  2. Replies: 2
    Last Post: July 25th, 2013, 09:56 AM
  3. finally clause
    By jean28 in forum Exceptions
    Replies: 2
    Last Post: January 21st, 2013, 02:30 AM
  4. Finding String in ArrayList
    By Noob_Programmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 6th, 2011, 05:18 AM
  5. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM

Tags for this Thread