Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click here for Oracle 8i Internet Seminars.
Click here for Oracle 8i Internet Seminars.
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


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.


Figure 10-5:  JavaBank applet login screen.

The names of the main controls are:

  welcomeAccountNo—a text field to get the account number
  welcomePinCode—a text field to get the personal identification number
  welcomeOkButton—a button to log into the database
  welcomeEndButton—a button to close the database connection and quit the application
  statusBar—a noneditable text field to display various messages

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 customer’s account balance and transaction history are displayed, as shown in Figure 10-6.


Figure 10-6:  JavaBank applet account balance.

These are the controls:

  balanceHistory—a scrollable text list to display the transaction history for the current account
  balanceBalance—a noneditable text field to display the balance of this account
  balanceRefreshButton—a button to refresh the history log
  balanceEndButton—a button to exit the session
  statusBar—a noneditable text field to display various messages

The client’s 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.


Figure 10-7:  JavaBank applet transfer screen.

The control names include:

  transferAccountNo—a text field for the recipient’s account number
  transferAmount—a text field for the amount of money to transfer
  transferYesButton—a button to commit the transfer
  transferEndButton—a button to exit the session
  statusBar—a noneditable text field to display various messages

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.


Figure 10-8:  JavaBank applet cash withdrawal.

The controls include:

  withdrawXX, where XX is one of 20, 100, 200, 1,000—radio buttons used to select the amount of money to withdraw
  withdrawImage—a picture of a banknote that displays and scrolls when the withdrawal is committed
  withdrawWithdrawButton—a button to commit the withdrawal
  withdrawEndButton—a button to exit the session
  statusBar—a noneditable text field to display various messages

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) { }
  }
}


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.