An airline company wants you to develop an application that displays flight information (Fig. 26.41). The database contains two tables, one containing information about the flights and the other containing passenger information. The user should be able to choose a flight number from a JComboBox. When the View Flight Information JButton is clicked, the application should display the date of the flight, the flight’s departure and arrival cities and the names of the passengers scheduled to take the flight. The provided airline database contains two tables—reservations and flights. The reservations table (Fig. 26.42) has three columns—firstName, lastName and flightNumber. The values in the firstName and lastName column are Strings. The values in the







a) Copying the template to your working directory. Copy the C:Examples Tutorial26ExercisesAirlineReservation directory to your C:SimplyJava directory.


b) Copying the database to your working directory. Copy the airline database directory from C:ExamplesTutorial26ExercisesDatabases to your C:Simply- JavaAirlineReservation directory.


c) Opening the template file. Open the AirlineReservation.java file in your text editor.


d) Adding a database connection and creating a Statement object. In line 45 in the AirlineReservation constructor, insert statements that load the database driver, connect to the airline database and create a Statement to submit SQL to the data- base. Assume that the driver class name and JDBC URL are passed to the construc- tor from main. Note that three instance variables—myConnection, myStatemen


```
1 // AirlineReservation.java
2 // Displays flight information.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.sql.*;
6 import javax.swing.*;
7
8 public class AirlineReservation extends JFrame
9 {
10 // JLabel and JComboBox for Choose a Flight
11 private JLabel chooseFlightJLabel;
12 private JComboBox chooseFlightJComboBox;
13
14 // JButton for View Flight Information
15 private JButton flightInfoJButton;
16
17 // JPanel for displaying flight information
18 private JPanel flightInfoJPanel;
19
20 // JLabel and JTextField for Date
21 private JLabel dateJLabel;
22 private JTextField dateJTextField;
23
24 // JLabel and JTextField for Departure City
25 private JLabel departureCityJLabel;
26 private JTextField departureCityJTextField;
27
28 // JLabel and JTextField for Arrival City
29 private JLabel arrivalCityJLabel;
30 private JTextField arrivalCityJTextField;
31
32 // JPanel and JTextArea for displaying passenger list
33 private JPanel passengerListJPanel;
34 private JTextArea displayJTextArea;
35
36 // declare instance variables for database processing
37 private Connection myConnection;
38 private Statement myStatement;
39 private ResultSet myResultSet;
40
41 // constructor
42 public AirlineReservation(
43 String databaseDriver, String databaseURL )
44 {
45 // attempt database connection
46 try
47 {
48 // load Cloudscape driver
49 Class.forName( databaseDriver );
50
51 // connect to database
52 myConnection =
53 DriverManager.getConnection( databaseURL );
54
55 // create statement
56 myStatement = myConnection.createStatement();
57 }
58 catch ( SQLException exception )
59 {
60 exception.printStackTrace();
61 }
62 catch ( ClassNotFoundException exception )
63 {
64 exception.printStackTrace();
65 }
66
67 // create statement
68
69 myStatement = myConnection.createStatement();
70
71 }
72 catch ( SQLException exception )
73 {
74 // get content pane for attaching GUI components
75 Container contentPane = getContentPane();
76
77 // enable explicit positioning of GUI components
78 contentPane.setLayout( null );
79
80 // set up chooseFlightJLabel
81 chooseFlightJLabel = new JLabel();
82 chooseFlightJLabel.setBounds( 24, 16, 100, 16 );
83 chooseFlightJLabel.setText( "Choose a Flight:" );
84 contentPane.add( chooseFlightJLabel );
85
86 // set up chooseFlightJComboBox
87 chooseFlightJComboBox = new JComboBox();
88 chooseFlightJComboBox.setBounds( 130, 16, 72, 25 );
89 chooseFlightJComboBox.addItem( "" );
90 contentPane.add( chooseFlightJComboBox );
91
92 // load flight numbers into chooseFlightJComboBox
93 loadFlightNumbers();
94
95 // set up flightInfoJButton
96 flightInfoJButton = new JButton();
97 flightInfoJButton.setBounds( 230, 16, 150, 30 );
98 flightInfoJButton.setText( "View Flight Information" );
99 flightInfoJButton.setBorder(
100 BorderFactory.createRaisedBevelBorder() );
101 contentPane.add( flightInfoJButton );
102 flightInfoJButton.addActionListener(
103
104 new ActionListener() // anonymous inner class
105 {
106 // event handler called when flightInfoJButton is clicked
107 public void actionPerformed( ActionEvent event )
108 {
109 flightInfoJButtonActionPerformed( event );
110 }
111
112 } // end anonymous inner class
113
114 ); // end addActionListener
115
116 // set up flightInformationJPanel
117 createFlightInfoJPanel();
118 contentPane.add( flightInfoJPanel );
119
120 // set properties of application’s window
121 setTitle( "Airline Reservation" ); // set title bar string
122 setSize( 410, 250 ); // set window size
123 setVisible( true ); // display window
124
125 // ensure database connection is closed
126 // when user quits application
127 addWindowListener(
128
129 new WindowAdapter() // anonymous inner class
130 {
131 // event handler called when close button is clicked
132 public void windowClosing( WindowEvent event )
133 {
134 frameWindowClosing( event );
135 }
136
137 } // end anonymous inner class
138
139 ); // end addWindowListener
140
141 } // end method createUserInterface
142
143 // set up flightInformationJPanel
144 private void createFlightInfoJPanel()
145 {
146 // set up flightInfoJPanel
147 flightInfoJPanel = new JPanel();
148 flightInfoJPanel.setBounds( 15, 56, 370, 152 );
149 flightInfoJPanel.setBorder( BorderFactory.createTitledBorder(
150 BorderFactory.createEtchedBorder(),
151 "Flight Information" ) );
152 flightInfoJPanel.setLayout( null );
153
154 // set up dateJLabel
155 dateJLabel = new JLabel();
156 dateJLabel.setBounds( 16, 32, 100, 16 );
157 dateJLabel.setText( "Date:" );
158 flightInfoJPanel.add( dateJLabel );
159
160 // set up dateJTextField
161 dateJTextField = new JTextField();
162 dateJTextField.setBounds( 116, 32, 88, 20 );
163 dateJTextField.setEditable( false );
164 dateJTextField.setBorder(
165 BorderFactory.createLoweredBevelBorder() );
166 flightInfoJPanel.add( dateJTextField );
167
168 // set up depatureCityJLabel
169 departureCityJLabel = new JLabel();
170 departureCityJLabel.setBounds( 16, 64, 100, 16 );
171 departureCityJLabel.setText( "Departure City:" );
172 flightInfoJPanel.add( departureCityJLabel );
173
174 // set up departureCityJTextField
175 departureCityJTextField = new JTextField();
176 departureCityJTextField.setBounds( 116, 64, 88, 20 );
177 departureCityJTextField.setEditable( false );
178 departureCityJTextField.setBorder(
179 BorderFactory.createLoweredBevelBorder() );
180 flightInfoJPanel.add( departureCityJTextField );
181
182 // set up arrivalCityJLabel
183 arrivalCityJLabel = new JLabel();
184 arrivalCityJLabel.setBounds( 16, 96, 100, 16 );
185 arrivalCityJLabel.setText( "Arrival City:" );
186 flightInfoJPanel.add( arrivalCityJLabel );
187
188 // set up arrivalCityJTextField
189 arrivalCityJTextField = new JTextField();
190 arrivalCityJTextField.setBounds( 116, 96, 88, 20 );
191 arrivalCityJTextField.setEditable( false );
192 arrivalCityJTextField.setBorder(
193 BorderFactory.createLoweredBevelBorder() );
194 flightInfoJPanel.add( arrivalCityJTextField );
195
196 // set up passengerListJPanel
197 createPassengerListJPanel();
198 flightInfoJPanel.add( passengerListJPanel );
199
200 } // end method createFlightInfoJPanel
201
202 // set up passengerListJPanel
203 private void createPassengerListJPanel()
204 {
205 // set up passengerListJPanel
206 passengerListJPanel = new JPanel();
207 passengerListJPanel.setBounds( 220, 16, 128, 120 );
208 passengerListJPanel.setBorder(
209 BorderFactory.createTitledBorder(
210 BorderFactory.createEtchedBorder(),
211 "Passenger List" ) );
212 passengerListJPanel.setLayout( null );
213
214 // set up displayJTextArea
215 displayJTextArea = new JTextArea();
216 displayJTextArea.setBounds( 16, 24, 96, 82 );
217 displayJTextArea.setEditable( false );
218 displayJTextArea.setBorder(
219 BorderFactory.createLoweredBevelBorder() );
220 passengerListJPanel.add( displayJTextArea );
221
222 } // end method createPassengerListJPanel
223
224 // load flight numbers into chooseFlightJComboBox
225 private void loadFlightNumbers()
226 {
227 // read flight numbers from database
228 try
229 {
230 // execute query to obtain all flight numbers
231 "SELECT flightNumber FROM flights" );
232
233 // add items to combo box
234 while ( myResultSet.next() )
235 {
236 chooseFlightJComboBox.addItem(
237 myResultSet.getString( "flightNumber" ) );
238 }
239
240 myResultSet.close(); // close myResultSet
241
242 } // end try
243 catch ( SQLException exception )
244 {
245 exception.printStackTrace();
246 }
247
248
249 } // end method loadFlightNumbers
250
251 // user click View Flight Information JButton
252 private void flightInfoJButtonActionPerformed( ActionEvent event )
253 {
254 // get selected flight number
255 String flightNumber =
256 ( String ) chooseFlightJComboBox.getSelectedItem();
257
258 // display flight information
259 displayFlightInformation( flightNumber );
260
261 } // end flightInfoJButtonActionPerformed
262
263 // display flight information
264 private void displayFlightInformation( String flightNumber )
265 {
266 // display flight date, departure city and arrival city
267 // display passenger list
268 try
269 {
270 // get flight date, departure city and arrival city
271 myResultSet = myStatement.executeQuery( "SELECT flightDate,"
272 + " departureCity, arrivalCity FROM flights WHERE " +
273 "flightNumber = " + Integer.parseInt( flightNumber ) );
274
275
276
277
278
279
280
281
282
283
275 // non-empty result
276 if ( myResultSet.next() )
277 {
278 dateJTextField.setText(
279 myResultSet.getString( "flightDate" ) );
280 departureCityJTextField.setText(
281 myResultSet.getString( "departureCity" ) );
282 arrivalCityJTextField.setText(
283 myResultSet.getString( "arrivalCity" ) );
284 }
285
286 myResultSet.close(); // close myResultSet
287
288 // get passenger list
289
290
291
292
293 displayJTextArea.setText( "" ); // clear displayJTextAre

Computer Science & Information Technology

You might also like to view...

The accompanying figure illustrates the screen that appears when you type "word" in the text box. Which of the following is the title of this screen that is missing in this picture?

A. Search B. Start C. Programs D. Select

Computer Science & Information Technology

Find another slide transition in PowerPoint or Keynote or some other presentation software, and implement it as a movie.

What will be an ideal response?

Computer Science & Information Technology

The Task Usage report is a crosstab report of summarized work, displaying tasks and their assigned resources in the row area.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

You can delete all comments in a document at once.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology