springboot动态加载驱动

DynamicDriverUtil:
package com.zy.fastdync.demos.web;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.*;
import java.util.Properties;
import java.util.logging.Logger;

public class DynamicDriverUtil {
  // 动态加载jdbc驱动
  public static void dynamicLoadJdbc(String inputJarPath,String className) throws Exception {
    File jarPath = new File(inputJarPath);
    URL u = jarPath.toURI().toURL();
    URLClassLoader ucl = new URLClassLoader(new URL[]{u},Thread.currentThread().getContextClassLoader());

    Driver d = (Driver) ucl.loadClass(className).newInstance();
    DriverShim driver = new DriverShim(d);
    DriverManager.registerDriver(driver);
  }
}

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, P

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
青葱年少的头像青葱年少普通用户
上一篇 2023年11月29日
下一篇 2023年11月29日

相关推荐