Evan
Evan

Reputation: 1

JDBC, SELECT statement only returns last record.

Using Java and MySQL, the while loop only returns the last record that satisfies the query. The query appears to be correct based on running it in MySQL Workbench. There should be more than one record being returned.

Statement statement2 = connection.createStatement();

     String entryCrew = crewFlight.getText();
     String s2 = "select airemployee.eid, airemployee.Fname, airemployee.lname, airemployee.phone, airemployee.JobDescription, airemployee.AircraftID, airemployee.salary, flightno\n" +
    "from airemployee inner join flight on airemployee.aircraftID = flight.aircraftID where flightno = '"+entryCrew+"'";
     ResultSet rs2 = statement2.executeQuery(s2);


     while (rs2.next()){
     outputArea.setText("EID:"+rs2.getInt("EID")+"---"+"First Name:"+rs2.getString("FName")+"---"+"Last Name:"+rs2.getString("LName")+"---"+"Phone:"+rs2.getString("Phone")+"---"+"Job:"+rs2.getString("JobDescription")+"---"+"AircraftID:"+rs2.getInt("AircraftID")+"---"+"Salary:"+rs2.getInt("Salary"));
     }
     }
     catch (Exception exc){
       JOptionPane.showMessageDialog(null, exc);
   }
}                                             

Upvotes: 0

Views: 54

Answers (1)

setText does not accumulate. Every step in the while loop overwrites what is there, leaving only the final record data at the end.

Collect into a StringBuffer and set at the end.

Upvotes: 1

Related Questions