tutorial 26 – atm application introducing database programming and using command-line arguments

Post on 19-Jan-2016

42 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Tutorial 26 – ATM Application Introducing Database Programming and Using Command-Line Arguments. - PowerPoint PPT Presentation

TRANSCRIPT

1

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Outline26.1 IBM Cloudscape Database26.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 Arguments26.7 Creating Database Connections26.8 Programming the Screen Saver Application26.9 Wrap-Up

Tutorial 26 – ATM ApplicationIntroducing Database Programming and Using

Command-Line Arguments

2

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

26.2 Test-Driving the ATM Application

Application Requirements A local bank has asked you to create a prototype automated teller machine (ATM) application to access a database that contains sample customer records. Each record consists of an account number, Personal Identification Number (PIN), first name and balance amount. For testing purposes, valid account numbers will be provided in a JComboBox. The ATM application should allow the user to log in to an account by providing a valid PIN. Once logged in, the user should be able to view the account balance and withdraw money from the account (if the account contains sufficient funds). If money is withdrawn, the application should update the database.

15

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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.

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.

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.

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 JButtons are disabled initially

JComboBox that displays account numbers

19

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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 JButtons are enabled

JComboBox is disabled

Done JButton is enabled

20

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.2 Test-Driving the ATM Application (Cont.)

Figure 26.15 Entering the PIN for the selected account.

Enter JButton is enabled

An 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.

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 JButtons 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.

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.

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.

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 JButtons 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.

26.2 Test-Driving the ATM Application (Cont.)

Figure 26.19 ATM application displaying the withdrawal amount.

Keypad JButtons are disabled

Balance JButton is enabled

Withdraw JButton is enabled

Display the withdrawal amount

26

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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.

26.2 Test-Driving the ATM Application (Cont.)

Figure 26.21 ATM application ready for next customer.

JComboBox is enabled

JButtons are disabled

Displays instructions and messages to the user

Keypad JButtons are disabled

28

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.3 Planning the ATM Application

When the user selects an account number from the JComboBoxDisable the JComboBoxPrompt the user to enter a PINClear the JTextField for the PINEnable the keypad JbuttonsEnable the Done JButton

When the user enters the PIN Enable the Enter JbuttonAppend the number to the PIN

29

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.3 Planning the ATM Application (Cont.)

When the user clicks the Enter JButton to submit the PINSearch the database for the account number’s corresponding account informationIf the user provided a correct PIN

Clear the JTextFieldDisable the Enter JbuttonDisable the keypad JbuttonsEnable the Balance and Withdraw JbuttonsDisplay the status to the user

ElseClear the JTextFieldPrompt the user to enter a valid PIN

When the user clicks the Balance JbuttonDisplay the balance

30

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.3 Planning the ATM Application (Cont.)

When the user clicks the Withdraw JbuttonDisable the Balance and Withdraw JbuttonsEnable the keypad JbuttonsPrompt the user to enter the withdrawal amount

When the user clicks the Enter JButton to submit the withdrawal amountDisable the Enter JbuttonDisable the keypad JbuttonsProcess the withdrawal and display the withdrawal amountClear the withdrawal amount in the JTextFieldEnable the Balance and Withdraw JButtons

When the user clicks the Done JbuttonDisable the keypad JbuttonsDisable the Enter, Balance, Withdraw and Done JbuttonsEnable the JComboBoxDisplay instructions for the next customer in the JTextArea

31

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.3 Planning the ATM Application (Cont.)

Action Component Event User selects an account number from the JComboBox

accountNumberJComboBox User selects item from Account Numbers: JComboBox

Disable the JComboBox accountNumberJComboBox

Prompt user to enter a PIN messageJTextArea

Clear the JTextField for the PIN

numberJTextField

Enable the keypad JButtons zeroJButton, oneJButton, twoJButton, threeJButton, fourJButton, fiveJButton, sixJButton, sevenJButton, eightJButton, nineJButton

Enable the Done JButton doneJButton

Figure 26.22 ACE table for the ATM application (Part 1 of 6).

