1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| package com.xiaoguan.bank.mvc;
import com.xiaoguan.bank.utils.DBUtil;
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List;
public class AccountDao {
public AccountDao() { }
public int insert(Account act){ Connection conn=null; PreparedStatement ps=null; int count=0; try { conn = DBUtil.getConnection(); String sql="insert into t_act(actno,balance) values(?,?)"; ps=conn.prepareStatement(sql); ps.setString(1,act.getActno()); ps.setDouble(2,act.getBalance()); count=ps.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); }finally { DBUtil.close(null,ps,null); } return count; } public int deleteById(Long id){ Connection conn=null; PreparedStatement ps=null; int count=0; try { conn = DBUtil.getConnection(); String sql="delete from t_act where id=?"; ps=conn.prepareStatement(sql); ps.setLong(1,id); count=ps.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); }finally { DBUtil.close(null,ps,null); } return count; } public int update(Account account){ Connection conn=null; PreparedStatement ps=null; int count=0; try { conn = DBUtil.getConnection(); String sql="update t_act set balance=?,actno=? where id=?"; ps=conn.prepareStatement(sql); ps.setDouble(1,account.getBalance()); ps.setString(2,account.getActno()); ps.setLong(3,account.getId()); count=ps.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); }finally { DBUtil.close(null,ps,null); } return count; } public Account selectByActno(String actno){ Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; Account account=new Account(); try { conn = DBUtil.getConnection(); String sql="select id,balance from t_act where actno=?"; ps=conn.prepareStatement(sql); ps.setString(1,actno); rs=ps.executeQuery(); if(rs.next()){ Long id= rs.getLong("id"); Double balance=rs.getDouble("balance"); account.setActno(actno); account.setId(id); account.setBalance(balance); }
} catch (SQLException e) { throw new RuntimeException(e); }finally { DBUtil.close(null,ps,rs); } return account; } public List<Account> selectAll(){ Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; List<Account> accounts=new ArrayList<>(); try { conn = DBUtil.getConnection(); String sql="select id,balance,actno from t_act "; ps=conn.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()){ Long id= rs.getLong("id"); Double balance=rs.getDouble("balance"); String actno=rs.getString("actno"); accounts.add(new Account(id,actno,balance)); }
} catch (SQLException e) { throw new RuntimeException(e); }finally { DBUtil.close(null,ps,rs); } return accounts; } }
|