瀏覽代碼

废弃 DBUtil

deason 4 年之前
父節點
當前提交
4aef4ab7fb

+ 0 - 14
src/main/java/cn/com/qmth/examcloud/bridge/BridgeApp.java

@@ -1,7 +1,6 @@
 package cn.com.qmth.examcloud.bridge;
 
 import cn.com.qmth.examcloud.web.bootstrap.AppBootstrap;
-import cn.com.qmth.examcloud.web.support.SpringContextHolder;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 import org.springframework.context.annotation.ComponentScan;
@@ -33,20 +32,7 @@ public class BridgeApp {
      * @author WANGWEI
      */
     public static void main(String[] args) {
-
         AppBootstrap.run(BridgeApp.class, args);
-
-        test();
-    }
-
-    /**
-     * 测试方法
-     *
-     * @author WANGWEI
-     */
-    private static void test() {
-        Tester tester = SpringContextHolder.getBean(Tester.class);
-        tester.test();
     }
 
 }

+ 0 - 40
src/main/java/cn/com/qmth/examcloud/bridge/Tester.java

@@ -1,40 +0,0 @@
-package cn.com.qmth.examcloud.bridge;
-
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-
-import org.springframework.stereotype.Component;
-
-import cn.com.qmth.examcloud.bridge.commons.BridgeDataSource;
-import cn.com.qmth.examcloud.bridge.commons.BridgeDatabaseUtil;
-
-@Component
-public class Tester {
-
-	public void test() {
-		// testJdbc();
-	}
-
-	/**
-	 * 测试JDBC
-	 *
-	 * @author WANGWEI
-	 */
-	public void testJdbc() {
-		try {
-			Connection conn = BridgeDatabaseUtil.getConnection(BridgeDataSource.EXAM_CLOUD);
-			PreparedStatement ps = conn.prepareStatement("select 'jdbc test' from dual");
-			ResultSet rs = ps.executeQuery();
-
-			while (rs.next()) {
-				Object object = rs.getObject(1);
-				System.out.println(object);
-			}
-		} catch (SQLException e) {
-			e.printStackTrace();
-		}
-	}
-
-}

+ 0 - 25
src/main/java/cn/com/qmth/examcloud/bridge/commons/BridgeDatabaseUtil.java

@@ -1,25 +0,0 @@
-package cn.com.qmth.examcloud.bridge.commons;
-
-import java.sql.Connection;
-
-/**
- * 数据库工具
- *
- * @author WANGWEI
- * @date 2019年10月14日
- * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
- */
-public class BridgeDatabaseUtil {
-
-	/**
-	 * get JDBC connection
-	 *
-	 * @author WANGWEI
-	 * @param bridgeDataSource
-	 * @return
-	 */
-	public static Connection getConnection(BridgeDataSource bridgeDataSource) {
-		return DBUtil.getConnection(bridgeDataSource.getName());
-	}
-
-}

+ 0 - 254
src/main/java/cn/com/qmth/examcloud/bridge/commons/DBUtil.java