32

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.3 Planning the ATM Application (Cont.)

Action Component Event/Method User enters the PIN zeroJButton,

oneJButton, twoJButton, threeJButton, fourJButton, fiveJButton, sixJButton, sevenJButton, eightJButton, nineJButton

User clicks a keypad JButton

Enable the Enter JButton enterJButton

Append the number to the PIN numberJTextField

User clicks the Enter JButton to submit the PIN

enterJButton User clicks Enter

JButton

Search database for the account number’s corresponding account information

myStatement, myResultSet

executeQuery, close, next, getString, getDouble

User provided a correct PIN

Figure 26.22 ACE table for the ATM application (Part 2 of 6).

33

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.3 Planning the ATM Application (Cont.)

Action Component Event/Method Clear the JTextField numberJTextField

Disable the Enter JButton enterJButton

Disable the keypad JButtons zeroJButton, oneJButton, twoJButton, threeJButton, fourJButton, fiveJButton, sixJButton, sevenJButton, eightJButton, nineJButton

Enable the Balance and Withdraw JButtons

balanceJButton, withdrawJButton

Display status to the user messageJTextArea

User did not provide a correct PIN

Clear the JTextField numberJTextField

Figure 26.22 ACE table for the ATM application (Part 3 of 6).

34

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.3 Planning the ATM Application (Cont.)

Action Component Event/Method Prompt the user to enter a valid PIN

messageJTextArea

User clicks the Balance JButton balanceJButton User clicks Balance JButton

Display the balance messageJTextArea

User clicks the Withdraw JButton

withdrawJButton User clicks Withdraw JButton

Disable the Balance and Withdraw JButtons

balanceJButton, withdrawJButton

Enable the keypad JButtons zeroJButton, oneJButton, twoJButton, threeJButton, fourJButton, fiveJButton, sixJButton, sevenJButton, eightJButton, nineJButton

Prompt the user to enter the withdrawal amount

messageJTextArea

Figure 26.22 ACE table for the ATM application (Part 4 of 6).

35

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.3 Planning the ATM Application (Cont.)

Action Component Event/Method User clicks the Enter JButton to submit the withdrawal amount

enterJButton User clicks Enter JButton

Disable the Enter JButton enterJButton

Disable the keypad JButtons zeroJButton, oneJButton, twoJButton, threeJButton, fourJButton, fiveJButton, sixJButton, sevenJButton, eightJButton, nineJButton

Process the withdrawal and display the withdrawal amount

myStatement, messageJTextArea

Clear withdrawal amount in the JTextField

numberJTextField

Enable the Balance and Withdraw JButtons

balanceJButton, withdrawJButton

User clicks the Done JButton doneJButton User clicks Done JButton

Figure 26.22 ACE table for the ATM application (Part 5 of 6).

36

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.3 Planning the ATM Application (Cont.)

Action Component Event/Method Disable the keypad JButtons zeroJButton,

oneJButton, twoJButton, threeJButton, fourJButton, fiveJButton, sixJButton, sevenJButton, eightJButton, nineJButton

Disable the Enter, Balance, Withdraw and Done JButtons

enterJButton, balanceJButton, withdrawJButton, doneJButton

Enable the JComboBox accountNumber-JComboBox

Display instructions for the next customer in the JTextArea

messageJTextArea

Figure 26.22 ACE table for the ATM application (Part 6 of 6).

37

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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.

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.

26.4 Relational Database Overview: The ATM Database (Cont.)

accountNumber pin firstName balanceAmount 12548693 1234 John 980.0

24578648 8568 Susan 125.0

35682458 5689 Joseph 3400.99

45632598 8790 Michael 1254.76

52489635 2940 Donna 9200.02

55698632 3457 Elizabeth 788.9

69857425 6765 Jennifer 677.87

71869534 5678 Al 7799.24

88965723 1245 Ben 736.78

98657425 2456 Bob 946.09

Figure 26.23 accountInformation table of the ATM database.

40

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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.

