3.3.4.5 包: com.atguigu.dataanalasis.util

1. 类: JDBCUtil

package com.atguigu.dataanalasis.util;

import java.sql.*;

public class JDBCUtil {
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URI = "jdbc:mysql://hadoop201:3306/db_telecom?useUnicode=true&characterEncoding=UTF8";
    private static final String USER_NAME = "root";
    private static final String PASSWORD = "aaa";

    private static Connection connection;

    /**
     * 单例模式
     * @return
     */
    public static Connection getInstance() {
        if (connection == null) {
            synchronized (JDBCUtil.class) {
                if (connection == null) {
                    connection = getConnection();
                }
            }
        }
        return connection;
    }

    /**
     * 获取 JDBC 连接
     *
     * @return
     */
    private static Connection getConnection() {
        try {
            Class.forName(DRIVER);
            return DriverManager.getConnection(URI, USER_NAME, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 关闭资源
     *
     * @param resultSet
     * @param preparedStatement
     * @param connection
     */
    public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

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

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

}

2. 类: LRUCache

缓存类, 提高查找速度

package com.atguigu.dataanalasis.util;

import java.util.LinkedHashMap;
import java.util.Map;

public class LRUCache extends LinkedHashMap<String, Integer> {
    private static final long serialVersionUID = 1L;
    protected int maxElements;

    public LRUCache(int maxSize) {
        super(maxSize, 0.75F, true);
        this.maxElements = maxSize;
    }

    protected boolean removeEldestEntry(Map.Entry<String, Integer> eldest) {
        return this.size() > this.maxElements;
    }
}
Copyright © 尚硅谷大数据 2019 all right reserved,powered by Gitbook
该文件最后修订时间: 2018-11-27 16:04:01

results matching ""

    No results matching ""