Download presentation
Presentation is loading. Please wait.
Published byKevin Goodwin Modified over 9 years ago
1
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 26.1 IBM Cloudscape Database 26.2 Test-Driving the ATM Application 26.3 Planning the ATM Application 26.4 Relational Database Overview: The ATM Database 26.5 SQL 26.6 Using Command-Line Arguments 26.7Creating Database Connections 26.8 Programming the Screen Saver Application 26.9 Wrap-Up Tutorial 26 – ATM Application Introducing Database Programming and Using Command-Line Arguments
2
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Install the Cloudscape database. –Connect to databases. –Create SQL queries. –Retrieve and update information in databases. –Use command-line arguments to pass options to an application as it begins executing.
3
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 26.1 IBM Cloudscape Database Cloudscape –Pure-Java embedded database management system. Installing Cloudscape –Type java –jar D:\software\Cloudscape513\cloudscape_eval_513.jar to begin the installation process
4
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 26.1 IBM Cloudscape Database (Cont.) Figure 26.1 Welcome page of Cloudscape installer. (Courtesy of IBM Corporation.)
5
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 26.1 IBM Cloudscape Database (Cont.) Figure 26.2 Options to view the release notes. (Courtesy of IBM Corporation.)
6
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 26.1 IBM Cloudscape Database (Cont.) Figure 26.3 Cloudscape 5.1 release notes. (Courtesy of IBM Corporation.)
7
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 26.1 IBM Cloudscape Database (Cont.) Figure 26.4 Accepting the Cloudscape license agreement. (Courtesy of IBM Corporation.)
8
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 26.1 IBM Cloudscape Database (Cont.) Figure 26.5 Choosing the Cloudscape installation directory. (Courtesy of IBM Corporation.)
9
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 26.1 IBM Cloudscape Database (Cont.) Figure 26.6 Choosing the default setup type. (Courtesy of IBM Corporation.)
10
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 26.1 IBM Cloudscape Database (Cont.) Figure 26.7 Confirming the location of the Cloudscape installation. (Courtesy of IBM Corporation.)
11
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 26.1 IBM Cloudscape Database (Cont.) Figure 26.8 Cloudscape installation process dialog. (Courtesy of IBM Corporation.)
12
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 26.1 IBM Cloudscape Database (Cont.) Figure 26.9 Completing the Cloudscape installation. (Courtesy of IBM Corporation.)
13
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 26.1 IBM Cloudscape Database (Cont.) Figure 26.10 Exiting the installer wizard. (Courtesy of IBM Corporation.)
14
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 26.2 Test-Driving the ATM Application
15
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 26.2 Test-Driving the ATM Application (Cont.) Setting the CLASSPATH environment variable –Set this variable so Java can interact with Cloudscape Running the ATM application –Type java ATM com.ibm.db2j.jdbc.DB2jDriver jdbc:db2j:ATM in the Command Prompt window Command-line arguments
16
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 26.2 Test-Driving the ATM Application (Cont.) Figure 26.11 Setting the CLASSPATH environment variable. Result of running setCP.bat
17
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 26.2 Test-Driving the ATM Application (Cont.) Figure 26.12 Passing command-line arguments to the ATM application.
18
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 26.2 Test-Driving the ATM Application (Cont.) Figure 26.13 ATM application. Displays instructions and messages to the user Keypad for entering a PIN or a withdrawal amount All JButton s are disabled initially JComboBox that displays account numbers
19
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 26.2 Test-Driving the ATM Application (Cont.) Figure 26.14 Selecting an account number from the JComboBox. Prompt the user to provide a PIN Keypad JButton s are enabled JComboBox is disabled Done JButton is enabled
20
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 26.2 Test-Driving the ATM Application (Cont.) Figure 26.15 Entering the PIN for the selected account. Enter JButton is enabledAn asterisk is displayed here for each keypad JButton pressed for the PIN
21
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21 26.2 Test-Driving the ATM Application (Cont.) Figure 26.16 ATM displaying welcome message. Enter JButton is disabled Balance JButton is enabled Withdraw JButton is enabled Keypad JButton s are disabled Welcome message displays in JTextArea when the user enters the correct PIN
22
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 26.2 Test-Driving the ATM Application (Cont.) Confirming the use of the database –Click the Balance JButton to check the account balance –Withdrawing money Click the Withdrawal JButton Input an amount to withdraw and click the Enter JButton –Click the Balance JButton again Notice that the balance reflects the withdrawal you performed
23
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 23 26.2 Test-Driving the ATM Application (Cont.) Figure 26.17 Viewing the account balance. Balance displays in JTextArea
24
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24 26.2 Test-Driving the ATM Application (Cont.) Figure 26.18 Withdrawing money from the account. Balance JButton is disabled Withdraw JButton is disabled Keypad JButton s are enabled Ask the user to enter a withdrawal amount
25
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 26.2 Test-Driving the ATM Application (Cont.) Figure 26.19 ATM application displaying the withdrawal amount. Keypad JButton s are disabled Balance JButton is enabled Withdraw JButton is enabledDisplay the withdrawal amount
26
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 26.2 Test-Driving the ATM Application (Cont.) Figure 26.20 Checking new balance. Display new balance
27
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27 26.2 Test-Driving the ATM Application (Cont.) Figure 26.21 ATM application ready for next customer. JComboBox is enabled JButton s are disabled Displays instructions and messages to the user Keypad JButton s are disabled
28
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 28 26.3 Planning the ATM Application When the user selects an account number from the JComboBox Disable the JComboBox Prompt the user to enter a PIN Clear the JTextField for the PIN Enable the keypad Jbuttons Enable the Done JButton When the user enters the PIN Enable the Enter Jbutton Append the number to the PIN
29
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29 26.3 Planning the ATM Application (Cont.) When the user clicks the Enter JButton to submit the PIN Search the database for the account number’s corresponding account information If the user provided a correct PIN Clear the JTextField Disable the Enter Jbutton Disable the keypad Jbuttons Enable the Balance and Withdraw Jbuttons Display the status to the user Else Clear the JTextField Prompt the user to enter a valid PIN When the user clicks the Balance Jbutton Display the balance
30
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 30 26.3 Planning the ATM Application (Cont.) When the user clicks the Withdraw Jbutton Disable the Balance and Withdraw Jbuttons Enable the keypad Jbuttons Prompt the user to enter the withdrawal amount When the user clicks the Enter JButton to submit the withdrawal amount Disable the Enter Jbutton Disable the keypad Jbuttons Process the withdrawal and display the withdrawal amount Clear the withdrawal amount in the JTextField Enable the Balance and Withdraw JButtons When the user clicks the Done Jbutton Disable the keypad Jbuttons Disable the Enter, Balance, Withdraw and Done Jbuttons Enable the JComboBox Display instructions for the next customer in the JTextArea
31
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 31 26.3 Planning the ATM Application (Cont.)
32
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 32 26.3 Planning the ATM Application (Cont.)
33
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 33 26.3 Planning the ATM Application (Cont.)
34
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 34 26.3 Planning the ATM Application (Cont.)
35
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 35 26.3 Planning the ATM Application (Cont.)
36
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 36 26.3 Planning the ATM Application (Cont.)
37
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 37 26.4 Relational Database Overview: The ATM Database Database –Organized collection of data –Database management system (DBMS) enables applications to access and store data without worrying about how the data is organized
38
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 38 26.4 Relational Database Overview: The ATM Database (Cont.) Relational database –Stores data in tables Tables store data in rows and columns –Primary key - field that contains unique values used to distinguish records from one another Structured Query Language –Perform queries –Manipulate data
39
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 39 26.4 Relational Database Overview: The ATM Database (Cont.)
40
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 40 26.5 SQL Basic SELECT Query –Selects rows and columns from a table –Performed by SELECT Basic form: SELECT * FROM TableName –* indicates all columns from TableName will be selected –* can be replaced by comma-separated list of columns to retrieve e.g. SELECT accountNumber FROM accountInformation –FROM indicates the table from which to retrieve data
41
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 41 26.5 SQL (Cont.) SELECT accountNumber, firstName FROM accountInformation
42
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 42 26.5 SQL (Cont.) WHERE Clause –Specify the selection criteria for a query Only rows that satisfy the criteria will be selected –Combined with a SELECT statement –General form: SELECT columnName1, columnName1, … FROM TableName WHERE criteria –Example SELECT pin, firstName, balanceAmount FROM accountInformation WHERE accountNumber = ’12548693’
43
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 43 26.5 SQL (Cont.)
44
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 44 26.5 SQL (Cont.) UPDATE Statement –Modify data in a table –Basic form: UPDATE TableName SET columnName1 = value1, columnName2 = value2 … WHERE criteria TableName specifies table to update SET keyword is followed by list of column name/value pairs WHERE clause determines which rows to update –Example UPDATE accountInformation SET balanceAmount = 1000 WHERE accountNumber = ‘12548693
45
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 45 26.5 SQL (Cont.)
46
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 46 26.6 Using Command-Line Arguments main method –Command line arguments Passed to main as String array args Length of args Standard output object –System.out –Display text in the Command Prompt window
47
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 47 26.6 Using Command-Line Arguments (Cont.) Figure 26.27 Viewing the main method. Display a line of text that indicates the syntax to run the application Get command-line arguments Pass command-line arguments to the constructor Check the number of command-line arguments Start the main method declaration
48
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 48 26.7 Creating Database Connections JDBC API –Communicate and manipulate databases JDBC Driver –Provided by a DMNS vendor –Enable Java applications to access a particular database
49
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 49 26.7 Creating Database Connections (Cont.) Importing the java.sql package Figure 26.28 Importing the java.sql package. java.sql package –Perform database processing
50
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 50 26.7 Creating Database Connections (Cont.) Connection object –Manages connection between Java application and database –Allows applications to create SQL statements Statement object –Enables applications to execute SQL statements ResultSet object –Returned by executing a query –Contain rows and columns selected –Rows of table returned in sequence –One ResultSet per Statement
51
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 51 26.7 Creating Database Connections (Cont.) Figure 26.29 Declaring instance variables for database processing. Declaring instance variables for managing the database connection
52
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 52 26.7 Creating Database Connections (Cont.) Connecting to a database –Load database driver forName method of class Class DriverManager class –Manages JDBC drivers and establishes connections to databases –getConnection method connects to database –JDBC URL Form: protocol:subprotocol:subname Specifies protocol and subprotocol for communication and name of database Protocols and subprotocols define how data is transferred between a Java application and a database
53
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 53 26.7 Creating Database Connections (Cont.) Figure 26.30 Adding a database connection. Load database driver class ( com.ibm.db2j.jdbc.JB2j Driver ) Connect to ATM database ( jdbc:db2j:ATM ) Create a Statement objectCatch any SQLException s thrown from lines 86-87 or line 90 Catch ClassNotFoundException thrown from line 83
54
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 54 26.8 Programming the ATM Application executeQuery method of Statement –Takes a String representing a SQL query –Returns a ResultSet ResultSet –ResultSet cursor Points to a row in a ResultSet Needs to be positioned to the first row before processing data –next method Returns true if cursor can be positioned in next row Returns false if no more rows –Extract data from ResultSet as a specific Java type Methods getString, getInt and getDouble –Return the data as a String, int or double, respectively. close method closes the ResultSet and releases its resources
55
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 55 26.8 Programming the ATM Application (Cont.) Figure 26.31 Filling the accountNumberJComboBox with account numbers. Submit a query that selects the account numbers from table accountInformation Process the ResultSet and fill the accountNumberJComboBox with account numbers Close myResultSet to release database resources Catch any SQLExceptions thrown from the try block
56
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 56 26.8 Programming the ATM Application (Cont.) Figure 26.32 Retrieving account information from the database. Submit a query that selects the pin, firstName and balanceAmount values for the specified account number Get the pin, firstName and balanceAmount values from the ResultSet Close myResultSet to release database resources Catch any SQLException s thrown from the try block
57
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 57 26.8 Programming the ATM Application (Cont.) executeUpdate method of Statement –Submits a SQL statement that updates a database –Takes a String indicating SQL to execute –Returns an int specifying how many rows were updated
58
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 58 26.8 Programming the ATM Application (Cont.) Figure 26.33 Updating the balanceAmount column. Submit a SQL statement that updates the balanceAmount in table accountInformation for the row with the specified accountNumber Catch any SQLException s thrown from the try block
59
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 59 26.8 Programming the ATM Application (Cont.) Closing database connections –close method of Statement Closes Statement object –Releases resources –Prevents further SQL from being executed –close method of Connection Closes Connection object –Terminates connection between application and database
60
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 60 26.8 Programming the ATM Application (Cont.) Figure 26.34 Closing the database connection. Close Statement and Connection to release database resources Terminate the applicationCatch any SQLException s thrown from the try block
61
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 61 26.8 Programming the ATM Application (Cont.) Figure 26.35 Running the completed ATM application.
62
2004 Prentice Hall, Inc. All rights reserved. Outline 62 ATM.java (1 of 37) 1 // Tutorial 26: ATM.java 2 // ATM application allows users to access an account, 3 // view the balance and withdraw money from the account. 4 import java.awt.*; 5 import java.awt.event.*; 6 import java.sql.*; 7 import java.text.*; 8 import javax.swing.*; 9 import javax.swing.event.*; 10 11 public class ATM extends JFrame 12 { 13 // JTextArea to display message 14 private JTextArea messageJTextArea; 15 16 // JTextField to enter PIN or withdrawal amount 17 private JTextField numberJTextField; 18 19 // JPanel for number JButtons 20 private JPanel buttonsJPanel; 21 22 // JButtons for input of PIN or withdrawal amount 23 private JButton oneJButton; 24 private JButton twoJButton; 25 private JButton threeJButton; Importing java.sql package
63
2004 Prentice Hall, Inc. All rights reserved. Outline 63 ATM.java (2 of 37) 26 private JButton fourJButton; 27 private JButton fiveJButton; 28 private JButton sixJButton; 29 private JButton sevenJButton; 30 private JButton eightJButton; 31 private JButton nineJButton; 32 private JButton zeroJButton; 33 34 // JButton to submit PIN or withdrawal amount 35 private JButton enterJButton; 36 37 // JButton to view balance 38 private JButton balanceJButton; 39 40 // JButton to withdraw from account 41 private JButton withdrawJButton; 42 43 // JButton to close the transaction 44 private JButton doneJButton; 45 46 // JPanel to get account numbers 47 private JPanel accountNumberJPanel; 48
64
2004 Prentice Hall, Inc. All rights reserved. Outline 64 ATM.java (3 of 37) 49 // JLabel and JComboBox for account numbers 50 private JLabel accountNumberJLabel; 51 private JComboBox accountNumberJComboBox; 52 53 // constants for user action 54 private final static int ENTER_PIN = 1; 55 private final static int WITHDRAWAL = 2; 56 57 // instance variables used to store PIN and 58 // firstName from database 59 private String pin, firstName; 60 61 // instance variable used to distinguish user action 62 private int action; 63 64 // instance variables used to store user selected account number 65 // and PIN 66 private String userAccountNumber, userPIN; 67 68 // instance variable used to store account balance 69 private double balance; 70
65
2004 Prentice Hall, Inc. All rights reserved. Outline 65 ATM.java (4 of 37) 71 // instance variables used to manipulate database 72 private Connection myConnection; 73 private Statement myStatement; 74 private ResultSet myResultSet; 75 76 // constructor 77 public ATM( String databaseDriver, String databaseURL ) 78 { 79 // establish connection to database 80 try 81 { 82 // load Cloudscape driver 83 Class.forName( databaseDriver ); 84 85 // connect to database 86 myConnection = 87 DriverManager.getConnection( databaseURL ); 88 89 // create Statement for executing SQL 90 myStatement = myConnection.createStatement(); 91 } 92 catch ( SQLException exception ) 93 { 94 exception.printStackTrace(); 95 } Declaring instance variables to manage the database connection Loading database driver class ( com. ibm.db2j.jdbc. DB2jDriver ) Creating a Statement object Connecting to ATM database ( jdbc:db2j:ATM )
66
2004 Prentice Hall, Inc. All rights reserved. Outline 66 ATM.java (5 of 37) 96 catch ( ClassNotFoundException exception ) 97 { 98 exception.printStackTrace(); 99 } 100 101 createUserInterface(); // set up GUI 102 103 } // end constructor 104 105 // create and position GUI components; register event handler 106 private void createUserInterface() 107 { 108 // get content pane for attaching GUI components 109 Container contentPane = getContentPane(); 110 111 // enable explicit positioning of GUI components 112 contentPane.setLayout( null ); 113 114 // set up messageJTextArea 115 messageJTextArea = new JTextArea(); 116 messageJTextArea.setBounds( 40, 16, 288, 88 ); 117 messageJTextArea.setText( 118 "Please select your account number." ); 119 messageJTextArea.setBorder( 120 BorderFactory.createLoweredBevelBorder() ); Catching ClassNotFound- Exception from line 83
67
2004 Prentice Hall, Inc. All rights reserved. Outline 67 ATM.java (6 of 37) 121 messageJTextArea.setEditable( false ); 122 contentPane.add( messageJTextArea ); 123 124 // set up numberJTextField 125 numberJTextField = new JTextField(); 126 numberJTextField.setBounds( 110, 120, 128, 21 ); 127 numberJTextField.setBorder( 128 BorderFactory.createLoweredBevelBorder() ); 129 numberJTextField.setEditable( false ); 130 contentPane.add( numberJTextField ); 131 132 // set up buttonsJPanel 133 buttonsJPanel = new JPanel(); 134 buttonsJPanel.setBounds( 44, 160, 276, 150 ); 135 buttonsJPanel.setBorder( BorderFactory.createEtchedBorder() ); 136 buttonsJPanel.setLayout( null ); 137 contentPane.add( buttonsJPanel ); 138 139 // set up oneJButton 140 oneJButton = new JButton(); 141 oneJButton.setBounds( 53, 28, 24, 24 ); 142 oneJButton.setText( "1" ); 143 oneJButton.setBorder( 144 BorderFactory.createRaisedBevelBorder() ); 145 buttonsJPanel.add( oneJButton );
68
2004 Prentice Hall, Inc. All rights reserved. Outline 68 ATM.java (7 of 37) 146 oneJButton.addActionListener( 147 148 new ActionListener() // anonymous inner class 149 { 150 // event handler called when oneJButton is clicked 151 public void actionPerformed( ActionEvent event ) 152 { 153 oneJButtonActionPerformed( event ); 154 } 155 156 } // end anonymous inner class 157 158 ); // end call to addActionListener 159 160 // set up twoJButton 161 twoJButton = new JButton(); 162 twoJButton.setBounds( 77, 28, 24, 24 ); 163 twoJButton.setText( "2" ); 164 twoJButton.setBorder( 165 BorderFactory.createRaisedBevelBorder() ); 166 buttonsJPanel.add( twoJButton ); 167 twoJButton.addActionListener( 168 169 new ActionListener() // anonymous inner class 170 {
69
2004 Prentice Hall, Inc. All rights reserved. Outline 69 ATM.java (8 of 37) 171 // event handler called when twoJButton is clicked 172 public void actionPerformed( ActionEvent event ) 173 { 174 twoJButtonActionPerformed( event ); 175 } 176 177 } // end anonymous inner class 178 179 ); // end call to addActionListener 180 181 // set up threeJButton 182 threeJButton = new JButton(); 183 threeJButton.setBounds( 101, 28, 24, 24 ); 184 threeJButton.setText( "3" ); 185 threeJButton.setBorder( 186 BorderFactory.createRaisedBevelBorder() ); 187 buttonsJPanel.add( threeJButton ); 188 threeJButton.addActionListener( 189 190 new ActionListener() // anonymous inner class 191 { 192 // event handler called when threeJButton is clicked 193 public void actionPerformed( ActionEvent event ) 194 { 195 threeJButtonActionPerformed( event );
70
2004 Prentice Hall, Inc. All rights reserved. Outline 70 ATM.java (9 of 37) 196 } 197 198 } // end anonymous inner class 199 200 ); // end call to addActionListener 201 202 // set up fourJButton 203 fourJButton = new JButton(); 204 fourJButton.setBounds( 53, 52, 24, 24 ); 205 fourJButton.setText( "4" ); 206 fourJButton.setBorder( 207 BorderFactory.createRaisedBevelBorder() ); 208 buttonsJPanel.add( fourJButton ); 209 fourJButton.addActionListener( 210 211 new ActionListener() // anonymous inner class 212 { 213 // event handler called when fourJButton is clicked 214 public void actionPerformed( ActionEvent event ) 215 { 216 fourJButtonActionPerformed( event ); 217 } 218 219 } // end anonymous inner class 220
71
2004 Prentice Hall, Inc. All rights reserved. Outline 71 ATM.java (10 of 37) 221 ); // end call to addActionListener 222 223 // set up fiveJButton 224 fiveJButton = new JButton(); 225 fiveJButton.setBounds( 77, 52, 24, 24 ); 226 fiveJButton.setText( "5" ); 227 fiveJButton.setBorder( 228 BorderFactory.createRaisedBevelBorder() ); 229 buttonsJPanel.add( fiveJButton ); 230 fiveJButton.addActionListener( 231 232 new ActionListener() // anonymous inner class 233 { 234 // event handler called when fiveJButton is clicked 235 public void actionPerformed( ActionEvent event ) 236 { 237 fiveJButtonActionPerformed( event ); 238 } 239 240 } // end anonymous inner class 241 242 ); // end call to addActionListener 243 244 // set up sixJButton 245 sixJButton = new JButton();
72
2004 Prentice Hall, Inc. All rights reserved. Outline 72 ATM.java (11 of 37) 246 sixJButton.setBounds( 101, 52, 24, 24 ); 247 sixJButton.setText( "6" ); 248 sixJButton.setBorder( 249 BorderFactory.createRaisedBevelBorder() ); 250 buttonsJPanel.add( sixJButton ); 251 sixJButton.addActionListener( 252 253 new ActionListener() // anonymous inner class 254 { 255 // event handler called when sixJButton is clicked 256 public void actionPerformed( ActionEvent event ) 257 { 258 sixJButtonActionPerformed( event ); 259 } 260 261 } // end anonymous inner class 262 263 ); // end call to addActionListener 264 265 // set up sevenJButton 266 sevenJButton = new JButton(); 267 sevenJButton.setBounds( 53, 76, 24, 24 ); 268 sevenJButton.setText( "7" ); 269 sevenJButton.setBorder( 270 BorderFactory.createRaisedBevelBorder() );
73
2004 Prentice Hall, Inc. All rights reserved. Outline 73 ATM.java (12 of 37) 271 buttonsJPanel.add( sevenJButton ); 272 sevenJButton.addActionListener( 273 274 new ActionListener() // anonymous inner class 275 { 276 // event handler called when sevenJButton is clicked 277 public void actionPerformed( ActionEvent event ) 278 { 279 sevenJButtonActionPerformed( event ); 280 } 281 282 } // end anonymous inner class 283 284 ); // end call to addActionListener 285 286 // set up eightJButton 287 eightJButton = new JButton(); 288 eightJButton.setBounds( 77, 76, 24, 24 ); 289 eightJButton.setText( "8" ); 290 eightJButton.setBorder( 291 BorderFactory.createRaisedBevelBorder() ); 292 buttonsJPanel.add( eightJButton ); 293 eightJButton.addActionListener( 294 295 new ActionListener() // anonymous inner class
74
2004 Prentice Hall, Inc. All rights reserved. Outline 74 ATM.java (13 of 37) 296 { 297 // event handler called when eightJButton is clicked 298 public void actionPerformed( ActionEvent event ) 299 { 300 eightJButtonActionPerformed( event ); 301 } 302 303 } // end anonymous inner class 304 305 ); // end call to addActionListener 306 307 // set up nineJButton 308 nineJButton = new JButton(); 309 nineJButton.setBounds( 101, 76, 24, 24 ); 310 nineJButton.setText( "9" ); 311 nineJButton.setBorder( 312 BorderFactory.createRaisedBevelBorder() ); 313 buttonsJPanel.add( nineJButton ); 314 nineJButton.addActionListener( 315 316 new ActionListener() // anonymous inner class 317 { 318 // event handler called when nineJButton is clicked 319 public void actionPerformed( ActionEvent event ) 320 {
75
2004 Prentice Hall, Inc. All rights reserved. Outline 75 ATM.java (14 of 37) 321 nineJButtonActionPerformed( event ); 322 } 323 324 } // end anonymous inner class 325 326 ); // end call to addActionListener 327 328 // set up zeroJButton 329 zeroJButton = new JButton(); 330 zeroJButton.setBounds( 77, 100, 24, 24 ); 331 zeroJButton.setText( "0" ); 332 zeroJButton.setBorder( 333 BorderFactory.createRaisedBevelBorder() ); 334 buttonsJPanel.add( zeroJButton ); 335 zeroJButton.addActionListener( 336 337 new ActionListener() // anonymous inner class 338 { 339 // event handler called when zeroJButton is clicked 340 public void actionPerformed( ActionEvent event ) 341 { 342 zeroJButtonActionPerformed( event ); 343 } 344 345 } // end anonymous inner class
76
2004 Prentice Hall, Inc. All rights reserved. Outline 76 ATM.java (15 of 37) 346 347 ); // end call to addActionListener 348 349 disableKeyPad(); // disable numeric JButtons 350 351 // set up enterJButton 352 enterJButton = new JButton(); 353 enterJButton.setBounds( 149, 17, 72, 24 ); 354 enterJButton.setText( "Enter" ); 355 enterJButton.setBorder( 356 BorderFactory.createRaisedBevelBorder() ); 357 buttonsJPanel.add( enterJButton ); 358 enterJButton.setEnabled( false ); 359 enterJButton.addActionListener( 360 361 new ActionListener() // anonymous inner class 362 { 363 // event handler called when enterJButton is clicked 364 public void actionPerformed( ActionEvent event ) 365 { 366 enterJButtonActionPerformed( event ); 367 } 368 369 } // end anonymous inner class 370
77
2004 Prentice Hall, Inc. All rights reserved. Outline 77 ATM.java (16 of 37) 371 ); // end call to addActionListener 372 373 // set up balanceJButton 374 balanceJButton = new JButton(); 375 balanceJButton.setBounds( 149, 49, 72, 24 ); 376 balanceJButton.setText( "Balance" ); 377 balanceJButton.setBorder( 378 BorderFactory.createRaisedBevelBorder() ); 379 buttonsJPanel.add( balanceJButton ); 380 balanceJButton.setEnabled( false ); 381 balanceJButton.addActionListener( 382 383 new ActionListener() // anonymous inner class 384 { 385 // event handler called when balanceJButton is clicked 386 public void actionPerformed( ActionEvent event ) 387 { 388 balanceJButtonActionPerformed( event ); 389 } 390 391 } // end anonymous inner class 392 393 ); // end call to addActionListener 394 395 // set up withdrawJButton
78
2004 Prentice Hall, Inc. All rights reserved. Outline 78 ATM.java (17 of 37) 396 withdrawJButton = new JButton(); 397 withdrawJButton.setBounds( 149, 81, 72, 24 ); 398 withdrawJButton.setText( "Withdraw" ); 399 withdrawJButton.setBorder( 400 BorderFactory.createRaisedBevelBorder() ); 401 withdrawJButton.setEnabled( false ); 402 buttonsJPanel.add( withdrawJButton ); 403 withdrawJButton.addActionListener( 404 405 new ActionListener() // anonymous inner class 406 { 407 // event handler called when withdrawJButton is clicked 408 public void actionPerformed( ActionEvent event ) 409 { 410 withdrawJButtonActionPerformed( event ); 411 } 412 413 } // end anonymous inner class 414 415 ); // end call to addActionListener 416 417 // set up doneJButton 418 doneJButton = new JButton(); 419 doneJButton.setBounds( 149, 113, 72, 24 ); 420 doneJButton.setText( "Done" );
79
2004 Prentice Hall, Inc. All rights reserved. Outline 79 ATM.java (18 of 37) 421 doneJButton.setBorder( 422 BorderFactory.createRaisedBevelBorder() ); 423 doneJButton.setEnabled( false ); 424 buttonsJPanel.add( doneJButton ); 425 doneJButton.addActionListener( 426 427 new ActionListener() // anonymous inner class 428 { 429 // event handler called when doneJButton is clicked 430 public void actionPerformed( ActionEvent event ) 431 { 432 doneJButtonActionPerformed( event ); 433 } 434 435 } // end anonymous inner class 436 437 ); // end call to addActionListener 438 439 // set up accountNumberJPanel 440 accountNumberJPanel = new JPanel(); 441 accountNumberJPanel.setBounds( 44, 320, 276, 48 ); 442 accountNumberJPanel.setBorder( 443 BorderFactory.createEtchedBorder() ); 444 accountNumberJPanel.setLayout( null ); 445 contentPane.add( accountNumberJPanel );
80
2004 Prentice Hall, Inc. All rights reserved. Outline 80 ATM.java (19 of 37) 446 447 // set up accountNumberJLabel 448 accountNumberJLabel = new JLabel(); 449 accountNumberJLabel.setBounds( 25, 15, 100, 20 ); 450 accountNumberJLabel.setText( "Account Number:" ); 451 accountNumberJPanel.add( accountNumberJLabel ); 452 453 // set up accountNumberJComboBox 454 accountNumberJComboBox = new JComboBox(); 455 accountNumberJComboBox.setBounds( 150, 12, 96, 25 ); 456 accountNumberJComboBox.addItem( "" ); 457 accountNumberJComboBox.setSelectedIndex( 0 ); 458 accountNumberJPanel.add( accountNumberJComboBox ); 459 accountNumberJComboBox.addItemListener( 460 461 new ItemListener() // anonymous inner class 462 { 463 // event handler called when account number is chosen 464 public void itemStateChanged( ItemEvent event ) 465 { 466 accountNumberJComboBoxItemStateChanged( event ); 467 } 468 469 } // end anonymous inner class 470
81
2004 Prentice Hall, Inc. All rights reserved. Outline 81 ATM.java (20 of 37) 471 ); // end call to addItemListener 472 473 // read account numbers from database and 474 // place them in accountNumberJComboBox 475 loadAccountNumbers(); 476 477 // set properties of application's window 478 setTitle( "ATM" ); // set title bar string 479 setSize( 375, 410 ); // set window size 480 setVisible( true ); // display window 481 482 // ensure database connection is closed 483 // when user closes application window 484 addWindowListener( 485 486 new WindowAdapter() // anonymous inner class 487 { 488 public void windowClosing( WindowEvent event ) 489 { 490 frameWindowClosing( event ); 491 } 492 493 } // end anonymous inner class 494 495 ); // end addWindowListener
82
2004 Prentice Hall, Inc. All rights reserved. Outline 82 ATM.java (21 of 37) 496 497 } // end method createUserInterface 498 499 // process oneJButton click 500 private void oneJButtonActionPerformed( ActionEvent event ) 501 { 502 zeroToNineJButtonActionPerformed( "1" ); 503 504 } // end method oneJButtonActionPerformed 505 506 // process twoJButton click 507 private void twoJButtonActionPerformed( ActionEvent event ) 508 { 509 zeroToNineJButtonActionPerformed( "2" ); 510 511 } // end method twoJButtonActionPerformed 512 513 // process threeJButton click 514 private void threeJButtonActionPerformed( ActionEvent event ) 515 { 516 zeroToNineJButtonActionPerformed( "3" ); 517 518 } // end method threeJButtonActionPerformed 519 520 // process fourJButton click
83
2004 Prentice Hall, Inc. All rights reserved. Outline 83 ATM.java (22 of 37) 521 private void fourJButtonActionPerformed( ActionEvent event ) 522 { 523 zeroToNineJButtonActionPerformed( "4" ); 524 525 } // end method fourJButtonActionPerformed 526 527 // process fiveJButton click 528 private void fiveJButtonActionPerformed( ActionEvent event ) 529 { 530 zeroToNineJButtonActionPerformed( "5" ); 531 532 } // end method fiveJButtonActionPerformed 533 534 // process sixJButton click 535 private void sixJButtonActionPerformed( ActionEvent event ) 536 { 537 zeroToNineJButtonActionPerformed( "6" ); 538 539 } // end method sixJButtonActionPerformed 540 541 // process sevenJButton click 542 private void sevenJButtonActionPerformed( ActionEvent event ) 543 { 544 zeroToNineJButtonActionPerformed( "7" ); 545
84
2004 Prentice Hall, Inc. All rights reserved. Outline 84 ATM.java (23 of 37) 546 } // end method sevenJButtonActionPerformed 547 548 // process eightJButton click 549 private void eightJButtonActionPerformed( ActionEvent event ) 550 { 551 zeroToNineJButtonActionPerformed( "8" ); 552 553 } // end method eightJButtonActionPerformed 554 555 // process nineJButton click 556 private void nineJButtonActionPerformed( ActionEvent event ) 557 { 558 zeroToNineJButtonActionPerformed( "9" ); 559 560 } // end method nineJButtonActionPerformed 561 562 // process zeroJButton click 563 private void zeroJButtonActionPerformed( ActionEvent event ) 564 { 565 zeroToNineJButtonActionPerformed( "0" ); 566 567 } // end method zeroJButtonActionPerformed 568 569 // process clicks of a numeric JButton 570 private void zeroToNineJButtonActionPerformed( String number )
85
2004 Prentice Hall, Inc. All rights reserved. Outline 85 ATM.java (24 of 37) 571 { 572 // enable enterJButton if it is disabled 573 if ( !enterJButton.isEnabled() ) 574 { 575 enterJButton.setEnabled( true ); 576 } 577 578 // if user is entering PIN number display * to conceal PIN 579 if ( action == ENTER_PIN ) 580 { 581 userPIN += number; // append number to current PIN 582 numberJTextField.setText( 583 numberJTextField.getText() + "*" ); 584 } 585 586 else // otherwise display number of JButton user clicked 587 { 588 numberJTextField.setText( 589 numberJTextField.getText() + number ); 590 } 591 592 } // end method zeroToNineJButtonsActionPerformed 593 594 // verify PIN or withdraw from account 595 private void enterJButtonActionPerformed( ActionEvent event )
86
2004 Prentice Hall, Inc. All rights reserved. Outline 86 ATM.java (25 of 37) 596 { 597 if ( action == ENTER_PIN ) // checking PIN 598 { 599 // get pin, first name and balance for account number 600 // selected in accountNumberJComboBox 601 retrieveAccountInformation(); 602 603 numberJTextField.setText( "" ); // clear numberJTextField 604 605 // correct PIN number 606 if ( userPIN.equals( pin ) ) 607 { 608 // disable enterJButton 609 enterJButton.setEnabled( false ); 610 611 disableKeyPad(); // disable numeric JButtons 612 613 // enable balanceJButton and withdrawJButton 614 balanceJButton.setEnabled( true ); 615 withdrawJButton.setEnabled( true ); 616 617 // display status to user 618 messageJTextArea.setText( 619 "Welcome " + firstName + ", select a transaction." ); 620
87
2004 Prentice Hall, Inc. All rights reserved. Outline 87 ATM.java (26 of 37) 621 } // end if part of if...else 622 623 else // wrong PIN number 624 { 625 // indicate that incorrect PIN was provided 626 messageJTextArea.setText( 627 "Sorry, PIN number is incorrect." + 628 "\nPlease re-enter the PIN number." ); 629 630 userPIN = ""; // clear user's previous PIN entry 631 632 } // end else part of if...else 633 634 } // end if that processes PIN 635 636 else if ( action == WITHDRAWAL ) // process withdrawal 637 { 638 enterJButton.setEnabled( false ); // disable enterJButton 639 640 disableKeyPad(); // disable numeric JButtons 641 642 // process withdrawal 643 withdraw( 644 Double.parseDouble( numberJTextField.getText() ) ); 645
88
2004 Prentice Hall, Inc. All rights reserved. Outline 88 ATM.java (27 of 37) 646 numberJTextField.setText( "" ); // clear numberJTextField 647 648 // enable balanceJButton and withdrawJButton 649 balanceJButton.setEnabled( true ); 650 withdrawJButton.setEnabled( true ); 651 652 } // end if that processes withdrawal 653 654 } // end method enterJButtonActionPerformed 655 656 // display account balance 657 private void balanceJButtonActionPerformed( ActionEvent event ) 658 { 659 // define display format 660 DecimalFormat dollars = new DecimalFormat( "0.00" ); 661 662 // display user's balance 663 messageJTextArea.setText( "Your current balance is $" + 664 dollars.format( balance ) + "." ); 665 666 } // end method balanceJButtonActionPerformed 667 668 // display withdraw action 669 private void withdrawJButtonActionPerformed( ActionEvent event ) 670 {
89
2004 Prentice Hall, Inc. All rights reserved. Outline 89 ATM.java (28 of 37) 671 // disable Balance and Withdraw JButtons 672 balanceJButton.setEnabled( false ); 673 withdrawJButton.setEnabled( false ); 674 675 enableKeyPad(); // enable numeric JButtons 676 677 // display message to user 678 messageJTextArea.setText( 679 "Enter the amount you would like to withdraw" ); 680 681 // change action to indicate user will provide 682 // withdrawal amount 683 action = WITHDRAWAL; 684 685 } // end method withdrawJButtonActionPerformed 686 687 // reset GUI 688 private void doneJButtonActionPerformed( ActionEvent event ) 689 { 690 userPIN = ""; // clear userPIN 691 692 disableKeyPad(); // disable numeric JButtons 693 694 // disable OK, Balance, Withdraw and Done JButtons 695 enterJButton.setEnabled( false );
90
2004 Prentice Hall, Inc. All rights reserved. Outline 90 ATM.java (29 of 37) 696 balanceJButton.setEnabled( false ); 697 withdrawJButton.setEnabled( false ); 698 doneJButton.setEnabled( false ); 699 700 // enable and reset accountNumberJComboBox 701 accountNumberJComboBox.setEnabled( true ); 702 accountNumberJComboBox.setSelectedIndex( 0 ); 703 704 // reset messageJTextArea 705 messageJTextArea.setText( 706 "Please select your account number." ); 707 708 } // end method doneJButtonActionPerformed 709 710 // get account number and enable OK and Done JButtons 711 private void accountNumberJComboBoxItemStateChanged( 712 ItemEvent event ) 713 { 714 // get user selected account number if no transaction is 715 // in process 716 if ( ( event.getStateChange() == ItemEvent.SELECTED ) && 717 ( accountNumberJComboBox.getSelectedIndex() != 0 ) ) 718 { 719 // disable accountNumberJComboBox 720 accountNumberJComboBox.setEnabled( false );
91
2004 Prentice Hall, Inc. All rights reserved. Outline 91 ATM.java (30 of 37) 721 722 // get selected account number 723 userAccountNumber = 724 ( String ) accountNumberJComboBox.getSelectedItem(); 725 726 // change action to indicate that user will provide 727 // PIN number 728 action = ENTER_PIN; 729 userPIN = ""; 730 731 // prompt user to enter PIN number 732 messageJTextArea.setText( 733 "Please enter your PIN number." ); 734 735 numberJTextField.setText( "" ); // clear numberJTextField 736 enableKeyPad(); // enable numeric JButtons 737 doneJButton.setEnabled( true ); // enable doneJButton 738 739 } // end if 740 741 } // end method accountNumberJComboBoxItemStateChanged 742 743 // enable numeric JButtons 744 private void enableKeyPad() 745 {
92
2004 Prentice Hall, Inc. All rights reserved. Outline 92 ATM.java (31 of 37) 746 oneJButton.setEnabled( true ); // enable oneJButton 747 twoJButton.setEnabled( true ); // enable twoJButton 748 threeJButton.setEnabled( true ); // enable threeJButton 749 fourJButton.setEnabled( true ); // enable fourJButton 750 fiveJButton.setEnabled( true ); // enable fiveJButton 751 sixJButton.setEnabled( true ); // enable sixJButton 752 sevenJButton.setEnabled( true ); // enable sevenJButton 753 eightJButton.setEnabled( true ); // enable eightJButton 754 nineJButton.setEnabled( true ); // enable nineJButton 755 zeroJButton.setEnabled( true ); // enable zeroJButton 756 757 } // end method enableKeyPad 758 759 // disable numeric JButtons 760 private void disableKeyPad() 761 { 762 oneJButton.setEnabled( false ); // disable oneJButton 763 twoJButton.setEnabled( false ); // disable twoJButton 764 threeJButton.setEnabled( false ); // disable threeJButton 765 fourJButton.setEnabled( false ); // disable fourJButton 766 fiveJButton.setEnabled( false ); // disable fiveJButton 767 sixJButton.setEnabled( false ); // disable sixJButton 768 sevenJButton.setEnabled( false ); // disable sevenJButton 769 eightJButton.setEnabled( false ); // disable eightJButton 770 nineJButton.setEnabled( false ); // disable nineJButton
93
2004 Prentice Hall, Inc. All rights reserved. Outline 93 ATM.java (32 of 37) 771 zeroJButton.setEnabled( false ); // disable zeroJButton 772 773 } // end method disableKeyPad 774 775 // withdraw amount from account 776 private void withdraw( double withdrawAmount ) 777 { 778 // determine if amount can be withdrawn 779 if ( withdrawAmount <= balance ) 780 { 781 balance -= withdrawAmount; // calculate new balance 782 783 updateBalance(); // update row in database 784 785 // define display format 786 DecimalFormat dollars = new DecimalFormat( "0.00" ); 787 788 // display balance information to user 789 messageJTextArea.setText( "The withdrawal amount is $" + 790 dollars.format( withdrawAmount ) + "." ); 791 } 792 else // amount cannot be withdrawn 793 { 794 messageJTextArea.setText( 795 "The withdrawal amount is too large." +
94
2004 Prentice Hall, Inc. All rights reserved. Outline 94 ATM.java (33 of 37) 796 "\nSelect Withdraw and enter a different amount." ); 797 } 798 799 } // end method withdraw 800 801 // load account numbers to accountNumberJComboBox 802 private void loadAccountNumbers() 803 { 804 // get all account numbers from database 805 try 806 { 807 myResultSet = myStatement.executeQuery( 808 "SELECT accountNumber from accountInformation" ); 809 810 // add account numbers to accountNumberJComboBox 811 while ( myResultSet.next() ) 812 { 813 accountNumberJComboBox.addItem( 814 myResultSet.getString( "accountNumber" ) ); 815 } 816 817 myResultSet.close(); // close myResultSet 818 819 } // end try 820 Submit a query that selects the account numbers from the accountInformation table Process the ResultSet and fill accountNumberJCom- boBox with account numbers Close myResultSet to release database resources
95
2004 Prentice Hall, Inc. All rights reserved. Outline 95 ATM.java (34 of 37) 821 catch ( SQLException exception ) 822 { 823 exception.printStackTrace(); 824 } 825 826 } // end method loadAccountNumbers 827 828 // get account information from database 829 private void retrieveAccountInformation() 830 { 831 // get account information 832 try 833 { 834 myResultSet = myStatement.executeQuery( "SELECT pin, " + 835 "firstName, balanceAmount FROM accountInformation " + 836 "WHERE accountNumber = '" + userAccountNumber + "'" ); 837 838 // get next result 839 if ( myResultSet.next() ) 840 { 841 pin = myResultSet.getString( “pin" ); 842 firstName = myResultSet.getString( "firstName" ); 843 balance = myResultSet.getDouble( "balanceAmount" ); 844 } 845 Catch any SQLExceptions thrown from the try block Submit a query that selects the pin, firstName and balanceAmount values for the specified account number Get the pin, firstName and balanceAmount values from the ResultSet
96
2004 Prentice Hall, Inc. All rights reserved. Outline 96 ATM.java (35 of 37) 846 myResultSet.close(); // close myResultSet 847 848 } // end try 849 850 catch ( SQLException exception ) 851 { 852 exception.printStackTrace(); 853 } 854 855 } // end method retrieveAccountInformation 856 857 // update database after withdrawing 858 private void updateBalance() 859 { 860 // update balance in database 861 try 862 { 863 myStatement.executeUpdate( "UPDATE accountInformation" + 864 " SET balanceAmount = " + balance + " WHERE " + 865 "accountNumber = '" + userAccountNumber + "'" ); 866 } Close myResultSet to release database resources Catch any SQLException s thrown from the try block Submit a SQL statement that updates the balanceAmount in the accountInformation table for the row with the specified accountNumber
97
2004 Prentice Hall, Inc. All rights reserved. Outline 97 ATM.java (36 of 37) 867 catch ( SQLException exception ) 868 { 869 exception.printStackTrace(); 870 } 871 872 } // end method updateBalance 873 874 // close statement and database connection 875 private void frameWindowClosing( WindowEvent event ) 876 { 877 // close myStatement and database connection 878 try 879 { 880 myStatement.close(); 881 myConnection.close(); 882 } 883 catch ( SQLException sqlException ) 884 { 885 sqlException.printStackTrace(); 886 } 887 finally 888 { 889 System.exit( 0 ); 890 } 891 Catch any SQLException s thrown from the try block Close myStatement and myConnection to release database resources Catch any SQLException s thrown from the try block Terminate the application
98
2004 Prentice Hall, Inc. All rights reserved. Outline 98 ATM.java (37 of 37) 892 } // end method frameWindowClosing 893 894 // method main 895 public static void main( String[] args ) 896 { 897 // check command-line arguments 898 if ( args.length == 2 ) 899 { 900 // get command-line arguments 901 String databaseDriver = args[ 0 ]; 902 String databaseURL = args[ 1 ]; 903 904 // create new ATM 905 ATM atm = new ATM( databaseDriver, databaseURL ); 906 } 907 else // invalid command-line arguments 908 { 909 System.out.println( 910 "Usage: java ATM databaseDriver databaseURL" ); 911 } 912 913 } // end method main 914 915 } // end class ATM Check command- line arguments Create new ATM instance Get command-line arguments Display a line of text that indicates the syntax to run the application
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.