简体   繁体   中英

Unload the jdbc sql server drivers

I load JDBC SQL Server drivers to connect to a database after the whole process I want to unload all the registered drivers

To register the drivers I use

  static {
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

How can I deregister all these drivers?

I tried the following but it seems not to be working

 Enumeration<Driver> drivers = DriverManager.getDrivers();
    while(drivers.nextElement() != null){
        Driver d = drivers.nextElement();
        try {
            DriverManager.deregisterDriver(d);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

I get the following exception

java.util.NoSuchElementException: Vector Enumeration

Edit

Now I get the following error when I run another task which also needs a connection to database using JDBC

 WARNING: Failed to load the sqljdbc_auth.dll cause : Native Library C:\sqljdbc_4.0\enu\auth\x86\sqljdbc_auth.dll already loaded in another classloader

any idea?

You have to ensure there's an element with hasMoreElements() before using it with nextElement() .

See also the javadoc: https://docs.oracle.com/javase/8/docs/api/java/util/Enumeration.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM