利用三层架构的原理优化JDBC连接MySql

原理图:

 

优化的原因:

        对其连接数据库,只需要连接一次即可,不需要重复连接多次

解决方法:

        利用设计模式中的单例模式,解决只加载一次驱动的问题

 

 

        将配置信息写入配置文件,利用properties类读取即可

        

单例模式:

        1.私有化构造函数

        2.创建一个方法,获取当前类的对象

        3.创建一个方法,加载配置文件中的信息

 

 

 

         4.当前类继承Properties类

                 [以便调用properties 类中的 getProperty()方法]

.

        

 

package com.qf.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @ClassName Env
 * @Description TODO
 * @Author 好名字
 * @Date 2022/7/23 16:25
 * @Version 1.0
 **/

public class Env extends Properties {

    static Env env = null;

    private Env (){
        loadD();
    }

    public static synchronized Env getInstance(){
        if (env == null){
            env = new Env();
        }
        return env;
    }

    private void loadD(){

//        System.out.println(this.getClass().getClassLoader());

        InputStream is = this.getClass().getClassLoader().getResourceAsStream("db.properties");
        try {
            //调用Properties工具的load()方法加载文件流
            load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

 

package com.qf.utils;

import java.sql.*;

/**
 * @ClassName DaoUtil
 * @Description TODO
 * @Author 好名字
 * @Date 2022/7/23 16:18
 * @Version 1.0
 **/

public class DaoUtil {

    static final String URL = Env.getInstance().getProperty("jdbc.url");
    static final String USERNAME = Env.getInstance().getProperty("jdbc.username");
    static final String PASSWORD = Env.getInstance().getProperty("jdbc.password");
    static final String DRIVER = Env.getInstance().getProperty("jdbc.Driver");

    protected Connection connection = null;
    protected Statement statement = null;
    protected PreparedStatement preparedStatement = null;
    protected ResultSet resultSet = null;

    static {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void getConnection(){
        try {
            connection = DriverManager.getConnection(URL , USERNAME , PASSWORD);

            /**
             * 获取配置文件中的信息
             */
            System.out.println( "URL:" +  Env.getInstance().getProperty("jdbc.url") + "\n"
                    +"username:" + Env.getInstance().getProperty("jdbc.username") + "\n"
                    + "password:" + Env.getInstance().getProperty("jdbc.password") + "\n"
                    + "Driver:" + Env.getInstance().getProperty("jdbc.Driver"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void  closeAll(){
        try {
            if (resultSet != null) resultSet.close();
            if (statement != null) statement.close();
            if (preparedStatement != null) preparedStatement.close();
            if (connection != null) connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值