1  import java.sql.Connection;
  2  import java.sql.ResultSet;
  3  import java.sql.PreparedStatement;
  4  import java.sql.SQLException;
  5  
  6  /**
  7     A bank consisting of multiple bank accounts.
  8  */
  9  public class Bank
 10  {
 11     /**
 12        Finds a customer with a given number and PIN.
 13        @param customerNumber the customer number
 14        @param pin the personal identification number
 15        @return the matching customer, or null if none found
 16     */
 17     public Customer findCustomer(int customerNumber, int pin)
 18           throws SQLException
 19     {  
 20        Connection conn = SimpleDataSource.getConnection();
 21        try
 22        {
 23           Customer c = null;
 24           PreparedStatement stat = conn.prepareStatement(
 25                 "SELECT * FROM BankCustomer WHERE Customer_Number = ?");
 26           stat.setInt(1, customerNumber);
 27           
 28           ResultSet result = stat.executeQuery();
 29           if (result.next() && pin == result.getInt("PIN")) 
 30           {
 31              c = new Customer(customerNumber,
 32                    result.getInt("Checking_Account_Number"),
 33                    result.getInt("Savings_Account_Number"));
 34           }
 35           return c;
 36        }
 37        finally
 38        {
 39           conn.close();
 40        }
 41     }      
 42  }
 43  
 44