CheckFlightsServlet.java。打开 CheckFlightsServlet.java。关于这个 servlet 要注意的一点是,在解析传入的参数之后,它调用 DerbyDatabase 方法 origDestFlightList()。这个方法返回一个 FlightsBean 对象数组。
清单 13. CheckFlightsServlet 类
Connection conn = DerbyDatabase.getConnInstance();
FlightsBean[] fromToFlights = \
DerbyDatabase.origDestFlightList(conn, from, to);
当 FlightsBean 数组填充了内容之后,CheckFlightsServlet 将结果放入变量名为 fromToFlights 的会话对象。
清单 14. 将 FlightsBean 数组放入会话对象
request.getSession().setAttribute("fromToFlights", fromToFlights);
用Eclipse、WTP和Derby构建Web应用程序(16)
时间:2011-02-13 IBM Susan Cline
现在打开 DerbyDatabase.java 类,看看这个方法做些什么。为便于查看,其中部分代码的格式稍微作了调整。
清单 15. 查看 DerbyDatabase 类中的 origDestFlightList 方法
public static FlightsBean[] origDestFlightList(Connection conn, \
String origAirport, String destAirport)
{
String query = "select flight_id, segment_number, orig_airport, " +
"depart_time, dest_airport, arrive_time, meal, flying_time, miles," +
"aircraft from app.flights where ORIG_AIRPORT = ? AND " +
"DEST_AIRPORT = ?";
List list = Collections.synchronizedList(new ArrayList(10));
try
{
PreparedStatement prepStmt = conn.prepareStatement(query);
prepStmt.setString(1, origAirport);
prepStmt.setString(2, destAirport);
ResultSet results = prepStmt.executeQuery();
while(results.next())
{
String flightId = results.getString(1);
String segmentNumber = results.getString(2);
String startAirport = results.getString(3);
String departTime = results.getString(4);
String endAirport = results.getString(5);
String arriveTime = results.getString(6);
String meal = results.getString(7);
String flyingTime = String.valueOf(results.getDouble(8));
String miles = String.valueOf(results.getInt(9));
String aircraft = results.getString(10);
list.add(new FlightsBean(flightId, segmentNumber, startAirport,
departTime, endAirport, arriveTime, meal, flyingTime, miles, aircraft));
}
results.close();
prepStmt.close();
}
catch (SQLException sqlExcept)
{
sqlExcept.printStackTrace();
}
return (FlightsBean[])list.toArray(new FlightsBean[list.size()]);
}
origDestFlightList 方法使用 PreparedStatement 发出一个 SQL 查询,然后将结果放入一个 FlightsBean[] 数组。下面显示了 FlightsBean 的一个构造函数。
清单 16. FlightsBean 的一个构造函数
public FlightsBean(String flight_id, String segNumber,
String origAirport, String depart_time, String destAirport,
String arrive_time, String food, String flying_time,
String mile, String jet)
{
flightId = flight_id;
segmentNumber = segNumber;
startAirport = origAirport;
departTime = depart_time;
endAirport = destAirport;
arriveTime = arr
|