为了账号安全,请及时绑定邮箱和手机立即绑定

JDBC 女神走过来 3-3

标签:
Java MySQL

Model Account.java

package com.imooc.model;

import java.util.Date;

public class Account {

    private Integer id;
    private String account;
    private Double amount;
    private Date createAt;

    public Date getCreateAt() {
        return createAt;
    }

    public void setCreateAt(Date createAt) {
        this.createAt = createAt;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

}

Model TransInfos.java

package com.imooc.model;

import java.util.Date;

public class TransInfos {

    private Integer id;
    private Integer sourceId;
    private String sourceAccount;
    private Integer destinationId;
    private String destinationAccount;
    private Double amount;
    private Date createAt;

    public String getDestinationAccount() {
        return destinationAccount;
    }

    public void setDestinationAccount(String destinationAccount) {
        this.destinationAccount = destinationAccount;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getSourceId() {
        return sourceId;
    }

    public void setSourceId(Integer sourceId) {
        this.sourceId = sourceId;
    }

    public String getSourceAccount() {
        return sourceAccount;
    }

    public void setSourceAccount(String sourceAccount) {
        this.sourceAccount = sourceAccount;
    }

    public Integer getDestinationId() {
        return destinationId;
    }

    public void setDestinationId(Integer destinationId) {
        this.destinationId = destinationId;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

    public Date getCreateAt() {
        return createAt;
    }

    public void setCreateAt(Date createAt) {
        this.createAt = createAt;
    }

}

DAO AccountDao.java

package com.imooc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.imooc.db.DBUtil;
import com.imooc.model.Account;

public class AccountDao {

    public void addAccount(Account a) throws Exception{
        Connection conn = DBUtil.getConnection();
        String sql = ""+
                    "insert into account_info "+
                    " (account,amount,create_at) "+
                    " values("+
                    "?,?,current_date()) ";
        PreparedStatement ptmt = conn.prepareStatement(sql);

        ptmt.setString(1, a.getAccount());
        ptmt.setDouble(2, a.getAmount());

        ptmt.execute();
    }

    public void updateAccount(Account a) throws Exception{
        Connection conn = DBUtil.getConnection();
        String sql = ""+
                    " update account_info "+
                    " set account = ?,amount = ?"+
                    " createAt = current_date() "+
                    " where id = ? ";
        PreparedStatement ptmt = conn.prepareStatement(sql);

        ptmt.setString(1, a.getAccount());
        ptmt.setDouble(2, a.getAmount());
        ptmt.setInt(3, a.getId());

        ptmt.execute();
    }

    public void delAccount(Integer id) throws Exception{
        Connection conn = DBUtil.getConnection();
        String sql = ""+
                    " delete from account_info "+
                    " where id = ? ";
        PreparedStatement ptmt = conn.prepareStatement(sql);

        ptmt.setInt(1, id);
        ptmt.execute();
    }

    public List<Account> queryAccount(List<Map<String, Object>> params) throws Exception{
        Connection conn = DBUtil.getConnection();

        StringBuilder sb = new StringBuilder();
        sb.append("select * from account_info where 1=1");

        if(params!=null&&params.size()>0){
            for(int i = 0;i < params.size();i++){
                Map<String, Object> map = params.get(i);
                sb.append(" and "+ map.get("name") + " " + map.get("rela")+ " " + map.get("value") + " " );
            }
        }
        PreparedStatement ptmt = conn.prepareStatement(sb.toString());

        ResultSet rs = ptmt.executeQuery();

        List<Account> result = new ArrayList<Account>();
        Account a = null;

        while(rs.next()){
            a = new Account();
            a.setId(rs.getInt("id"));
            a.setAccount(rs.getString("account"));
            a.setAmount(rs.getDouble("amount"));
            a.setCreateAt(rs.getDate("create_at"));

            result.add(a);
        }
        return result;
    }
}

DAO TransInfosDao.java

package com.imooc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.imooc.db.DBUtil;
import com.imooc.model.TransInfos;

public class TransInfoDao {

    public void addTransInfos(TransInfos t) throws Exception{
        Connection conn = DBUtil.getConnection();
        String sql = ""+
                    "insert into trans_info "+
                    " (source_id,source_account,destination_id,destination_account,amount,create_at) "+
                    " values("+
                    "?,?,?,?,?,current_date()) ";
        PreparedStatement ptmt = conn.prepareStatement(sql);

        ptmt.setInt(1, t.getSourceId());
        ptmt.setString(2, t.getSourceAccount());
        ptmt.setInt(3, t.getDestinationId());
        ptmt.setString(4, t.getSourceAccount());
        ptmt.setDouble(5, t.getAmount());

        ptmt.execute();
    }

    public void updateTransInfos(TransInfos t) throws Exception{
        Connection conn = DBUtil.getConnection();
        String sql = ""+
                    " update trans_info "+
                    " set source_id = ?,source_account = ?,destination_id = ?,destination_account = ?,"+
                    " amount = ?,createAt = current_date() "+
                    " where id = ? ";
        PreparedStatement ptmt = conn.prepareStatement(sql);

        ptmt.setInt(1, t.getSourceId());
        ptmt.setString(2, t.getSourceAccount());
        ptmt.setInt(3, t.getDestinationId());
        ptmt.setString(4, t.getSourceAccount());
        ptmt.setDouble(5, t.getAmount());
        ptmt.setInt(6, t.getId());

        ptmt.execute();
    }

    public void delTransInfos(Integer id) throws Exception{
        Connection conn = DBUtil.getConnection();
        String sql = ""+
                    " delete from trans_info "+
                    " where id = ? ";
        PreparedStatement ptmt = conn.prepareStatement(sql);

        ptmt.setInt(1, id);
        ptmt.execute();
    }

    public List<TransInfos> queryTransInfos(List<Map<String, Object>> params) throws Exception{
        Connection conn = DBUtil.getConnection();

        StringBuilder sb = new StringBuilder();
        sb.append("select * from trans_info where 1=1");

        if(params!=null&&params.size()>0){
            for(int i = 0;i < params.size();i++){
                Map<String, Object> map = params.get(i);
                sb.append(" and "+ map.get("name") + " " + map.get("rela")+ " " + map.get("value") + " " );
            }
        }
        PreparedStatement ptmt = conn.prepareStatement(sb.toString());

        ResultSet rs = ptmt.executeQuery();

        List<TransInfos> result = new ArrayList<TransInfos>();
        TransInfos t = null;

        while(rs.next()){
            t = new TransInfos();
            t.setId(rs.getInt("id"));
            t.setSourceId(rs.getInt("source_id"));
            t.setSourceAccount(rs.getString("source_account"));
            t.setDestinationId(rs.getInt("destination_id"));
            t.setDestinationAccount(rs.getString("destination_account"));
            t.setAmount(rs.getDouble("amount"));
            t.setCreateAt(rs.getDate("create_at"));

            result.add(t);
        }
        return result;
    }
}
点击查看更多内容
4人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消