26.5 SQL (Cont.)

accountNumber firstName 12548693 John

24578648 Susan

35682458 Joseph

45632598 Michael

52489635 Donna

55698632 Elizabeth

69857425 Jennifer

71869534 Al

88965723 Ben

98657425 Bob

Figure 26.24 Selecting the accountNumber and firstName columns of the accountInformation table.

SELECT accountNumber, firstName FROM accountInformation

42

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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.

26.5 SQL (Cont.)

pin firstName balanceAmount 1234 John 980.0

Figure 26.25 Selecting the pin, firstName and balanceAmount for the person with accountNumber 12548693

44

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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 = 1000WHERE accountNumber = ‘12548693

45

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.5 SQL (Cont.)

accountNumber pin firstName balanceAmount 12548693 1234 John 1000.0

24578648 8568 Susan 125.0

35682458 5689 Joseph 3400.99

45632598 8790 Michael 1254.76

52489635 2940 Donna 9200.02

55698632 3457 Elizabeth 788.9

69857425 6765 Jennifer 677.87

71869534 5678 Al 7799.24

88965723 1245 Ben 736.78

98657425 2456 Bob 946.09

Figure 26.26 accountInformation table after executing an UPDATE statement.

46

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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.

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.

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.

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.

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.

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.

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.

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 object

Catch any SQLExceptions 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.

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.

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.

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 SQLExceptions thrown from the try block

57

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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.

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 SQLExceptions thrown from the try block

59

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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.

26.8 Programming the ATM Application (Cont.)

Figure 26.34 Closing the database connection.

Close Statement and Connection to release

database resources

Terminate the application

Catch any SQLExceptions thrown from the try block

61

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.8 Programming the ATM Application (Cont.)

Figure 26.35 Running the completed ATM application.

2004 Prentice Hall, Inc.All rights reserved.

Outline62

ATM.java (1 of 37)

1 // Tutorial 26: ATM.java2 // 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 JFrame12 {13 // JTextArea to display message14 private JTextArea messageJTextArea;15

16 // JTextField to enter PIN or withdrawal amount17 private JTextField numberJTextField;18

19 // JPanel for number JButtons20 private JPanel buttonsJPanel;21

22 // JButtons for input of PIN or withdrawal amount23 private JButton oneJButton;24 private JButton twoJButton;25 private JButton threeJButton;

Importing java.sql package

2004 Prentice Hall, Inc.All rights reserved.

Outline63

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 amount35 private JButton enterJButton;36

37 // JButton to view balance38 private JButton balanceJButton;39

40 // JButton to withdraw from account41 private JButton withdrawJButton;42

43 // JButton to close the transaction44 private JButton doneJButton;45

46 // JPanel to get account numbers47 private JPanel accountNumberJPanel;48

2004 Prentice Hall, Inc.All rights reserved.

Outline64

ATM.java (3 of 37)

49 // JLabel and JComboBox for account numbers50 private JLabel accountNumberJLabel;51 private JComboBox accountNumberJComboBox;52

53 // constants for user action54 private final static int ENTER_PIN = 1;55 private final static int WITHDRAWAL = 2;56

57 // instance variables used to store PIN and58 // firstName from database59 private String pin, firstName;60

61 // instance variable used to distinguish user action62 private int action;63

64 // instance variables used to store user selected account number65 // and PIN66 private String userAccountNumber, userPIN;67

68 // instance variable used to store account balance69 private double balance;70

2004 Prentice Hall, Inc.All rights reserved.

Outline65

ATM.java (4 of 37)

71 // instance variables used to manipulate database72 private Connection myConnection; 73 private Statement myStatement; 74 private ResultSet myResultSet; 75

76 // constructor77 public ATM( String databaseDriver, String databaseURL )78 {79 // establish connection to database80 try81 {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)

2004 Prentice Hall, Inc.All rights reserved.

Outline66

ATM.java (5 of 37)

96 catch ( ClassNotFoundException exception )97 {98 exception.printStackTrace();99 }100

