Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ator-gui into MichaelJY91-master
  • Loading branch information
zouzg committed Jun 20, 2018
2 parents 6b9fa12 + 431f51a commit 71f60b8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zzg.mybatis.generator.controller;

import com.zzg.mybatis.generator.exception.DbDriverLoadingException;
import com.zzg.mybatis.generator.model.DatabaseConfig;
import com.zzg.mybatis.generator.util.ConfigHelper;
import com.zzg.mybatis.generator.util.DbUtil;
Expand Down Expand Up @@ -68,6 +69,9 @@ void testConnection() {
try {
DbUtil.getConnection(config);
AlertUtil.showInfoAlert("连接成功");
} catch (DbDriverLoadingException e){
_LOG.error("{}", e);
AlertUtil.showWarnAlert("连接失败, "+e.getMessage());
} catch (Exception e) {
_LOG.error(e.getMessage(), e);
AlertUtil.showWarnAlert("连接失败");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.zzg.mybatis.generator.exception;

/**
* 数据库驱动加载异常
* @Date 2017/8/15 21:46
* @Author jy
*/
public class DbDriverLoadingException extends RuntimeException{

public DbDriverLoadingException(String message){
super(message);
}
}
44 changes: 25 additions & 19 deletions src/main/java/com/zzg/mybatis/generator/util/DbUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zzg.mybatis.generator.util;

import com.zzg.mybatis.generator.exception.DbDriverLoadingException;
import com.zzg.mybatis.generator.model.DatabaseConfig;
import com.zzg.mybatis.generator.model.DbType;
import com.zzg.mybatis.generator.model.UITableColumnVO;
Expand All @@ -18,34 +19,22 @@ public class DbUtil {
private static final Logger _LOG = LoggerFactory.getLogger(DbUtil.class);
private static final int DB_CONNECTION_TIMEOUTS_SECONDS = 1;

private static Map<DbType, Driver> drivers;
private static Map<DbType, Driver> drivers = new HashMap<>();

static {
drivers = new HashMap<>();
List<String> driverJars = ConfigHelper.getAllJDBCDriverJarPaths();
ClassLoader classloader = ClassloaderUtility.getCustomClassloader(driverJars);
DbType[] dbTypes = DbType.values();
for (DbType dbType : dbTypes) {
try {
Class clazz = Class.forName(dbType.getDriverClass(), true, classloader);
Driver driver = (Driver) clazz.newInstance();
_LOG.info("load driver class: {}", driver);
drivers.put(dbType, driver);
} catch (Exception e) {
_LOG.error("load driver error");
}
public static Connection getConnection(DatabaseConfig config) throws ClassNotFoundException, SQLException {
DbType dbType = DbType.valueOf(config.getDbType());
if (drivers.get(dbType) == null){
loadDbDriver(dbType);
}
}

public static Connection getConnection(DatabaseConfig config) throws ClassNotFoundException, SQLException {
String url = getConnectionUrlWithSchema(config);
String url = getConnectionUrlWithSchema(config);
Properties props = new Properties();

props.setProperty("user", config.getUsername()); //$NON-NLS-1$
props.setProperty("password", config.getPassword()); //$NON-NLS-1$

DriverManager.setLoginTimeout(DB_CONNECTION_TIMEOUTS_SECONDS);
Connection connection = drivers.get(DbType.valueOf(config.getDbType())).connect(url, props);
Connection connection = drivers.get(dbType).connect(url, props);
_LOG.info("getConnection, connection url: {}", connection);
return connection;
}
Expand Down Expand Up @@ -116,4 +105,21 @@ public static String getConnectionUrlWithSchema(DatabaseConfig dbConfig) throws
return connectionUrl;
}

/**
* 加载数据库驱动
* @param dbType 数据库类型
*/
private static void loadDbDriver(DbType dbType){
List<String> driverJars = ConfigHelper.getAllJDBCDriverJarPaths();
ClassLoader classloader = ClassloaderUtility.getCustomClassloader(driverJars);
try {
Class clazz = Class.forName(dbType.getDriverClass(), true, classloader);
Driver driver = (Driver) clazz.newInstance();
_LOG.info("load driver class: {}", driver);
drivers.put(dbType, driver);
} catch (Exception e) {
_LOG.error("load driver error", e);
throw new DbDriverLoadingException("找不到"+dbType.getConnectorJarFile()+"驱动");
}
}
}

0 comments on commit 71f60b8

Please sign in to comment.