使用Jdbc实现简单的ATM系统

本文档介绍了一个采用三层架构和Jdbc技术实现的ATM系统,包括存钱、取钱、转账、查看余额和修改密码等功能。系统配置了db.properties文件,使用DbUtils工具类管理数据库连接和事务。在设计上,有Account实体类、AccountDao接口及其实现类AccountImpl,以及业务层的AccountService接口和服务实现类AccountServiceImpl,重点讲述了转账功能中事务处理的实现,确保操作的原子性和一致性。最后通过测试类验证了系统的正常运行。
摘要由CSDN通过智能技术生成

本系统采用了三层架构以及Jdbc的技术简单实现了存钱、取钱、转账、查看余额、以及修改密码等功能
1.准备mysql的jar包,进行导入以及一个db.properties配置文件
2.工具类DbUtils类:包装了获取连接、增删改、以及对事务操作的一些方法

/**
 * @description:
 * @author: yrm
 * @create: 2020-08-22 08:30
 **/

public class DbUtils {
   
    private static String driver;
    private static String url;
    private static String user;
    private static String password;
    //创建一个线程局部变量
    private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();
    //1.注册驱动
    static {
   
        Properties properties = new Properties();
        InputStream is = DbUtils.class.getClassLoader().getResourceAsStream("db.properties");
        try {
   
            properties.load(is);
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");

            Class.forName(driver);
        } catch (IOException | ClassNotFoundException e) {
   
            System.out.println("注册失败!");
        }
    }
    //2.获取连接
    public static Connection getConnection() {
   
        try {
   
            //判断线程中有没有连接
            Connection conn = threadLocal.get();
            if (conn == null) {
   
                conn = DriverManager.getConnection(url, user, password);
                //绑定连接
                threadLocal.set(conn);
            }
            return conn;
        } catch (SQLException e) {
   
            e.printStackTrace();
        }
        return null;
    }
    //3.释放资源
    public static void closeAll(ResultSet rs, Statement st, Connection conn) {
   
        try {
   
            if (rs != null) {
   
                rs.close();
            }
            if (st != null) {
   
                st.close();
            }
            if (conn != null) {
   
                //是否开启事务... 如果没开启事务  则进行销毁连接
                if (conn.getAutoCommit()) {
   
                    threadLocal.remove();
                    conn.close();
                }
            }
        } catch (SQLException e) {
   
            e.printStackTrace();
        }
    }
    //4.执行命令
    public static int executeUpdate(String sql,Object... params) {
   
        Connection conn = null;
        PreparedStatement ps = null;
        try {
   
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
   
                ps.setObject(i + 1, params[i]);
            }
            return ps.executeUpdate();
        } catch (SQLException e) {
   
            e.printStackTrace();
        } finally {
   
            DbUtils.closeAll(null,ps,conn);
        }
        return -1;
    }

    //1.开启事务
    public static void beginAutoCommit() {
   
        Connection conn = getConnection();
        if (conn != null) {
   
            try {
   
                conn.setAutoCommit(
  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值