101 createUserInterface(); // set up GUI102 103 } // end constructor104

105 // create and position GUI components; register event handler106 private void createUserInterface()107 {108 // get content pane for attaching GUI components109 Container contentPane = getContentPane();110

111 // enable explicit positioning of GUI components112 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

2004 Prentice Hall, Inc.All rights reserved.

Outline67

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 buttonsJPanel133 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 oneJButton140 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.

Outline68

ATM.java (7 of 37)

146 oneJButton.addActionListener(147

148 new ActionListener() // anonymous inner class149 {150 // event handler called when oneJButton is clicked151 public void actionPerformed( ActionEvent event )152 {153 oneJButtonActionPerformed( event );154 }155

156 } // end anonymous inner class157

158 ); // end call to addActionListener159

160 // set up twoJButton161 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 class170 {

2004 Prentice Hall, Inc.All rights reserved.

Outline69

ATM.java (8 of 37)

171 // event handler called when twoJButton is clicked172 public void actionPerformed( ActionEvent event )173 {174 twoJButtonActionPerformed( event );175 }176

177 } // end anonymous inner class178

179 ); // end call to addActionListener180

181 // set up threeJButton182 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 class191 {192 // event handler called when threeJButton is clicked193 public void actionPerformed( ActionEvent event )194 {195 threeJButtonActionPerformed( event );

2004 Prentice Hall, Inc.All rights reserved.

Outline70

ATM.java (9 of 37)

196 }197

198 } // end anonymous inner class199

200 ); // end call to addActionListener201

202 // set up fourJButton203 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 class212 {213 // event handler called when fourJButton is clicked214 public void actionPerformed( ActionEvent event )215 {216 fourJButtonActionPerformed( event );217 }218

219 } // end anonymous inner class220

2004 Prentice Hall, Inc.All rights reserved.

Outline71

ATM.java (10 of 37)

221 ); // end call to addActionListener222 223 // set up fiveJButton224 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 class233 {234 // event handler called when fiveJButton is clicked235 public void actionPerformed( ActionEvent event )236 {237 fiveJButtonActionPerformed( event );238 }239

240 } // end anonymous inner class241

242 ); // end call to addActionListener243

244 // set up sixJButton245 sixJButton = new JButton();

2004 Prentice Hall, Inc.All rights reserved.

Outline72

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 class254 {255 // event handler called when sixJButton is clicked256 public void actionPerformed( ActionEvent event )257 {258 sixJButtonActionPerformed( event );259 }260

261 } // end anonymous inner class262

263 ); // end call to addActionListener264

265 // set up sevenJButton266 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.

Outline73

ATM.java (12 of 37)

271 buttonsJPanel.add( sevenJButton );272 sevenJButton.addActionListener(273

274 new ActionListener() // anonymous inner class275 {276 // event handler called when sevenJButton is clicked277 public void actionPerformed( ActionEvent event )278 {279 sevenJButtonActionPerformed( event );280 }281

282 } // end anonymous inner class283

284 ); // end call to addActionListener285

286 // set up eightJButton287 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

2004 Prentice Hall, Inc.All rights reserved.

Outline74

ATM.java (13 of 37)

296 {297 // event handler called when eightJButton is clicked298 public void actionPerformed( ActionEvent event )299 {300 eightJButtonActionPerformed( event );301 }302

303 } // end anonymous inner class304

305 ); // end call to addActionListener306

307 // set up nineJButton308 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 class317 {318 // event handler called when nineJButton is clicked319 public void actionPerformed( ActionEvent event )320 {

2004 Prentice Hall, Inc.All rights reserved.

Outline75

ATM.java (14 of 37)

321 nineJButtonActionPerformed( event );322 }323

324 } // end anonymous inner class325

326 ); // end call to addActionListener327

328 // set up zeroJButton329 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 class338 {339 // event handler called when zeroJButton is clicked340 public void actionPerformed( ActionEvent event )341 {342 zeroJButtonActionPerformed( event );343 }344

345 } // end anonymous inner class

