© Copyright 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
© Copyright 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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved IBM Cloudscape Database (Cont.) Figure 26.1 Welcome page of Cloudscape installer. (Courtesy of IBM Corporation.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved IBM Cloudscape Database (Cont.) Figure 26.2 Options to view the release notes. (Courtesy of IBM Corporation.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved IBM Cloudscape Database (Cont.) Figure 26.3 Cloudscape 5.1 release notes. (Courtesy of IBM Corporation.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved IBM Cloudscape Database (Cont.) Figure 26.4 Accepting the Cloudscape license agreement. (Courtesy of IBM Corporation.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved IBM Cloudscape Database (Cont.) Figure 26.5 Choosing the Cloudscape installation directory. (Courtesy of IBM Corporation.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved IBM Cloudscape Database (Cont.) Figure 26.6 Choosing the default setup type. (Courtesy of IBM Corporation.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved IBM Cloudscape Database (Cont.) Figure 26.7 Confirming the location of the Cloudscape installation. (Courtesy of IBM Corporation.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved IBM Cloudscape Database (Cont.) Figure 26.8 Cloudscape installation process dialog. (Courtesy of IBM Corporation.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved IBM Cloudscape Database (Cont.) Figure 26.9 Completing the Cloudscape installation. (Courtesy of IBM Corporation.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved IBM Cloudscape Database (Cont.) Figure Exiting the installer wizard. (Courtesy of IBM Corporation.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the ATM Application
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the ATM Application (Cont.) Figure Setting the CLASSPATH environment variable. Result of running setCP.bat
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the ATM Application (Cont.) Figure Passing command-line arguments to the ATM application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the ATM Application (Cont.) Figure 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the ATM Application (Cont.) Figure 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the ATM Application (Cont.) Figure Entering the PIN for the selected account. Enter JButton is enabledAn asterisk is displayed here for each keypad JButton pressed for the PIN
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the ATM Application (Cont.) Figure 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the ATM Application (Cont.) Figure Viewing the account balance. Balance displays in JTextArea
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the ATM Application (Cont.) Figure 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the ATM Application (Cont.) Figure ATM application displaying the withdrawal amount. Keypad JButton s are disabled Balance JButton is enabled Withdraw JButton is enabledDisplay the withdrawal amount
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the ATM Application (Cont.) Figure Checking new balance. Display new balance
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the ATM Application (Cont.) Figure 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Planning the ATM Application (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Planning the ATM Application (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Planning the ATM Application (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Planning the ATM Application (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Planning the ATM Application (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Planning the ATM Application (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Relational Database Overview: The ATM Database (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved SQL (Cont.) SELECT accountNumber, firstName FROM accountInformation
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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 = ’ ’
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved SQL (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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 = ‘
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved SQL (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Command-Line Arguments (Cont.) Figure 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Creating Database Connections JDBC API –Communicate and manipulate databases JDBC Driver –Provided by a DMNS vendor –Enable Java applications to access a particular database
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Creating Database Connections (Cont.) Importing the java.sql package Figure Importing the java.sql package. java.sql package –Perform database processing
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Creating Database Connections (Cont.) Figure Declaring instance variables for database processing. Declaring instance variables for managing the database connection
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Creating Database Connections (Cont.) Figure 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 or line 90 Catch ClassNotFoundException thrown from line 83
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Programming the ATM Application (Cont.) Figure 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Programming the ATM Application (Cont.) Figure 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Programming the ATM Application (Cont.) Figure 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Programming the ATM Application (Cont.) Figure Closing the database connection. Close Statement and Connection to release database resources Terminate the applicationCatch any SQLException s thrown from the try block
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Programming the ATM Application (Cont.) Figure Running the completed ATM application.
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.*; public class ATM extends JFrame 12 { 13 // JTextArea to display message 14 private JTextArea messageJTextArea; // JTextField to enter PIN or withdrawal amount 17 private JTextField numberJTextField; // JPanel for number JButtons 20 private JPanel buttonsJPanel; // JButtons for input of PIN or withdrawal amount 23 private JButton oneJButton; 24 private JButton twoJButton; 25 private JButton threeJButton; Importing java.sql package
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; // JButton to submit PIN or withdrawal amount 35 private JButton enterJButton; // JButton to view balance 38 private JButton balanceJButton; // JButton to withdraw from account 41 private JButton withdrawJButton; // JButton to close the transaction 44 private JButton doneJButton; // JPanel to get account numbers 47 private JPanel accountNumberJPanel; 48
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; // constants for user action 54 private final static int ENTER_PIN = 1; 55 private final static int WITHDRAWAL = 2; // instance variables used to store PIN and 58 // firstName from database 59 private String pin, firstName; // instance variable used to distinguish user action 62 private int action; // instance variables used to store user selected account number 65 // and PIN 66 private String userAccountNumber, userPIN; // instance variable used to store account balance 69 private double balance; 70
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; // 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 ); // connect to database 86 myConnection = 87 DriverManager.getConnection( databaseURL ); // 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 )
2004 Prentice Hall, Inc. All rights reserved. Outline 66 ATM.java (5 of 37) 96 catch ( ClassNotFoundException exception ) 97 { 98 exception.printStackTrace(); 99 } createUserInterface(); // set up GUI } // end constructor // 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(); // enable explicit positioning of GUI components 112 contentPane.setLayout( null ); // 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
2004 Prentice Hall, Inc. All rights reserved. Outline 67 ATM.java (6 of 37) 121 messageJTextArea.setEditable( false ); 122 contentPane.add( messageJTextArea ); // 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 ); // 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 ); // 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 );
2004 Prentice Hall, Inc. All rights reserved. Outline 68 ATM.java (7 of 37) 146 oneJButton.addActionListener( 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 } } // end anonymous inner class ); // end call to addActionListener // 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( new ActionListener() // anonymous inner class 170 {
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 } } // end anonymous inner class ); // end call to addActionListener // 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( new ActionListener() // anonymous inner class 191 { 192 // event handler called when threeJButton is clicked 193 public void actionPerformed( ActionEvent event ) 194 { 195 threeJButtonActionPerformed( event );
2004 Prentice Hall, Inc. All rights reserved. Outline 70 ATM.java (9 of 37) 196 } } // end anonymous inner class ); // end call to addActionListener // 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( 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 } } // end anonymous inner class 220
2004 Prentice Hall, Inc. All rights reserved. Outline 71 ATM.java (10 of 37) 221 ); // end call to addActionListener // 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( 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 } } // end anonymous inner class ); // end call to addActionListener // set up sixJButton 245 sixJButton = new JButton();
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( 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 } } // end anonymous inner class ); // end call to addActionListener // set up sevenJButton 266 sevenJButton = new JButton(); 267 sevenJButton.setBounds( 53, 76, 24, 24 ); 268 sevenJButton.setText( "7" ); 269 sevenJButton.setBorder( 270 BorderFactory.createRaisedBevelBorder() );
2004 Prentice Hall, Inc. All rights reserved. Outline 73 ATM.java (12 of 37) 271 buttonsJPanel.add( sevenJButton ); 272 sevenJButton.addActionListener( 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 } } // end anonymous inner class ); // end call to addActionListener // 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( new ActionListener() // anonymous inner class
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 } } // end anonymous inner class ); // end call to addActionListener // 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( new ActionListener() // anonymous inner class 317 { 318 // event handler called when nineJButton is clicked 319 public void actionPerformed( ActionEvent event ) 320 {
2004 Prentice Hall, Inc. All rights reserved. Outline 75 ATM.java (14 of 37) 321 nineJButtonActionPerformed( event ); 322 } } // end anonymous inner class ); // end call to addActionListener // 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( 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 } } // end anonymous inner class
2004 Prentice Hall, Inc. All rights reserved. Outline 76 ATM.java (15 of 37) ); // end call to addActionListener disableKeyPad(); // disable numeric JButtons // 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( 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 } } // end anonymous inner class 370
2004 Prentice Hall, Inc. All rights reserved. Outline 77 ATM.java (16 of 37) 371 ); // end call to addActionListener // 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( 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 } } // end anonymous inner class ); // end call to addActionListener // set up withdrawJButton
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( 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 } } // end anonymous inner class ); // end call to addActionListener // set up doneJButton 418 doneJButton = new JButton(); 419 doneJButton.setBounds( 149, 113, 72, 24 ); 420 doneJButton.setText( "Done" );
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( 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 } } // end anonymous inner class ); // end call to addActionListener // 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 );
2004 Prentice Hall, Inc. All rights reserved. Outline 80 ATM.java (19 of 37) // set up accountNumberJLabel 448 accountNumberJLabel = new JLabel(); 449 accountNumberJLabel.setBounds( 25, 15, 100, 20 ); 450 accountNumberJLabel.setText( "Account Number:" ); 451 accountNumberJPanel.add( accountNumberJLabel ); // 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( 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 } } // end anonymous inner class 470
2004 Prentice Hall, Inc. All rights reserved. Outline 81 ATM.java (20 of 37) 471 ); // end call to addItemListener // read account numbers from database and 474 // place them in accountNumberJComboBox 475 loadAccountNumbers(); // 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 // ensure database connection is closed 483 // when user closes application window 484 addWindowListener( new WindowAdapter() // anonymous inner class 487 { 488 public void windowClosing( WindowEvent event ) 489 { 490 frameWindowClosing( event ); 491 } } // end anonymous inner class ); // end addWindowListener
2004 Prentice Hall, Inc. All rights reserved. Outline 82 ATM.java (21 of 37) } // end method createUserInterface // process oneJButton click 500 private void oneJButtonActionPerformed( ActionEvent event ) 501 { 502 zeroToNineJButtonActionPerformed( "1" ); } // end method oneJButtonActionPerformed // process twoJButton click 507 private void twoJButtonActionPerformed( ActionEvent event ) 508 { 509 zeroToNineJButtonActionPerformed( "2" ); } // end method twoJButtonActionPerformed // process threeJButton click 514 private void threeJButtonActionPerformed( ActionEvent event ) 515 { 516 zeroToNineJButtonActionPerformed( "3" ); } // end method threeJButtonActionPerformed // process fourJButton click
2004 Prentice Hall, Inc. All rights reserved. Outline 83 ATM.java (22 of 37) 521 private void fourJButtonActionPerformed( ActionEvent event ) 522 { 523 zeroToNineJButtonActionPerformed( "4" ); } // end method fourJButtonActionPerformed // process fiveJButton click 528 private void fiveJButtonActionPerformed( ActionEvent event ) 529 { 530 zeroToNineJButtonActionPerformed( "5" ); } // end method fiveJButtonActionPerformed // process sixJButton click 535 private void sixJButtonActionPerformed( ActionEvent event ) 536 { 537 zeroToNineJButtonActionPerformed( "6" ); } // end method sixJButtonActionPerformed // process sevenJButton click 542 private void sevenJButtonActionPerformed( ActionEvent event ) 543 { 544 zeroToNineJButtonActionPerformed( "7" ); 545
2004 Prentice Hall, Inc. All rights reserved. Outline 84 ATM.java (23 of 37) 546 } // end method sevenJButtonActionPerformed // process eightJButton click 549 private void eightJButtonActionPerformed( ActionEvent event ) 550 { 551 zeroToNineJButtonActionPerformed( "8" ); } // end method eightJButtonActionPerformed // process nineJButton click 556 private void nineJButtonActionPerformed( ActionEvent event ) 557 { 558 zeroToNineJButtonActionPerformed( "9" ); } // end method nineJButtonActionPerformed // process zeroJButton click 563 private void zeroJButtonActionPerformed( ActionEvent event ) 564 { 565 zeroToNineJButtonActionPerformed( "0" ); } // end method zeroJButtonActionPerformed // process clicks of a numeric JButton 570 private void zeroToNineJButtonActionPerformed( String number )
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 } // 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 } else // otherwise display number of JButton user clicked 587 { 588 numberJTextField.setText( 589 numberJTextField.getText() + number ); 590 } } // end method zeroToNineJButtonsActionPerformed // verify PIN or withdraw from account 595 private void enterJButtonActionPerformed( ActionEvent event )
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(); numberJTextField.setText( "" ); // clear numberJTextField // correct PIN number 606 if ( userPIN.equals( pin ) ) 607 { 608 // disable enterJButton 609 enterJButton.setEnabled( false ); disableKeyPad(); // disable numeric JButtons // enable balanceJButton and withdrawJButton 614 balanceJButton.setEnabled( true ); 615 withdrawJButton.setEnabled( true ); // display status to user 618 messageJTextArea.setText( 619 "Welcome " + firstName + ", select a transaction." ); 620
2004 Prentice Hall, Inc. All rights reserved. Outline 87 ATM.java (26 of 37) 621 } // end if part of if...else else // wrong PIN number 624 { 625 // indicate that incorrect PIN was provided 626 messageJTextArea.setText( 627 "Sorry, PIN number is incorrect." "\nPlease re-enter the PIN number." ); userPIN = ""; // clear user's previous PIN entry } // end else part of if...else } // end if that processes PIN else if ( action == WITHDRAWAL ) // process withdrawal 637 { 638 enterJButton.setEnabled( false ); // disable enterJButton disableKeyPad(); // disable numeric JButtons // process withdrawal 643 withdraw( 644 Double.parseDouble( numberJTextField.getText() ) ); 645
2004 Prentice Hall, Inc. All rights reserved. Outline 88 ATM.java (27 of 37) 646 numberJTextField.setText( "" ); // clear numberJTextField // enable balanceJButton and withdrawJButton 649 balanceJButton.setEnabled( true ); 650 withdrawJButton.setEnabled( true ); } // end if that processes withdrawal } // end method enterJButtonActionPerformed // display account balance 657 private void balanceJButtonActionPerformed( ActionEvent event ) 658 { 659 // define display format 660 DecimalFormat dollars = new DecimalFormat( "0.00" ); // display user's balance 663 messageJTextArea.setText( "Your current balance is $" dollars.format( balance ) + "." ); } // end method balanceJButtonActionPerformed // display withdraw action 669 private void withdrawJButtonActionPerformed( ActionEvent event ) 670 {
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 ); enableKeyPad(); // enable numeric JButtons // display message to user 678 messageJTextArea.setText( 679 "Enter the amount you would like to withdraw" ); // change action to indicate user will provide 682 // withdrawal amount 683 action = WITHDRAWAL; } // end method withdrawJButtonActionPerformed // reset GUI 688 private void doneJButtonActionPerformed( ActionEvent event ) 689 { 690 userPIN = ""; // clear userPIN disableKeyPad(); // disable numeric JButtons // disable OK, Balance, Withdraw and Done JButtons 695 enterJButton.setEnabled( false );
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 ); // enable and reset accountNumberJComboBox 701 accountNumberJComboBox.setEnabled( true ); 702 accountNumberJComboBox.setSelectedIndex( 0 ); // reset messageJTextArea 705 messageJTextArea.setText( 706 "Please select your account number." ); } // end method doneJButtonActionPerformed // 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 );
2004 Prentice Hall, Inc. All rights reserved. Outline 91 ATM.java (30 of 37) // get selected account number 723 userAccountNumber = 724 ( String ) accountNumberJComboBox.getSelectedItem(); // change action to indicate that user will provide 727 // PIN number 728 action = ENTER_PIN; 729 userPIN = ""; // prompt user to enter PIN number 732 messageJTextArea.setText( 733 "Please enter your PIN number." ); numberJTextField.setText( "" ); // clear numberJTextField 736 enableKeyPad(); // enable numeric JButtons 737 doneJButton.setEnabled( true ); // enable doneJButton } // end if } // end method accountNumberJComboBoxItemStateChanged // enable numeric JButtons 744 private void enableKeyPad() 745 {
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 } // end method enableKeyPad // 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
2004 Prentice Hall, Inc. All rights reserved. Outline 93 ATM.java (32 of 37) 771 zeroJButton.setEnabled( false ); // disable zeroJButton } // end method disableKeyPad // 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 updateBalance(); // update row in database // define display format 786 DecimalFormat dollars = new DecimalFormat( "0.00" ); // display balance information to user 789 messageJTextArea.setText( "The withdrawal amount is $" dollars.format( withdrawAmount ) + "." ); 791 } 792 else // amount cannot be withdrawn 793 { 794 messageJTextArea.setText( 795 "The withdrawal amount is too large." +
2004 Prentice Hall, Inc. All rights reserved. Outline 94 ATM.java (33 of 37) 796 "\nSelect Withdraw and enter a different amount." ); 797 } } // end method withdraw // 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" ); // add account numbers to accountNumberJComboBox 811 while ( myResultSet.next() ) 812 { 813 accountNumberJComboBox.addItem( 814 myResultSet.getString( "accountNumber" ) ); 815 } myResultSet.close(); // close myResultSet } // 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
2004 Prentice Hall, Inc. All rights reserved. Outline 95 ATM.java (34 of 37) 821 catch ( SQLException exception ) 822 { 823 exception.printStackTrace(); 824 } } // end method loadAccountNumbers // get account information from database 829 private void retrieveAccountInformation() 830 { 831 // get account information 832 try 833 { 834 myResultSet = myStatement.executeQuery( "SELECT pin, " "firstName, balanceAmount FROM accountInformation " "WHERE accountNumber = '" + userAccountNumber + "'" ); // 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
2004 Prentice Hall, Inc. All rights reserved. Outline 96 ATM.java (35 of 37) 846 myResultSet.close(); // close myResultSet } // end try catch ( SQLException exception ) 851 { 852 exception.printStackTrace(); 853 } } // end method retrieveAccountInformation // update database after withdrawing 858 private void updateBalance() 859 { 860 // update balance in database 861 try 862 { 863 myStatement.executeUpdate( "UPDATE accountInformation" " SET balanceAmount = " + balance + " WHERE " "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
2004 Prentice Hall, Inc. All rights reserved. Outline 97 ATM.java (36 of 37) 867 catch ( SQLException exception ) 868 { 869 exception.printStackTrace(); 870 } } // end method updateBalance // 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
2004 Prentice Hall, Inc. All rights reserved. Outline 98 ATM.java (37 of 37) 892 } // end method frameWindowClosing // 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 ]; // 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 } } // end method main } // 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