@@ -1,254 +0,0 @@
-package cn.com.qmth.examcloud.bridge.commons;
-
-import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
-import cn.com.qmth.examcloud.commons.util.PropertiesUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.sql.DataSource;
-import java.sql.Connection;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
-/**
- * 类注释
- *
- * @author WANGWEI
- */
-public class DBUtil {
-
-	/**
-	 * 属性注释
-	 */
-	private static final Logger LOG = LoggerFactory.getLogger(DBUtil.class);
-
-	/**
-	 * 属性注释
-	 */
-	private static final Object LOCK = new Object();
-
-	/**
-	 * 属性注释
-	 */
-	private static Map<String, DataSource> dataSources = new ConcurrentHashMap<String, DataSource>();
-
-	/**
-	 * 初始化数据源(C3P0)
-	 *
-	 * @author WANGWEI
-	 * @param dataSourceName
-	 * @return
-	 */
-	public static DataSource initDataSource(final String dataSourceName) {
-		Class<?> c = null;
-		try {
-			c = Class.forName("com.alibaba.druid.pool.DruidDataSource");
-		} catch (ClassNotFoundException e) {
-			// ignore
-		}
-
-		if (null == c) {
-			try {
-				c = Class.forName("com.mchange.v2.c3p0.ComboPooledDataSource");
-			} catch (ClassNotFoundException e) {
-				// ignore
-			}
-		}
-
-		if (null == c) {
-			throw new ExamCloudRuntimeException("no class about dataSource implementation.");
-		}
-
-		@SuppressWarnings("unchecked")
-		Class<? extends DataSource> z = (Class<? extends DataSource>) c;
-
-		return initDataSource(z, dataSourceName);
-	}
-
-	/**
-	 * 初始化数据源
-	 *
-	 * @author WANGWEI
-	 * 
-	 * @param implementation
-	 *            数据源实现类
-	 * @param dataSourceName
-	 *            数据源名称
-	 * @return
-	 */
-	public static DataSource initDataSource(Class<? extends DataSource> implementation,
-			final String dataSourceName) {
-
-		String dbDriver = PropertiesUtil.getString(dataSourceName + ".driver");
-		String url = PropertiesUtil.getString(dataSourceName + ".url");
-		String userName = PropertiesUtil.getString(dataSourceName + ".username");
-		String password = PropertiesUtil.getString(dataSourceName + ".password");
-		int initialPoolSize = PropertiesUtil.getInt(dataSourceName + ".initialPoolSize", 1);
-		int minPoolSize = PropertiesUtil.getInt(dataSourceName + ".minPoolSize", 1);
-		int maxPoolSize = PropertiesUtil.getInt(dataSourceName + ".maxPoolSize", 10);
-
-		DataSource dataSource = null;
-		String simpleName = implementation.getSimpleName();
-		try {
-			// C3P0
-			if (simpleName.equals("ComboPooledDataSource")) {
-				DataSource c3po = implementation.newInstance();
-				dataSource = c3po;
-				implementation.getMethod("setDriverClass", String.class).invoke(c3po, dbDriver);
-				implementation.getMethod("setJdbcUrl", String.class).invoke(c3po, url);
-				implementation.getMethod("setUser", String.class).invoke(c3po, userName);
-				implementation.getMethod("setPassword", String.class).invoke(c3po, password);
-				implementation.getMethod("setInitialPoolSize", int.class).invoke(c3po,
-						initialPoolSize);
-				implementation.getMethod("setMinPoolSize", int.class).invoke(c3po, minPoolSize);
-				implementation.getMethod("setMaxPoolSize", int.class).invoke(c3po, maxPoolSize);
-				implementation.getMethod("setPreferredTestQuery", String.class).invoke(c3po,
-						"select 1 from dual");
-			}
-			// druid
-			else if (simpleName.equals("DruidDataSource")) {
-				DataSource druid = implementation.newInstance();
-				dataSource = druid;
-				implementation.getMethod("setDriverClassName", String.class).invoke(druid,
-						dbDriver);
-				implementation.getMethod("setUrl", String.class).invoke(druid, url);
-				implementation.getMethod("setUsername", String.class).invoke(druid, userName);
-				implementation.getMethod("setPassword", String.class).invoke(druid, password);
-				implementation.getMethod("setInitialSize", int.class).invoke(druid,
-						initialPoolSize);
-				implementation.getMethod("setMaxActive", int.class).invoke(druid, maxPoolSize);
-
-				implementation.getMethod("setValidationQuery", String.class).invoke(druid,
-						"select 1 from dual");
-			}
-
-		} catch (Exception e) {
-			LOG.error("[JDBC] Fail to init dataSource. dataSourceName=" + dataSourceName, e);
-			throw new RuntimeException(e);
-		}
-
-		Connection conn = null;
-		try {
-			conn = dataSource.getConnection();
-		} catch (Exception e) {
-			LOG.error("[JDBC] Fail to get connection. dataSourceName=" + dataSourceName, e);
-			throw new RuntimeException(e);
-		} finally {
-			close(conn);
-		}
-
-		return dataSource;
-	}
-
-	/**
-	 * 方法注释
-	 *
-	 * @author WANGWEI
-	 * @param dataSourceName
-	 * @return
-	 */
-	public static DataSource getDataSource(String dataSourceName) {
-		DataSource dataSource = dataSources.get(dataSourceName);
-		if (null == dataSource) {
-			synchronized (LOCK) {
-				dataSource = dataSources.get(dataSourceName);
-				if (null == dataSource) {
-					dataSource = initDataSource(dataSourceName);
-					dataSources.put(dataSourceName, dataSource);
-				}
-			}
-		}
-		return dataSource;
-	}
-
-	/**
-	 * 方法注释
-	 *
-	 * @author WANGWEI
-	 * @param dataSourceName
-	 * @return
-	 */
-	public static Connection getConnection(String dataSourceName) {
-		Connection conn = null;
-		try {
-			conn = getDataSource(dataSourceName).getConnection();
-		} catch (SQLException e) {
-			LOG.error("[JDBC] Fail to get connection.", e);
-		}
-
-		return conn;
-	}
-
-	/**
-	 * @param conn
-	 * @param stmt
-	 * @param res
-	 */
-	public static void close(Connection conn, Statement statement, ResultSet rs) {
-		close(rs);
-		close(conn, statement);
-	}
-
-	/**
-	 * @param conn
-	 * @param statement
-	 */
-	public static void close(Connection conn, Statement statement) {
-		close(statement);
-		close(conn);
-	}
-
-	/**
-	 * @param stmt
-	 * @param res
-	 */
-	public static void close(Statement statement, ResultSet rs) {
-		close(statement);
-		close(rs);
-	}
-
-	/**
-	 * @param conn
-	 */
-	public static void close(Connection conn) {
-		if (conn != null) {
-			try {
-				conn.close();
-			} catch (SQLException e) {
-				LOG.error("[JDBC] Fail to close Connection.", e);
-			}
-		}
-	}
-
-	/**
-	 * @param statement
-	 */
-	public static void close(Statement statement) {
-		if (statement != null) {
-			try {
-				statement.close();
-			} catch (SQLException e) {
-				LOG.error("[JDBC] Fail to close Statement.", e);
-			}
-		}
-	}
-
-	/**
-	 * @param rs
-	 */
-	public static void close(ResultSet rs) {
-		if (rs != null) {
-			try {
-				rs.close();
-			} catch (SQLException e) {
-				LOG.error("[JDBC] Fail to close ResultSet.", e);
-			}
-		}
-	}
-
-
-
-}