2004 Prentice Hall, Inc.All rights reserved.

Outline76

ATM.java (15 of 37)

346

347 ); // end call to addActionListener348

349 disableKeyPad(); // disable numeric JButtons350

351 // set up enterJButton352 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 class362 {363 // event handler called when enterJButton is clicked364 public void actionPerformed( ActionEvent event )365 {366 enterJButtonActionPerformed( event );367 }368

369 } // end anonymous inner class370

2004 Prentice Hall, Inc.All rights reserved.

Outline77

ATM.java (16 of 37)

371 ); // end call to addActionListener372

373 // set up balanceJButton374 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 class384 {385 // event handler called when balanceJButton is clicked386 public void actionPerformed( ActionEvent event )387 {388 balanceJButtonActionPerformed( event );389 }390

391 } // end anonymous inner class392

393 ); // end call to addActionListener394

395 // set up withdrawJButton

2004 Prentice Hall, Inc.All rights reserved.

Outline78

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 class406 {407 // event handler called when withdrawJButton is clicked408 public void actionPerformed( ActionEvent event )409 {410 withdrawJButtonActionPerformed( event );411 }412

413 } // end anonymous inner class414

415 ); // end call to addActionListener416

417 // set up doneJButton418 doneJButton = new JButton();419 doneJButton.setBounds( 149, 113, 72, 24 );420 doneJButton.setText( "Done" );

2004 Prentice Hall, Inc.All rights reserved.

Outline79

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 class428 {429 // event handler called when doneJButton is clicked430 public void actionPerformed( ActionEvent event )431 {432 doneJButtonActionPerformed( event );433 }434

435 } // end anonymous inner class436

437 ); // end call to addActionListener438

439 // set up accountNumberJPanel440 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.

Outline80

ATM.java (19 of 37)

446

447 // set up accountNumberJLabel448 accountNumberJLabel = new JLabel();449 accountNumberJLabel.setBounds( 25, 15, 100, 20 );450 accountNumberJLabel.setText( "Account Number:" );451 accountNumberJPanel.add( accountNumberJLabel );452

453 // set up accountNumberJComboBox454 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 class462 {463 // event handler called when account number is chosen464 public void itemStateChanged( ItemEvent event )465 {466 accountNumberJComboBoxItemStateChanged( event );467 }468

469 } // end anonymous inner class470

2004 Prentice Hall, Inc.All rights reserved.

Outline81

ATM.java (20 of 37)

471 ); // end call to addItemListener472

473 // read account numbers from database and 474 // place them in accountNumberJComboBox475 loadAccountNumbers();476

477 // set properties of application's window478 setTitle( "ATM" ); // set title bar string479 setSize( 375, 410 ); // set window size480 setVisible( true ); // display window481

482 // ensure database connection is closed483 // when user closes application window484 addWindowListener(485

486 new WindowAdapter() // anonymous inner class487 {488 public void windowClosing( WindowEvent event )489 {490 frameWindowClosing( event );491 }492

493 } // end anonymous inner class494

495 ); // end addWindowListener

2004 Prentice Hall, Inc.All rights reserved.

Outline82

ATM.java (21 of 37)

496

497 } // end method createUserInterface498

499 // process oneJButton click500 private void oneJButtonActionPerformed( ActionEvent event )501 {502 zeroToNineJButtonActionPerformed( "1" );503

504 } // end method oneJButtonActionPerformed505

506 // process twoJButton click507 private void twoJButtonActionPerformed( ActionEvent event )508 {509 zeroToNineJButtonActionPerformed( "2" );510

511 } // end method twoJButtonActionPerformed512

513 // process threeJButton click514 private void threeJButtonActionPerformed( ActionEvent event )515 {516 zeroToNineJButtonActionPerformed( "3" );517

518 } // end method threeJButtonActionPerformed519

520 // process fourJButton click

2004 Prentice Hall, Inc.All rights reserved.

Outline83

ATM.java (22 of 37)

