1. 程式人生 > >動態載入jdbc驅動(可測試jdbc不同版本相容性)

動態載入jdbc驅動(可測試jdbc不同版本相容性)

緣由

接到一個新任務,需要測試應用對mysql jdbc不同版本驅動的相容性。這裡希望能夠以程式碼的形式,手動載入不同版本的mysql jdbc驅動,比如有下列的驅動版本,我要一個一個進行測試:

  • mysql-connector-java-5.1.6.jar
  • mysql-connector-java-5.1.31.jar
  • mysql-connector-java-5.1.35.jar
  • mysql-connector-java-5.1.39.jar
  • mysql-connector-java-6.0.3.jar

實現

參考stackoverflow上做法:

覺得挺有意思的,寫了如下程式碼,可以實現動態載入不同版本的jdbc驅動進行一些測試:

import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
import
java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.logging.Logger; public class MycatMulitJdbcVersionTest { private static final String JDBC_URL = "jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC&useSSL=false"; private static final String USER = "root"
; private static final String PASSWORD = "mysql"; private static final Map<String, String> jdbcVersionMap = new HashMap<String, String>(); private static final Map<String, Driver> tmpDriverMap = new HashMap<String, Driver>(); // 動態載入jdbc驅動 private static void dynamicLoadJdbc(String mysqlJdbcFile) throws Exception { URL u = new URL("jar:file:lib/" + mysqlJdbcFile + "!/"); String classname = jdbcVersionMap.get(mysqlJdbcFile); URLClassLoader ucl = new URLClassLoader(new URL[] { u }); Driver d = (Driver)Class.forName(classname, true, ucl).newInstance(); DriverShim driver = new DriverShim(d); DriverManager.registerDriver(driver); tmpDriverMap.put(mysqlJdbcFile, driver); } // 每一次測試完解除安裝對應版本的jdbc驅動 private static void dynamicUnLoadJdbc(String mysqlJdbcFile) throws SQLException { DriverManager.deregisterDriver(tmpDriverMap.get(mysqlJdbcFile)); } // 進行一次測試 private static void testOneVersion(String mysqlJdbcFile) { System.out.println("start test mysql jdbc version : " + mysqlJdbcFile); try { dynamicLoadJdbc(mysqlJdbcFile); } catch (Exception e1) { e1.printStackTrace(); } Connection conn = null; try { conn = DriverManager.getConnection(JDBC_URL, USER, PASSWORD); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select user()"); System.out.println("select user() output : "); while(rs.next()) { System.out.println(rs.getObject(1)); } rs = stmt.executeQuery("show tables"); System.out.println("show tables output : "); while(rs.next()) { System.out.println(rs.getObject(1)); } } catch (SQLException e) { e.printStackTrace(); } finally { if(conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } try { dynamicUnLoadJdbc(mysqlJdbcFile); } catch (SQLException e) { e.printStackTrace(); } System.out.println("end !!!"); System.out.println(); } public static void main(String[] args) { jdbcVersionMap.put("mysql-connector-java-6.0.3.jar", "com.mysql.cj.jdbc.Driver"); jdbcVersionMap.put("mysql-connector-java-5.1.6.jar", "com.mysql.jdbc.Driver"); jdbcVersionMap.put("mysql-connector-java-5.1.31.jar", "com.mysql.jdbc.Driver"); jdbcVersionMap.put("mysql-connector-java-5.1.35.jar", "com.mysql.jdbc.Driver"); jdbcVersionMap.put("mysql-connector-java-5.1.39.jar", "com.mysql.jdbc.Driver"); for(String mysqlJdbcFile : jdbcVersionMap.keySet()) { testOneVersion(mysqlJdbcFile); } } } class DriverShim implements Driver { private Driver driver; DriverShim(Driver d) { this.driver = d; } public boolean acceptsURL(String u) throws SQLException { return this.driver.acceptsURL(u); } public Connection connect(String u, Properties p) throws SQLException { return this.driver.connect(u, p); } @Override public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { return this.driver.getPropertyInfo(url, info); } @Override public int getMajorVersion() { return this.driver.getMajorVersion(); } @Override public int getMinorVersion() { return this.driver.getMinorVersion(); } @Override public boolean jdbcCompliant() { return this.driver.jdbcCompliant(); } @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { return this.driver.getParentLogger(); } }