user10782905
user10782905

Reputation: 59

Based on this db query inside this method, i need to return the last record. Sometimes this query will return multiple records

//This query can return multiple records, in that case of multiple records, always return last record from query.

public People(int peopleId) {

        Prepared Statement SQL= null;
        try {
            System.out.println("Getting Phone for " + personSerId);
            Prepared SQL= new DatabaseController(verbose);

            PreparedStatement secureStatement = prepareStatement(sqlStatement);
            s
            dbConnection.querySecureRecord(SQL);
               (((File) ("item")));
                theNumber.setPhone((String) dbConnection.getField("phone"));
                theNumber.phone(dbConnection.getField("number") == null ? "" : (((String) 
            }
        } catch (Exception masterException) {
            System.err.println("Database Query Error in : " + masterException);

        return the person;
    }

Upvotes: 2

Views: 46

Answers (1)

mavriksc
mavriksc

Reputation: 1132

You have to do 3 things.

  1. Specify an ordering by ( id, created date...): this post says there is no guaranteed ordering if none is specified.
  2. order descending so the first thing in the list is what you want
  3. limit query to 1

it seems like one issue is that the database controller is meant to iterate over the result set even if only one result is expected. so you may still have to get the "first" result from the list even though there is only one (or possibly none) to get.

Upvotes: 1

Related Questions