521 private void fourJButtonActionPerformed( ActionEvent event )522 {523 zeroToNineJButtonActionPerformed( "4" );524

525 } // end method fourJButtonActionPerformed526

527 // process fiveJButton click528 private void fiveJButtonActionPerformed( ActionEvent event )529 {530 zeroToNineJButtonActionPerformed( "5" );531

532 } // end method fiveJButtonActionPerformed533

534 // process sixJButton click535 private void sixJButtonActionPerformed( ActionEvent event )536 {537 zeroToNineJButtonActionPerformed( "6" );538

539 } // end method sixJButtonActionPerformed540

541 // process sevenJButton click542 private void sevenJButtonActionPerformed( ActionEvent event )543 {544 zeroToNineJButtonActionPerformed( "7" );545

2004 Prentice Hall, Inc.All rights reserved.

Outline84

ATM.java (23 of 37)

546 } // end method sevenJButtonActionPerformed547

548 // process eightJButton click549 private void eightJButtonActionPerformed( ActionEvent event )550 {551 zeroToNineJButtonActionPerformed( "8" );552

553 } // end method eightJButtonActionPerformed554

555 // process nineJButton click556 private void nineJButtonActionPerformed( ActionEvent event )557 {558 zeroToNineJButtonActionPerformed( "9" );559

560 } // end method nineJButtonActionPerformed561

562 // process zeroJButton click563 private void zeroJButtonActionPerformed( ActionEvent event )564 {565 zeroToNineJButtonActionPerformed( "0" );566

567 } // end method zeroJButtonActionPerformed568

569 // process clicks of a numeric JButton570 private void zeroToNineJButtonActionPerformed( String number )

2004 Prentice Hall, Inc.All rights reserved.

Outline85

ATM.java (24 of 37)

571 {572 // enable enterJButton if it is disabled573 if ( !enterJButton.isEnabled() )574 {575 enterJButton.setEnabled( true ); 576 }577 578 // if user is entering PIN number display * to conceal PIN579 if ( action == ENTER_PIN )580 {581 userPIN += number; // append number to current PIN582 numberJTextField.setText( 583 numberJTextField.getText() + "*" );584 }585

586 else // otherwise display number of JButton user clicked587 {588 numberJTextField.setText( 589 numberJTextField.getText() + number );590 }591

592 } // end method zeroToNineJButtonsActionPerformed593

594 // verify PIN or withdraw from account595 private void enterJButtonActionPerformed( ActionEvent event )

2004 Prentice Hall, Inc.All rights reserved.

Outline86

ATM.java (25 of 37)

596 {597 if ( action == ENTER_PIN ) // checking PIN598 {599 // get pin, first name and balance for account number600 // selected in accountNumberJComboBox601 retrieveAccountInformation(); 602

603 numberJTextField.setText( "" ); // clear numberJTextField604

605 // correct PIN number 606 if ( userPIN.equals( pin ) )607 {608 // disable enterJButton609 enterJButton.setEnabled( false ); 610

611 disableKeyPad(); // disable numeric JButtons612

613 // enable balanceJButton and withdrawJButton614 balanceJButton.setEnabled( true );615 withdrawJButton.setEnabled( true ); 616

617 // display status to user618 messageJTextArea.setText( 619 "Welcome " + firstName + ", select a transaction." );620

2004 Prentice Hall, Inc.All rights reserved.

Outline87

ATM.java (26 of 37)

621 } // end if part of if...else622 623 else // wrong PIN number624 {625 // indicate that incorrect PIN was provided626 messageJTextArea.setText( 627 "Sorry, PIN number is incorrect." +628 "\nPlease re-enter the PIN number." );629

630 userPIN = ""; // clear user's previous PIN entry631

632 } // end else part of if...else633

634 } // end if that processes PIN635

636 else if ( action == WITHDRAWAL ) // process withdrawal637 {638 enterJButton.setEnabled( false ); // disable enterJButton639

640 disableKeyPad(); // disable numeric JButtons641

642 // process withdrawal643 withdraw( 644 Double.parseDouble( numberJTextField.getText() ) );645

