A stockbroker wants an application that will display a client’s stock portfolio (Fig. 26.37). All the companies that the user holds stock in should be displayed in a JComboBox when the application is loaded. When the user selects a company from the JComboBox and clicks the Stock Information JButton, the stock information for that company should be displayed in output JTextFields. The provided stocks database contains one table, stockInformation (Fig. 26.38), which has four columns—stockName, stockSymbol, shares and price. The values stored in the stockName and stockSymbol col- umns are Strings. The values stored in the shares column are ints. The values stored in the price column are doubles.



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

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

c) Opening the template file. Open the StockPortfolio.java file in your text editor. d) Declaring instance variables for database processing. In line 45, declare three

instance variables—myConnection, myStatement and myResultSet—of types Con-

nection, Statement and ResultSet, respectively.

e) Adding a database connection and creating a Statement object. In line 52 in the StockPortfolio constructor, insert statements that load the database driver, connect to the stocks database and create a Statement for submitting SQL to the database. Assume that the driver class name and JDBC URL are passed to the constructor


```
1 // Exercise 26.11: StockPortfolio.java
2 // Displays a client's stock portfolio.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.sql.*;
6 import java.text.*;
7 import javax.swing.*;
8
9 public class StockPortfolio extends JFrame
10 {
11 // JLabels for prompt
12 private JLabel prompt1JLabel;
13 private JLabel prompt2JLabel;
14
15 // JComboBox for stock names
16 private JComboBox stockNamesJComboBox;
17
18 // JButton for stock information
19 private JButton stockInfoJButton;
20
21 // JPanel for displaying stock information
22 private JPanel stockInfoJPanel;
23
24 // JLabel and JTextField for stock name
25 private JLabel stockNameJLabel;
26 private JTextField stockNameJTextField;
27
28 // JLabel and JTextField for stock symbol
29 private JLabel stockSymbolJLabel;
30 private JTextField stockSymbolJTextField;
31
32 // JLabel and JTextField for number of shares
33 private JLabel sharesJLabel;
34 private JTextField sharesJTextField;
35
36 // JLabel and JTextField for price of shares
37 private JLabel priceJLabel;
38 private JTextField priceJTextField;
39
40 // JLabel and JTextField for total value
41 private JLabel totalJLabel;
42 private JTextField totalJTextField;
43
44 // instance variables for database processing
45 private Connection myConnection;
46 private Statement myStatement;
47 private ResultSet myResultSet;
48
49 // constructor
50 public StockPortfolio( String databaseDriver, String databaseURL )
51 {
52 // attempt database connection
53 try
54 {
55 // load Cloudscape driver
56 Class.forName( databaseDriver );
57
58 // connect to database
59 myConnection =
60 DriverManager.getConnection( databaseURL );
61
62
62 // create Statement
63 myStatement = myConnection.createStatement();
64 }
65 catch ( SQLException exception )
66 {
67 exception.printStackTrace();
68 }
69 catch ( ClassNotFoundException exception )
70 {
71 exception.printStackTrace();
72 }
73
74 createUserInterface(); // set up GUI
75
76 } // end constructor
77
78 // create and position GUI components; register event handlers
79 private void createUserInterface()
80 {
81 // get content pane for attaching GUI components
82 Container contentPane = getContentPane();
83
84 // enable explicit positioning of GUI components
85 contentPane.setLayout( null );
86
87 // set up prompt1JLlabel
88 prompt1JLabel = new JLabel();
89 prompt1JLabel.setBounds( 8, 16, 350, 16 );
90 prompt1JLabel.setText( "Select the name of " +
91 "the stock for which you want " );
92 contentPane.add( prompt1JLabel );
93
94 // set up prompt2JLabel
95 prompt2JLabel = new JLabel();
96 prompt2JLabel.setBounds( 8, 32, 350, 16 );
97 prompt2JLabel.setText( "information, and then " +
98 "press the Stock Information button." );
99 contentPane.add( prompt2JLabel );
100
101 // set up stockNamesJComboBox
102 stockNamesJComboBox = new JComboBox();
103 stockNamesJComboBox.setBounds( 76, 65, 200, 26 );
104 stockNamesJComboBox.addItem( "" );
105 contentPane.add( stockNamesJComboBox );
106
107 // load stock names into stockNamesJComboBox
108 loadStockNames();
109
110 // set up stockInfoJButton
111 stockInfoJButton = new JButton();
112 stockInfoJButton.setBounds( 100, 100, 150, 23 );
113 stockInfoJButton.setText( "Stock Information" );
114 contentPane.add( stockInfoJButton );
115 stockInfoJButton.addActionListener(
116
117 new ActionListener() // anonymous inner class
118 {
119 // event handler called when stockInfoJButton is clicked
120 public void actionPerformed( ActionEvent event )
121 {
122 stockInfoJButtonActionPerformed( event );
123 }
124
125 } // end anonymous inner class
126
127 ); // end addActionLstener
128
129 // set up stockInfoJPanel
130 stockInfoJPanel = new JPanel();
131 stockInfoJPanel.setBounds( 18, 145, 300, 192 );
132 stockInfoJPanel.setLayout( null );
133 stockInfoJPanel.setBorder( BorderFactory.createTitledBorder(
134 BorderFactory.createEtchedBorder(), "Stock Info" ) );
135 contentPane.add( stockInfoJPanel );
136
137 // set up stockNameJLabel
138 stockNameJLabel = new JLabel();
139 stockNameJLabel.setBounds( 8, 24, 150, 23 );
140 stockNameJLabel.setText( "Stock name:" );
141 stockInfoJPanel.add( stockNameJLabel );
142
143 // set up stockNameJTextField
144 stockNameJTextField = new JTextField();
145 stockNameJTextField.setBounds( 158, 24, 125, 24 );
146 stockNameJTextField.setEditable( false );
147 stockNameJTextField.setBorder(
148 BorderFactory.createLoweredBevelBorder() );
149 stockNameJTextField.setHorizontalAlignment(
150 JTextField.CENTER );
151 stockInfoJPanel.add( stockNameJTextField );
152
153 // set up stockSymbolJLabel
154 stockSymbolJLabel = new JLabel();
155 stockSymbolJLabel.setBounds( 8, 56, 150, 23 );
156 stockSymbolJLabel.setText( "Stock symbol:" );
157 stockInfoJPanel.add( stockSymbolJLabel );
158
159 // set up stockSymbolJTextField
160 stockSymbolJTextField = new JTextField();
161 stockSymbolJTextField.setBounds( 158, 56, 125, 24 );
162 stockSymbolJTextField.setEditable( false );
163 stockSymbolJTextField.setBorder(
164 BorderFactory.createLoweredBevelBorder() );
165 stockSymbolJTextField.setHorizontalAlignment(
166 JTextField.CENTER );
167 stockInfoJPanel.add( stockSymbolJTextField );
168
169 // set up sharesJLabel
170 sharesJLabel = new JLabel();
171 sharesJLabel.setBounds( 8, 88, 150, 23 );
172 sharesJLabel.setText( "Number of shares:" );
173 stockInfoJPanel.add( sharesJLabel );
174
175 // set up sharesJTextField
176 sharesJTextField = new JTextField();
177 sharesJTextField.setBounds( 158, 88, 125, 24 );
178 sharesJTextField.setEditable( false );
179 sharesJTextField.setBorder(
180 BorderFactory.createLoweredBevelBorder() );
181 sharesJTextField.setHorizontalAlignment( JTextField.CENTER );
182 stockInfoJPanel.add( sharesJTextField );
183
184 // set up priceJLabel
185 priceJLabel = new JLabel();
186 priceJLabel.setBounds( 8, 120, 150, 23 );
187 priceJLabel.setText( "Price per share:" );
188 stockInfoJPanel.add( priceJLabel );
189
190 // set up priceJTextField
191 priceJTextField = new JTextField();
192 priceJTextField.setBounds( 158, 120, 125, 24 );
193 priceJTextField.setEditable( false );
194 priceJTextField.setBorder(
195 BorderFactory.createLoweredBevelBorder() );
196 priceJTextField.setHorizontalAlignment( JTextField.CENTER );
197 stockInfoJPanel.add( priceJTextField );
198
199 // set up totalJLabel
200 totalJLabel = new JLabel();
201 totalJLabel.setBounds( 8, 152, 150, 23 );
202 totalJLabel.setText( "Total value:" );
203 stockInfoJPanel.add( totalJLabel );
204
205 // set up totalJTextField
206 totalJTextField = new JTextField();
207 totalJTextField.setBounds( 158, 152, 125, 24 );
208 totalJTextField.setEditable( false );
209 totalJTextField.setBorder(
210 BorderFactory.createLoweredBevelBorder() );
211 totalJTextField.setHorizontalAlignment( JTextField.CENTER );
212 stockInfoJPanel.add( totalJTextField );
213
214 // ensure database connection is closed
215 // when user quits application
216 addWindowListener(
217
218 new WindowAdapter() // anonymous inner class
219 {
220 public void windowClosing( WindowEvent event )
221 {
222 frameWindowClosing( event );
223 }
224
225 } // end anonymous inner class
226
227 ); // end addWindowListener
228
229 // set properties of application’s window
230 setTitle( "Stock Portfolio" ); // set title bar string
231 setSize( 350, 380 ); // set window size
232 setVisible( true ); // display window
233
234 } // end method createUserInterface
235
236 // load stock names into stockNamesJComboBox
237 private void loadStockNames()
238 {
239 // add stock names in database to stockNamesJComboBox
240 try
241 {
242 String stockName = "";
243
244 // get stock names from database
245 myResultSet = myStatement.executeQuery(
246 "SELECT stockName FROM stockInformation" );
247
248 // add each stockName to stockNamesJComboBox
249 while( myResultSet.next() )
250 {
251 stockName = myResultSet.getString( "stockName" );
252 stockNamesJComboBox.addItem( stockName );
253 }
254
255 myResultSet.close(); // close myResultSet
256 }
257 catch ( SQLException exception )
258 {
259 exception.printStackTrace();
260 }
261
262 } // end method loadStockNames
263
264 // user clicked stockInfoJButton
265 private void stockInfoJButtonActionPerformed( ActionEvent event )
266 {
267
268
269 String stockName =
( String ) stockNamesJComboBox.getSelectedItem();
stockData( stockName );
270
271 } // end method stockInfoJButtonActionPerformed
272
273 //
274 //
275 private void stockData( String stockName )
276 {
277 // execute query to get stock information
278 try
279

Computer Science & Information Technology

You might also like to view...

The decrement operator is ____________.

a. += b. -- c. -= d. ++

Computer Science & Information Technology

________ appear on the bottom right corner of each field heading in a table, which enable filtering and sorting of data

A) Sort arrows B) Checkmarks C) Filter arrows D) Funnels

Computer Science & Information Technology

A single file containing a series of individual images that creates simple animation is known as a(n) ____.

A. embedded object B. standard JPEG C. animated GIF D. Flash animation

Computer Science & Information Technology

The arrangement of records sorted in a datasheet is ____________________ unless the datasheet is saved as a table.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology