Search code examples
javaresultset

Result Set Get all row values in a single column


I am writing a test at the moment where I need to compare some database results against a UI table using Selenium in Java. I have my UI results arriving in an ArrayList format. My console tells me that the results look like "[27002, 55691, 58716, 95362]". This is working fine.

I have another class which does a JDBC call into my database to obtain the expected results. I'm using the ResultSet method to grab these values. My code looks like the following: (Note: my JDBC connections etc are handled in the method "runQuery" which I pass my SQL query into)

public ArrayList queryForTPOSMembersByHospital() throws SQLException {

    String strQueryText = "select distinct _Member_Number and rest of query here that returns one column of results and 4 rows of data";

    ResultSet rs = runQuery(strQueryText);

    ArrayList<String> memberNos = new ArrayList<String>();

    while (rs.next()) {

        memberNos.add(rs.getString("_Member_Number"));

        return memberNos;
    }

    return null;

    }

But when I output the memberNos I only get a single value [27002] rather than 4 I am expecting.

I'm thinking I need to do something like memberNos.addAll perhaps? rather than memberNos.add? Tried a few things around this method but not getting any luck yet.


Solution

  • It is because you are returning inside while loop, your code should be

    while (rs.next()) {
    
        memberNos.add(rs.getString("_Member_Number"));
    }
    return memberNos