2004 Prentice Hall, Inc.All rights reserved.

Outline88

ATM.java (27 of 37)

646 numberJTextField.setText( "" ); // clear numberJTextField647

648 // enable balanceJButton and withdrawJButton649 balanceJButton.setEnabled( true );650 withdrawJButton.setEnabled( true ); 651

652 } // end if that processes withdrawal653

654 } // end method enterJButtonActionPerformed655

656 // display account balance657 private void balanceJButtonActionPerformed( ActionEvent event )658 {659 // define display format660 DecimalFormat dollars = new DecimalFormat( "0.00" );661

662 // display user's balance663 messageJTextArea.setText( "Your current balance is $" +664 dollars.format( balance ) + "." );665

666 } // end method balanceJButtonActionPerformed667

668 // display withdraw action669 private void withdrawJButtonActionPerformed( ActionEvent event )670 {

2004 Prentice Hall, Inc.All rights reserved.

Outline89

ATM.java (28 of 37)

671 // disable Balance and Withdraw JButtons672 balanceJButton.setEnabled( false );673 withdrawJButton.setEnabled( false );674

675 enableKeyPad(); // enable numeric JButtons676

677 // display message to user678 messageJTextArea.setText( 679 "Enter the amount you would like to withdraw" );680

681 // change action to indicate user will provide 682 // withdrawal amount683 action = WITHDRAWAL;684

685 } // end method withdrawJButtonActionPerformed686

687 // reset GUI688 private void doneJButtonActionPerformed( ActionEvent event )689 {690 userPIN = ""; // clear userPIN691

692 disableKeyPad(); // disable numeric JButtons693

694 // disable OK, Balance, Withdraw and Done JButtons695 enterJButton.setEnabled( false );

2004 Prentice Hall, Inc.All rights reserved.

Outline90

ATM.java (29 of 37)

696 balanceJButton.setEnabled( false );697 withdrawJButton.setEnabled( false );698 doneJButton.setEnabled( false );699

700 // enable and reset accountNumberJComboBox701 accountNumberJComboBox.setEnabled( true );702 accountNumberJComboBox.setSelectedIndex( 0 );703

704 // reset messageJTextArea 705 messageJTextArea.setText( 706 "Please select your account number." );707

708 } // end method doneJButtonActionPerformed709

710 // get account number and enable OK and Done JButtons711 private void accountNumberJComboBoxItemStateChanged( 712 ItemEvent event )713 {714 // get user selected account number if no transaction is715 // in process716 if ( ( event.getStateChange() == ItemEvent.SELECTED ) && 717 ( accountNumberJComboBox.getSelectedIndex() != 0 ) )718 {719 // disable accountNumberJComboBox720 accountNumberJComboBox.setEnabled( false );

2004 Prentice Hall, Inc.All rights reserved.

Outline91

ATM.java (30 of 37)

721

722 // get selected account number723 userAccountNumber = 724 ( String ) accountNumberJComboBox.getSelectedItem();725 726 // change action to indicate that user will provide727 // PIN number728 action = ENTER_PIN;729 userPIN = "";730

731 // prompt user to enter PIN number732 messageJTextArea.setText( 733 "Please enter your PIN number." );734 735 numberJTextField.setText( "" ); // clear numberJTextField736 enableKeyPad(); // enable numeric JButtons737 doneJButton.setEnabled( true ); // enable doneJButton738

739 } // end if740

741 } // end method accountNumberJComboBoxItemStateChanged742

743 // enable numeric JButtons744 private void enableKeyPad()745 {

2004 Prentice Hall, Inc.All rights reserved.

Outline92

ATM.java (31 of 37)

746 oneJButton.setEnabled( true ); // enable oneJButton747 twoJButton.setEnabled( true ); // enable twoJButton748 threeJButton.setEnabled( true ); // enable threeJButton749 fourJButton.setEnabled( true ); // enable fourJButton750 fiveJButton.setEnabled( true ); // enable fiveJButton751 sixJButton.setEnabled( true ); // enable sixJButton752 sevenJButton.setEnabled( true ); // enable sevenJButton753 eightJButton.setEnabled( true ); // enable eightJButton754 nineJButton.setEnabled( true ); // enable nineJButton755 zeroJButton.setEnabled( true ); // enable zeroJButton756

