All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.xiaoyuge5201.util.ConnectUtil Maven / Gradle / Ivy

Go to download

数据库连接工具,查询mysql、oracle、sqlserver、postgresql的数据表以及字段信息;同时支持导出数据库设计文档

The newest version!
package com.github.xiaoyuge5201.util;

import com.github.xiaoyuge5201.config.DatabaseDriverEnum;
import com.github.xiaoyuge5201.config.MyDataSourceProperties;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 數據庫连接工具类
 *
 * @author xiaoyuge
 */
public class ConnectUtil {
    /**
     * 判断是否连接成功
     *
     * @param driver   驱动
     * @param url      ip+端口
     * @param username 用户名
     * @param pwd      密码
     * @param name     数据库名称
     * @return true/false
     */
    public static boolean testConnection(String driver, String url, String username, String pwd, String name) throws SQLException {
        Connection connections = getConnection(driver, url, username, pwd, name);
        if (connections != null) {
            connections.close();
            return true;
        }
        return false;
    }

    /**
     * 获取数据库地址
     *
     * @param driver 驱动
     * @param url    ip+端口
     * @param dbname 数据库名称
     * @return 数据库地址
     */
    public static String getUrl(String driver, String url, String dbname) {
        String urlStr = null;
        if (url.contains("jdbc:")) {
            return url;
        } else {
            if (DatabaseDriverEnum.MYSQL.getDriver().equals(driver)) {
                urlStr = "jdbc:mysql://" + url + "/" + dbname + "?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf8";
            } else if (DatabaseDriverEnum.POSTGRE_SQL.getDriver().equals(driver)) {
                urlStr = "jdbc:postgresql://" + url + "/" + dbname + "?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf8";
            } else if (DatabaseDriverEnum.ORACLE.getDriver().equals(driver)) {
                urlStr = "jdbc:oracle:thin:@//" + url + "/" + dbname + "?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf8";
            } else if (DatabaseDriverEnum.SQL_SERVER.getDriver().equals(driver)) {
                urlStr = "jdbc:sqlserver://" + url + ";DatabaseName=" + dbname + "?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf8";
            }
        }
        return urlStr;
    }

    /**
     * 根据数据库类型获取驱动类型
     *
     * @param dbType 数据库类型
     * @return 驱动类型
     */
    public static String getDriverClassName(Integer dbType) {
        return DatabaseDriverEnum.getValue(dbType);
    }

    /**
     * 获得数据库连接
     *
     * @param driver   驱动
     * @param url      地址
     * @param username 用户名
     * @param password 密码
     * @param dbname   数据库
     * @return 结果
     */
    public static Connection getConnection(String driver, String url, String username, String password, String dbname) {
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(getUrl(driver, url, dbname), username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 获取数据库连接
     *
     * @param properties 数据库连接属性
     * @return 连接对象
     */
    public static Connection getConnection(MyDataSourceProperties properties) {
        Connection conn = null;
        try {
            Class.forName(properties.getDriverClassName());
            conn = DriverManager.getConnection(properties.getUrl(), properties.getUsername(), properties.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy