`
小鑫的java
  • 浏览: 143179 次
  • 性别: Icon_minigender_1
  • 来自: 浙江
社区版块
存档分类
最新评论

JDBC_银行代码

阅读更多
//1
public interface IAccountService {
    /*增加账户,删除账户,查询余额,存款,取款,转帐*/
    public void createAccount(Account act);
    public void removeAccount(String actNo);
    public double getBal(String actNo);
    public void deposite(String actNo,double amount);
    public void withdraw(String actNo,double amount);
    public void transfer(String from ,String to, double amount);
}
//2
public class Account {
    private String actNo;
    private double bal;
    public void deposite(double amount){
        bal = bal + amount;
    }
    public void withdraw(double amount){
        bal = bal - amount;
        
    }
    public Account(String actNo, double bal) {
        super();
        this.actNo = actNo;
        this.bal = bal;
    }
    public String getActNo() {
        return actNo;
    }
    public void setActNo(String actNo) {
        this.actNo = actNo;
    }
    public double getBal() {
        return bal;
    }
    public void setBal(double bal) {
        this.bal = bal;
    }
}
//3
public interface IAccountDAO {
     public void insert(Account act,Connection con) throws DataException;
     public void del(String actNo,Connection con) throws DataException;
     public void update(Account act, Connection con) throws DataException;
     public Account findAccountByActNo(String actNo,Connection con) throws DataException;
}
//4
public class DataException extends Exception {

    public DataException() {
        // TODO Auto-generated constructor stub
    }

    public DataException(String message) {
        super(message);
        // TODO Auto-generated constructor stub
    }

    public DataException(Throwable cause) {
        super(cause);
        // TODO Auto-generated constructor stub
    }

    public DataException(String message, Throwable cause) {
        super(message, cause);
        // TODO Auto-generated constructor stub
    }

}
//5
public class AccountServiceJdbcImpl implements IAccountService {
    private IAccountDAO dao = AccountDAOFactory.getDAO();
    public void createAccount(Account act) {
        Connection con = null;
        try {
            con = JdbcUtil.getConnection();
            con.setAutoCommit(false);
            dao.insert(act, con);
            con.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e1) {
                 e1.printStackTrace();
            }
        }finally{
            JdbcUtil.release(con);
        }

    }

    public void deposite(String actNo, double amount) {
        Connection con = null;
        try {
            con = JdbcUtil.getConnection();
            con.setAutoCommit(false);
            Account act = dao.findAccountByActNo(actNo, con);
            act.deposite(amount);
            dao.update(act, con);
            con.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e1) {
                 e1.printStackTrace();
            }
        }finally{
            JdbcUtil.release(con);
        }


    }

    public double getBal(String actNo) {
        Connection con = null;
        double bal = 0.0;
        try {
            con = JdbcUtil.getConnection();
            con.setAutoCommit(false);
            Account act = dao.findAccountByActNo(actNo, con);
            bal = act.getBal();
            con.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e1) {
                 e1.printStackTrace();
            }
        }finally{
            JdbcUtil.release(con);
        }

        return bal;
    }

    public void removeAccount(String actNo) {
        Connection con = null;
        try {
            con = JdbcUtil.getConnection();
            con.setAutoCommit(false);
            dao.del(actNo, con);
            con.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e1) {
                 e1.printStackTrace();
            }
        }finally{
            JdbcUtil.release(con);
        }


    }

    public void transfer(String from, String to, double amount) {
        Connection con = null;
        try {
            con = JdbcUtil.getConnection();
            con.setAutoCommit(false);
            Account a1 = dao.findAccountByActNo(from, con);
            Account a2 = dao.findAccountByActNo(to, con);
            a1.withdraw(amount);
            a2.deposite(amount);
            dao.update(a1, con);
            dao.update(a2, con);
            con.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e1) {
                 e1.printStackTrace();
            }
        }finally{
            JdbcUtil.release(con);
        }


    }

    public void withdraw(String actNo, double amount) {
        Connection con = null;
        try {
            con = JdbcUtil.getConnection();
            con.setAutoCommit(false);
            Account act = dao.findAccountByActNo(actNo, con);
            act.withdraw(amount);
            dao.update(act, con);
            con.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e1) {
                 e1.printStackTrace();
            }
        }finally{
            JdbcUtil.release(con);
        }


    }

}
//6

public class AccountDAOJdbcImpl implements IAccountDAO {

    public void del(String actNo, Connection con) throws DataException {
        String sql = "delete from t_act where actNo=?";
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, actNo);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            throw  new DataException("del error");
        }finally{
            JdbcUtil.release(ps);
        }

    }

    public Account findAccountByActNo(String actNo, Connection con)
            throws DataException {
        String sql = "select actNo,bal from t_act" +
                " where actNo=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        Account act = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, actNo);
            rs = ps.executeQuery();
            if(rs.next()){
                act = 
                   new Account(rs.getString(1),rs.getDouble(2));            
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
            throw  new DataException("find error");
        }finally{
            JdbcUtil.release(rs,ps,null);
        }
        return act;
    }

    public void insert(Account act, Connection con) throws DataException {
        String sql = "insert into t_act values(?,?)";
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, act.getActNo());
            ps.setDouble(2, act.getBal());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            throw  new DataException("insert error");
        }finally{
            JdbcUtil.release(ps);
        }

    }

    public void update(Account act, Connection con) throws DataException {
        String sql = "update t_act set actNo=?,bal=? " +
                "where actNo=?";
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, act.getActNo());
            ps.setDouble(2, act.getBal());
            ps.setString(3,act.getActNo());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            throw  new DataException("update error");
        }finally{
            JdbcUtil.release(ps);
        }

    }

}
//7
public class AccountDAOFactory {
    public static IAccountDAO getDAO(){
        return new AccountDAOJdbcImpl();
    }
}
//8
public class AccountServiceFactory {
    public static IAccountService getService(){
        return new AccountServiceJdbcImpl();
    }
}
//9
public class Test {
    public static void main(String[] args) {
        IAccountService s = AccountServiceFactory.getService();
        s.createAccount(new Account("a-001",12000.0));
        s.createAccount(new Account("a-002",7000.0));
        StringBuffer sb = new StringBuffer();
        
        sb.append("a-001="+s.getBal("a-001")+"\n");
        sb.append("a-002="+s.getBal("a-002")+"\n");
        sb.append("从a-001转756.0到a-002\n");
        s.transfer("a-001", "a-002", 756.0);
        sb.append("a-001="+s.getBal("a-001")+"\n");
        sb.append("a-002="+s.getBal("a-002")+"\n");
        
        System.out.print(sb.toString());    
        
    }

}
//10
JdbcUtil工具类

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics