DynamicDataBaseUtil.java 3.61 KB
Newer Older
谢恒's avatar
谢恒 committed
1 2
package com.hs.admin.util;

谢恒's avatar
谢恒 committed
3 4
import com.hs.admin.base.CodeMessageEnum;
import com.hs.admin.base.HsRuntimeException;
谢恒's avatar
谢恒 committed
5
import com.hs.admin.bean.DataBase;
谢恒's avatar
谢恒 committed
6 7
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.mysql.cj.jdbc.MysqlDataSource;
谢恒's avatar
谢恒 committed
8
import com.zaxxer.hikari.HikariDataSource;
谢恒's avatar
谢恒 committed
9 10 11 12
import org.springframework.util.ObjectUtils;

import java.sql.Connection;
import java.sql.SQLException;
谢恒's avatar
谢恒 committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26

public class DynamicDataBaseUtil {
    /**
     * @description: 配置数据库
     * @return: HikariDataSource
     * @author: XieHeng
     * @date: 2021/5/7 5:02 下午
     */
    public static HikariDataSource configure(DataBase dataBase) {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(dataBase.getUrl());
        dataSource.setUsername(dataBase.getUsername());
        dataSource.setPassword(dataBase.getPassword());
        dataSource.setDriverClassName(dataBase.getDbDriver());
谢恒's avatar
谢恒 committed
27 28
//        dataSource.setMaximumPoolSize(2);
//        dataSource.setMinimumIdle(1);
谢恒's avatar
谢恒 committed
29 30 31 32 33 34 35
        dataSource.setAutoCommit(true);
//        dataSource.setConnectionTestQuery("SELECT 1 FROM DUAL");
        dataSource.addDataSourceProperty("cachePrepStmts", "true");
        dataSource.addDataSourceProperty("prepStmtCacheSize", "250");
        dataSource.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        return dataSource;
    }
谢恒's avatar
谢恒 committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

    public static boolean checkDataBaseConnection(DataBase dataBase) {
        String dbType = dataBase.getDbType();
        String username = dataBase.getUsername();
        String password = dataBase.getPassword();
        String host = dataBase.getHost();
        String port = dataBase.getPort();
        String dbName = dataBase.getName();
        if (ObjectUtils.isEmpty(username) || ObjectUtils.isEmpty(password) || ObjectUtils.isEmpty(host) || ObjectUtils.isEmpty(port) || ObjectUtils.isEmpty(dbType) || ObjectUtils.isEmpty(dbName)) {
            throw new HsRuntimeException(CodeMessageEnum.REQUEST_ERROR.getCode(), "数据库连接失败");
        }
        if ("1".equals(dbType)) {
            SQLServerDataSource ds = new SQLServerDataSource();
            ds.setUser(username);
            ds.setPassword(password);
            ds.setServerName(host);
            ds.setPortNumber(Integer.parseInt(port));
            ds.setDatabaseName(dbName);
            ds.setLoginTimeout(1);
            Connection connection = null;
            try {
                connection = ds.getConnection();
                return true;
            } catch (Exception e) {
                return false;
            } finally {
                try {
                    if (connection != null) {
                        connection.close();
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        } else if ("2".equals(dbType)) {
            MysqlDataSource ds = new MysqlDataSource();
            ds.setUser(username);
            ds.setPassword(password);
            ds.setServerName(host);
            ds.setPortNumber(Integer.parseInt(port));
            ds.setDatabaseName(dbName);
            Connection connection = null;
            try {
                connection = ds.getConnection();
                return true;
            } catch (Exception e) {
                return false;
            } finally {
                try {
                    if (connection != null) {
                        connection.close();
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return false;

    }
谢恒's avatar
谢恒 committed
96
}