使用Java和MySQL,while循环仅返回满足查询的最后一条记录。基于在MySQL Workbench中运行查询,查询似乎是正确的。应该返回多个记录。
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);
}
}
答案 0 :(得分:1)
setText不会累积。 while循环中的每一步都会覆盖那里的内容,最后只留下最终的记录数据。
收集到StringBuffer并设置在最后。