提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.mybatis.core.util; |
H |
2 |
|
d9f9ba
|
3 |
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource; |
e7c126
|
4 |
import com.baomidou.mybatisplus.annotation.DbType; |
d9f9ba
|
5 |
import com.iailab.framework.common.util.spring.SpringUtils; |
H |
6 |
import com.iailab.framework.mybatis.core.enums.DbTypeEnum; |
e7c126
|
7 |
|
d9f9ba
|
8 |
import javax.sql.DataSource; |
e7c126
|
9 |
import java.sql.Connection; |
H |
10 |
import java.sql.DriverManager; |
d9f9ba
|
11 |
import java.sql.SQLException; |
e7c126
|
12 |
|
H |
13 |
/** |
|
14 |
* JDBC 工具类 |
|
15 |
* |
4d4165
|
16 |
* @author iailab |
e7c126
|
17 |
*/ |
H |
18 |
public class JdbcUtils { |
|
19 |
|
|
20 |
/** |
|
21 |
* 判断连接是否正确 |
|
22 |
* |
|
23 |
* @param url 数据源连接 |
|
24 |
* @param username 账号 |
|
25 |
* @param password 密码 |
|
26 |
* @return 是否正确 |
|
27 |
*/ |
|
28 |
public static boolean isConnectionOK(String url, String username, String password) { |
|
29 |
try (Connection ignored = DriverManager.getConnection(url, username, password)) { |
|
30 |
return true; |
|
31 |
} catch (Exception ex) { |
|
32 |
return false; |
|
33 |
} |
|
34 |
} |
|
35 |
|
|
36 |
/** |
|
37 |
* 获得 URL 对应的 DB 类型 |
|
38 |
* |
|
39 |
* @param url URL |
|
40 |
* @return DB 类型 |
|
41 |
*/ |
|
42 |
public static DbType getDbType(String url) { |
d9f9ba
|
43 |
return com.baomidou.mybatisplus.extension.toolkit.JdbcUtils.getDbType(url); |
H |
44 |
} |
|
45 |
|
|
46 |
/** |
|
47 |
* 通过当前数据库连接获得对应的 DB 类型 |
|
48 |
* |
|
49 |
* @return DB 类型 |
|
50 |
*/ |
|
51 |
public static DbType getDbType() { |
|
52 |
DynamicRoutingDataSource dynamicRoutingDataSource = SpringUtils.getBean(DynamicRoutingDataSource.class); |
|
53 |
DataSource dataSource = dynamicRoutingDataSource.determineDataSource(); |
|
54 |
try (Connection conn = dataSource.getConnection()) { |
|
55 |
return DbTypeEnum.find(conn.getMetaData().getDatabaseProductName()); |
|
56 |
} catch (SQLException e) { |
|
57 |
throw new IllegalArgumentException(e.getMessage()); |
|
58 |
} |
e7c126
|
59 |
} |
H |
60 |
|
|
61 |
} |