ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

ATM机案例3之数据库交互实现(一)

2021-11-07 21:01:29  阅读:135  来源: 互联网

标签:数据库 ATM Connection try connection statement close null 交互


ATM机案例3之数据库交互实现(一)

一、表格数据建立

t_account(帐户表)

image

t_bank(银行表)

image

t_bank_card(银行_银行卡关联表)

image

t_bank_account(银行_账户关联表)

image

二、DBUtils(jdbc连接工具类)

在DBUtils类中完成以下3个方法:

点击查看代码
	//将装载驱动的方法封装起来,提高代码可用性
    public static Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName("oracle.jdbc.OracleDriver");
            connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:XE", "frank", "frank");
	//getconnection方法的三个参数分别对应url,数据库用户名,数据库密码
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }
点击查看代码
	//将关闭数据库连接的方法封装起来,以便复用
    public static void close(ResultSet resultSet, Statement statement, Connection connection) {
	//分别对三个对象做判断,非空时进行关闭的操作
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
点击查看代码
	//对于只有两个对象的情况单独操作,将ResultSet对象手动赋予空值,
	//调用上面的同名close方法
    public static void close(Statement statement, Connection connection) {
        close(null, statement, connection);
    }

未完。。

标签:数据库,ATM,Connection,try,connection,statement,close,null,交互
来源: https://www.cnblogs.com/black-jay-blogs/p/15521683.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有