Download ATM - ComSciGate

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