![]() |
|||
![]() ![]() |
![]() |
|
![]() |
The GUI part of this applet was written using Marimba Bongo, which generates a 100-percent portable .gui file. This file contains a persistent form of the widgets used in this example. The .gui file can be edited using Marimba Bongo, a demo version of which is on the CD-ROM accompanying this book. The unzipped Marimba classes must be in the CLASSPATH or present on the WWW server to run this example. The GUI part of the application uses the Marimba Bongo classes that are persistified to a portable file, which is the main reason why almost no GUI code is present in the source code. Each GUI control is a Marimba widget, which has a name and various properties that are also persistified in the permanent GUI file. Figure 10-5 shows the welcome screen. It prompts for an account number and a PIN code. There is a status bar below the validate button. If the PIN code is incorrect, this status bar displays an error message.
The names of the main controls are:
The values associated with these controls are checked and set within the program; the control names refer to them as instances of the Marimba GUI widgets. Once the client has logged in, a lookup is performed in the database, and the customers account balance and transaction history are displayed, as shown in Figure 10-6.
These are the controls:
The clients account balance and transaction history display each time the refresh button is pressed. The next screen panel allows transfers to other accounts. The name and address of the recipient is looked up in the database and displayed in the status bar. Then the amount of money is transferred, as shown in Figure 10-7.
The control names include:
This is the automatic teller machine (ATM) panel. After choosing an amount to withdraw and after pressing the withdraw button, a Java banknote appears and scrolls on the screen. Figure 10-8 shows this panel.
The controls include:
The applet contains two important classes: Account and NetBank. NetBank is the main class, which handles user input, while Account has specific methods to perform usual bank operations on an account. The most essential part of this example, the JDBC and SQL code, is in the Account class. The HTML File This is the HTML page that calls the applet. <html> <title>JavaBank Applet</title> <head> <h1>Welcome to JavaBank!</h1> </head> <body> <applet code=NetBank.class width=480 height=380> </applet> </body> </html> Account.java Listing 10-18 contains the source code for the Account.java class. Listing 10-18: Account.java. import java.sql.*; import java.io.*; import java.util.*; import java.net.*; public class Account { long acctNo = 0; Connection curConn; public Account(String url, String uid, String pwd) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); curConn = DriverManager.getConnection(url, uid, pwd); } catch(java.lang.Exception ex) { ex.printStackTrace(); } } protected void finalize() { try { curConn.close(); } catch (SQLException ex) { } } public boolean verifyPinCode(long checkAcctNo, String checkPinCode) { boolean rc = (0 == 1); String acctPinCode = ""; String curQueryString = "SELECT pincode FROM accounts WHERE acctno = " + checkAcctNo; try { Statement curSt = curConn.createStatement(); curSt.setQueryTimeout(60); ResultSet curRs = curSt.executeQuery(curQueryString); while (curRs.next()) { acctPinCode = curRs.getString("pincode"); } curRs.close(); curSt.close(); rc = (checkPinCode.compareTo(acctPinCode) == 0); } catch (SQLException ex) { } if (rc) { acctNo = checkAcctNo; } return rc; } public float checkAcctBalance() { float acctBalance = 0; String curQueryString = "SELECT balance FROM accounts WHERE acctno = " + acctNo; try { Statement curSt = curConn.createStatement(); curSt.setQueryTimeout(60); ResultSet curRs = curSt.executeQuery(curQueryString); while (curRs.next()) { acctBalance = curRs.getFloat("balance"); } curRs.close(); curSt.close(); } catch (SQLException ex) { } return (acctBalance); } public Vector checkHistory() { Vector acctTransactionHistory = new Vector(); String curQueryString = "SELECT tdate, typetransaction, otheracct, amount, ipaddress FROM history WHERE acctno = " + acctNo; try { Statement curSt = curConn.createStatement(); curSt.setQueryTimeout(60); ResultSet curRs = curSt.executeQuery(curQueryString); while (curRs.next()) { acctTransactionHistory.addElement(curRs.getString(1) + " " + curRs.getString(2) + " " + curRs.getString(3) + " " + curRs.getString(4) + " " + curRs.getString(5)); } curRs.close(); curSt.close(); } catch (SQLException ex) { } return (acctTransactionHistory); } public Vector checkAcctOwnerName(long checkAcctNo) { Vector acctOwner = new Vector(); String curQueryString = "SELECT name, address FROM clients WHERE ownerno = (SELECT ownerno FROM accounts WHERE acctno = " + checkAcctNo + ")"; try { Statement curSt = curConn.createStatement(); curSt.setQueryTimeout(60); ResultSet curRs = curSt.executeQuery(curQueryString); while (curRs.next()) { acctOwner.addElement(curRs.getString("name")); acctOwner.addElement(curRs.getString("address")); } curRs.close(); curSt.close(); } catch (SQLException ex) { } return (acctOwner); } public void makeTransfer(long toAcctNo, float amount) { String curUpdateString = "UPDATE accounts SET balance = balance + ? WHERE acctno = ?"; String logInsertString = "INSERT INTO history (tdate, acctno, typetransaction, otheracct, amount, ipaddress) VALUES (?, ?, ?, ?, ?, ?)"; try { curConn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZ ABLE); curConn.setAutoCommit(false); PreparedStatement curSt = curConn.prepareStatement(curUpdateString); curSt.setQueryTimeout(60); curSt.setFloat(1, -amount); curSt.setLong(2, acctNo); curSt.executeUpdate(); curSt.setFloat(1, amount); curSt.setLong(2, toAcctNo); curSt.executeUpdate(); java.util.Date toDay = new java.util.Date(); String localHost = ""; try { localHost = InetAddress.getLocalHost().toString(); } catch (UnknownHostException ex) { localHost = "localhost/127.0.0.1"; } PreparedStatement logSt = curConn.prepareStatement(logInsertString); logSt.setQueryTimeout(60); logSt.setString(1, toDay.toGMTString()); logSt.setLong(2, acctNo); logSt.setString(3, "Transfert"); logSt.setLong(4, toAcctNo); logSt.setFloat(5, -amount); logSt.setString(6, localHost); logSt.executeUpdate(); logSt.setString(1, toDay.toGMTString()); logSt.setLong(2, toAcctNo); logSt.setString(3, "Received"); logSt.setLong(4, acctNo); logSt.setFloat(5, amount); logSt.setString(6, localHost); logSt.executeUpdate(); curConn.commit(); curConn.setTransactionIsolation(Connection.TRANSACTION_NONE); curSt.close(); logSt.close(); } catch (SQLException ex) { } } public void cashWithdraw(float amount) { String curUpdateString = "UPDATE accounts SET balance = balance + ? WHERE acctno = ?"; String logInsertString = "INSERT INTO history (tdate, acctno, typetransaction, otheracct, amount, ipaddress) VALUES (?, ?, ?, ?, ?, ?)"; try { curConn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZ ABLE); curConn.setAutoCommit(false); PreparedStatement curSt = curConn.prepareStatement(curUpdateString); curSt.setQueryTimeout(60); curSt.setFloat(1, -amount); curSt.setLong(2, acctNo); curSt.executeUpdate(); java.util.Date toDay = new java.util.Date(); String localHost = ""; try { localHost = InetAddress.getLocalHost().toString(); } catch (UnknownHostException ex) { localHost = "localhost/127.0.0.1"; } PreparedStatement logSt = curConn.prepareStatement(logInsertString); logSt.setQueryTimeout(60); logSt.setString(1, toDay.toGMTString()); logSt.setLong(2, acctNo); logSt.setString(3, "Withdraw"); logSt.setLong(4, 0); logSt.setFloat(5, -amount); logSt.setString(6, localHost); logSt.executeUpdate(); curConn.commit(); curConn.setTransactionIsolation(Connection.TRANSACTION_NONE); curSt.close(); logSt.close(); } catch (SQLException ex) { } } }
|
![]() |
|