From 431f51ac8d4deb98bd062dc53bb7a6815dfb07ff Mon Sep 17 00:00:00 2001 From: michaeljy Date: Tue, 15 Aug 2017 22:00:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E9=A9=B1=E5=8A=A8=E5=8A=A0=E8=BD=BD=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=E5=85=B6=E5=8A=A0=E8=BD=BD=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/DbConnectionController.java | 4 ++ .../exception/DbDriverLoadingException.java | 13 ++++++ .../zzg/mybatis/generator/util/DbUtil.java | 44 +++++++++++-------- 3 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/zzg/mybatis/generator/exception/DbDriverLoadingException.java 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()+"驱动"); + } + } }