Iterating over a ResultIterable object using jdbi

Viewed 448

Yesterday I posted a question regarding retrieving data from a Db and iterating over it. Someone helpfully pointed my to JDBI and away from raw data types.

Caveats: I am a tester forst and foremost and have just started to explore JDBI for some automated tests.

So, I think I've improved the previous solution but I am just struggling with implementing the best way to iterate over my dataset now it is retrieved.

Here is my method to return the dataset:

    public List<FlightDataBean> lastFlightBookedResults(String supplierCode, String channel) {

    String sqlQuery = getData(supplierCode, channel);

    List<FlightDataBean> dataSet = jdbi.withHandle(handle ->
            handle.createQuery(sqlQuery)
                    .mapToBean(FlightDataBean.class)
                    .list());

    return dataSet;
}

Here is my Bean class:

public class FlightDataBean {

private String startDate;
private String origin;
private String destination;

public FlightDataBean(){

}

public FlightDataBean(String startDate, String origin, String destination) {
    this.startDate = startDate;
    this.origin = origin;
    this.destination = destination;

}

public String getStartDate() {
    return startDate;
}

public void setStartDate(String startDate) {
    this.startDate = startDate;
}

public String getOrigin() {
    return origin;
}

public void setOrigin(String origin) {
    this.origin = origin;
}

public String getDestination() {
    return destination;
}

public void setDestination(String destination) {
    this.destination = destination;
}

}

Here is an example of the returned dataset, 30 rows of 3 columns:

enter image description here

Clearly I can retrieve individual results by doing suchlike:

 List<FlightDataBean> resultSet;
    resultSet = getFlightData(syndicatorName);

    String startDate = (resultSet.get(0).getStartDate());
    String origin = String.valueOf((resultSet.get(1)).getOrigin());
    String destination = String.valueOf(resultSet.get(2).getDestination());

I just need a pointer as to the best/most efficient/safest way of iterating over all 30 because I am using the results as search test data and need to potentially use each one in turn in a further method which uses the dataset until it gets results back on a website.

I'm continuing to learn JDBI but in the meantime any help would be great

1 Answers

I answered your last question already with a similar answer...you can just adept the code to fit your current case:

for (FlightDataBean i : resultSet){
    String startDate = i.getStartDate();
    String origin = i.getOrigin();
    String destination = i.getDestination();
    //further code, go on from here.
}

The for-loop says nothing else, than for every Bean in your resultSet, extract the 3 values (and do the code you added afterwards).

Related