package com.iailab.framework.mybatis.core.util;
|
|
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
import com.baomidou.mybatisplus.annotation.DbType;
|
import com.iailab.framework.common.util.spring.SpringUtils;
|
import com.iailab.framework.mybatis.core.enums.DbTypeEnum;
|
|
import javax.sql.DataSource;
|
import java.sql.Connection;
|
import java.sql.DriverManager;
|
import java.sql.SQLException;
|
|
/**
|
* JDBC 工具类
|
*
|
* @author 芋道源码
|
*/
|
public class JdbcUtils {
|
|
/**
|
* 判断连接是否正确
|
*
|
* @param url 数据源连接
|
* @param username 账号
|
* @param password 密码
|
* @return 是否正确
|
*/
|
public static boolean isConnectionOK(String url, String username, String password) {
|
try (Connection ignored = DriverManager.getConnection(url, username, password)) {
|
return true;
|
} catch (Exception ex) {
|
return false;
|
}
|
}
|
|
/**
|
* 获得 URL 对应的 DB 类型
|
*
|
* @param url URL
|
* @return DB 类型
|
*/
|
public static DbType getDbType(String url) {
|
return com.baomidou.mybatisplus.extension.toolkit.JdbcUtils.getDbType(url);
|
}
|
|
/**
|
* 通过当前数据库连接获得对应的 DB 类型
|
*
|
* @return DB 类型
|
*/
|
public static DbType getDbType() {
|
DynamicRoutingDataSource dynamicRoutingDataSource = SpringUtils.getBean(DynamicRoutingDataSource.class);
|
DataSource dataSource = dynamicRoutingDataSource.determineDataSource();
|
try (Connection conn = dataSource.getConnection()) {
|
return DbTypeEnum.find(conn.getMetaData().getDatabaseProductName());
|
} catch (SQLException e) {
|
throw new IllegalArgumentException(e.getMessage());
|
}
|
}
|
|
}
|