diff --git a/src/main/java/com/zzg/mybatis/generator/controller/DbConnectionController.java b/src/main/java/com/zzg/mybatis/generator/controller/DbConnectionController.java index cdd5f38a..18331bd9 100644 --- a/src/main/java/com/zzg/mybatis/generator/controller/DbConnectionController.java +++ b/src/main/java/com/zzg/mybatis/generator/controller/DbConnectionController.java @@ -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; @@ -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("连接失败"); diff --git a/src/main/java/com/zzg/mybatis/generator/exception/DbDriverLoadingException.java b/src/main/java/com/zzg/mybatis/generator/exception/DbDriverLoadingException.java new file mode 100644 index 00000000..75b7c045 --- /dev/null +++ b/src/main/java/com/zzg/mybatis/generator/exception/DbDriverLoadingException.java @@ -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); + } +} diff --git a/src/main/java/com/zzg/mybatis/generator/util/DbUtil.java b/src/main/java/com/zzg/mybatis/generator/util/DbUtil.java index 3cb18852..544774b5 100644 --- a/src/main/java/com/zzg/mybatis/generator/util/DbUtil.java +++ b/src/main/java/com/zzg/mybatis/generator/util/DbUtil.java @@ -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; @@ -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 drivers; + private static Map drivers = new HashMap<>(); - static { - drivers = new HashMap<>(); - List 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; } @@ -107,4 +96,21 @@ public static String getConnectionUrlWithSchema(DatabaseConfig dbConfig) throws return connectionUrl; } + /** + * 加载数据库驱动 + * @param dbType 数据库类型 + */ + private static void loadDbDriver(DbType dbType){ + List 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()+"驱动"); + } + } }