Reputation: 501
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.
Upvotes: 1
Views: 8245
Reputation: 303
Try this code :
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;
}
You were returning from while loop that's why you were getting just one value .
Upvotes: 1
Reputation: 3305
Your method should return data with type ArrayList
to do so remove return statement from from your code and return memberNos
like following:
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;
}
Upvotes: 1
Reputation: 706
It is because you are returning inside while loop, your code should be
while (rs.next()) {
memberNos.add(rs.getString("_Member_Number"));
}
return memberNos
Upvotes: 4