使用Java和MySQL,while循环仅返回满足查询条件的最后一条记录。基于在MySQL Workbench中运行查询,该查询似乎是正确的。应该返回多个记录。

语句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);
   }
}

最佳答案

setText不累积。 while循环中的每一步都会覆盖那里的内容,最后只保留最终的记录数据。

收集到StringBuffer中并在末尾设置。

09-17 18:56