FileDocCategorySizeDatePackage
AccountFactory.javaAPI DocExample3781Mon Nov 18 20:09:16 GMT 2002com.oreilly.mock

AccountFactory.java

package com.oreilly.mock;

import java.sql.*;
import java.io.PrintWriter;

public class AccountFactory {
    private static AccountFactory instance;
    private String dbUrl;

    public static synchronized AccountFactory getInstance()
            throws DataSourceException {
        if (instance == null) {
            instance = new AccountFactory("sun.jdbc.odbc.JdbcOdbcDriver",
                    "jdbc:odbc:banking");
        }
        return instance;
    }

    public static void main(String[] args) throws Exception {
        AccountFactory acctFactory = AccountFactory.getInstance();
        Account acct = acctFactory.getAccount("0001");

        PrintWriter pw = new PrintWriter(System.out);
        acct.writeCsv(pw);
        pw.flush();
    }

    /**
     * @return an account from the database, or null if the account
     *         is not found.
     * @throws DataSourceException if a database error occurs.
     */
    public Account getAccount(String acctNumber) throws DataSourceException {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(this.dbUrl);
            return getAccount(acctNumber, conn);
        } catch (SQLException e) {
            throw new DataSourceException(e);
        } finally {
            close(conn);
        }
    }

    // a package-scope method that makes it easier for a unit test
    // to pass in a mock connection
    Account getAccount(String acctNumber, Connection conn)
            throws SQLException, DataSourceException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(
                    "SELECT balance, acctType " +
                    "FROM Accounts " +
                    "WHERE acctNumber = ?");
            ps.setString(1, acctNumber);

            Account acct = null;
            rs = ps.executeQuery();
            if (rs.next()) {
                double balance = rs.getDouble("balance");
                String acctTypeStr = rs.getString("acctType");
                int acctType = getAccountType(acctTypeStr);
                acct = new Account(acctType, acctNumber, balance);
            }
            return acct;
        } finally {
            close(rs);
            close(ps);
        }
    }

    private void close(PreparedStatement ps) {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
            }
        }
    }

    private void close(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            }
        }
    }

    private void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }

    int getAccountType(String acctTypeStr)
            throws SQLException, DataSourceException {
        if ("SA".equals(acctTypeStr)) {
            return Account.SAVINGS;
        }
        if ("CH".equals(acctTypeStr)) {
            return Account.CHECKING;
        }
        throw new DataSourceException("Unknown account type: " + acctTypeStr);
    }

    // package-scope constructor that is used for unit testing. This
    // constructor does not load a database driver.
    AccountFactory() {
    }

    private AccountFactory(String driverClass, String dbUrl)
            throws DataSourceException {
        try {
            Class.forName(driverClass);
            this.dbUrl = dbUrl;
        } catch (ClassNotFoundException e) {
            throw new DataSourceException(e);
        }
    }
}