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

AccountFactory

public class AccountFactory extends Object

Fields Summary
private static AccountFactory
instance
private String
dbUrl
Constructors Summary
private AccountFactory(String driverClass, String dbUrl)

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

    
Methods Summary
private voidclose(java.sql.PreparedStatement ps)

        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
            }
        }
    
private voidclose(java.sql.ResultSet rs)

        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            }
        }
    
private voidclose(java.sql.Connection conn)

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    
public AccountgetAccount(java.lang.String acctNumber)

return
an account from the database, or null if the account is not found.
throws
DataSourceException if a database error occurs.

        Connection conn = null;
        try {
            conn = DriverManager.getConnection(this.dbUrl);
            return getAccount(acctNumber, conn);
        } catch (SQLException e) {
            throw new DataSourceException(e);
        } finally {
            close(conn);
        }
    
AccountgetAccount(java.lang.String acctNumber, java.sql.Connection conn)

        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);
        }
    
intgetAccountType(java.lang.String acctTypeStr)

        if ("SA".equals(acctTypeStr)) {
            return Account.SAVINGS;
        }
        if ("CH".equals(acctTypeStr)) {
            return Account.CHECKING;
        }
        throw new DataSourceException("Unknown account type: " + acctTypeStr);
    
public static synchronized com.oreilly.mock.AccountFactorygetInstance()

        if (instance == null) {
            instance = new AccountFactory("sun.jdbc.odbc.JdbcOdbcDriver",
                    "jdbc:odbc:banking");
        }
        return instance;
    
public static voidmain(java.lang.String[] args)

        AccountFactory acctFactory = AccountFactory.getInstance();
        Account acct = acctFactory.getAccount("0001");

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