757 } // end method enableKeyPad758

759 // disable numeric JButtons760 private void disableKeyPad()761 {762 oneJButton.setEnabled( false ); // disable oneJButton763 twoJButton.setEnabled( false ); // disable twoJButton764 threeJButton.setEnabled( false ); // disable threeJButton765 fourJButton.setEnabled( false ); // disable fourJButton766 fiveJButton.setEnabled( false ); // disable fiveJButton767 sixJButton.setEnabled( false ); // disable sixJButton768 sevenJButton.setEnabled( false ); // disable sevenJButton769 eightJButton.setEnabled( false ); // disable eightJButton770 nineJButton.setEnabled( false ); // disable nineJButton

2004 Prentice Hall, Inc.All rights reserved.

Outline93

ATM.java (32 of 37)

771 zeroJButton.setEnabled( false ); // disable zeroJButton772

773 } // end method disableKeyPad774

775 // withdraw amount from account776 private void withdraw( double withdrawAmount )777 {778 // determine if amount can be withdrawn779 if ( withdrawAmount <= balance )780 {781 balance -= withdrawAmount; // calculate new balance782

783 updateBalance(); // update row in database784

785 // define display format786 DecimalFormat dollars = new DecimalFormat( "0.00" );787

788 // display balance information to user789 messageJTextArea.setText( "The withdrawal amount is $" +790 dollars.format( withdrawAmount ) + "." );791 }792 else // amount cannot be withdrawn793 {794 messageJTextArea.setText(795 "The withdrawal amount is too large." +

2004 Prentice Hall, Inc.All rights reserved.

Outline94

ATM.java (33 of 37)

796 "\nSelect Withdraw and enter a different amount." );797 }798

799 } // end method withdraw800

801 // load account numbers to accountNumberJComboBox802 private void loadAccountNumbers()803 {804 // get all account numbers from database805 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 myResultSet818

819 } // end try820

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.

Outline95

ATM.java (34 of 37)

821 catch ( SQLException exception )822 { 823 exception.printStackTrace(); 824 } 825

826 } // end method loadAccountNumbers827

828 // get account information from database829 private void retrieveAccountInformation()830 {831 // get account information832 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

2004 Prentice Hall, Inc.All rights reserved.

Outline96

ATM.java (35 of 37)

846 myResultSet.close(); // close myResultSet847

848 } // end try849

850 catch ( SQLException exception )851 { 852 exception.printStackTrace(); 853 } 854

855 } // end method retrieveAccountInformation856

857 // update database after withdrawing858 private void updateBalance()859 {860 // update balance in database861 try862 {863 myStatement.executeUpdate( "UPDATE accountInformation" +864 " SET balanceAmount = " + balance + " WHERE " + 865 "accountNumber = '" + userAccountNumber + "'" ); 866 }

Close myResultSet to release database resources

Catch any SQLExceptions 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.

Outline97

ATM.java (36 of 37)

867 catch ( SQLException exception )868 { 869 exception.printStackTrace(); 870 } 871

872 } // end method updateBalance873

874 // close statement and database connection875 private void frameWindowClosing( WindowEvent event )876 {877 // close myStatement and database connection878 try879 {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 SQLExceptions thrown from the try block

Close myStatement and myConnection to release database resources

Catch any SQLExceptions thrown from the try block

Terminate the application

2004 Prentice Hall, Inc.All rights reserved.

Outline98

ATM.java (37 of 37)

892 } // end method frameWindowClosing893

894 // method main895 public static void main( String[] args ) 896 {897 // check command-line arguments898 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 arguments908 {909 System.out.println( 910 "Usage: java ATM databaseDriver databaseURL" );911 }912

913 } // end method main914

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

top related