Example usage for java.sql DriverManager getDrivers

List of usage examples for java.sql DriverManager getDrivers

Introduction

In this page you can find the example usage for java.sql DriverManager getDrivers.

Prototype

@CallerSensitive
public static Enumeration<Driver> getDrivers() 

Source Link

Document

Retrieves an Enumeration with all of the currently loaded JDBC drivers to which the current caller has access.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    List drivers = Collections.list(DriverManager.getDrivers());
    for (int i = 0; i < drivers.size(); i++) {
        Driver driver = (Driver) drivers.get(i);

        String name = driver.getClass().getName();
        System.out.println(name);

        int majorVersion = driver.getMajorVersion();
        System.out.println(majorVersion);
        int minorVersion = driver.getMinorVersion();
        System.out.println(minorVersion);
        boolean isJdbcCompliant = driver.jdbcCompliant();
        System.out.println(isJdbcCompliant);
    }//from  w  w w .jav a2s  .c  om
}

From source file:org.sonews.Application.java

/**
 * The main entrypoint./*from   w  w w . ja v a  2 s. c  o  m*/
 *
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    System.out.println(VERSION);
    Thread.currentThread().setName("Mainthread");

    // Command line arguments
    boolean async = false;
    boolean feed = false; // Enable feeding?
    boolean purger = false; // Enable message purging?
    int port = -1;

    for (int n = 0; n < args.length; n++) {
        switch (args[n]) {
        case "-async": {
            async = true;
            break;
        }
        case "-c":
        case "-config": {
            Config.inst().set(Config.LEVEL_CLI, Config.CONFIGFILE, args[++n]);
            System.out.println("Using config file " + args[n]);
            break;
        }
        case "-C":
        case "-context": {
            // FIXME: Additional context files
            n++;
            break;
        }
        case "-dumpjdbcdriver": {
            System.out.println("Available JDBC drivers:");
            Enumeration<Driver> drvs = DriverManager.getDrivers();
            while (drvs.hasMoreElements()) {
                System.out.println(drvs.nextElement());
            }
            return;
        }
        case "-feed": {
            feed = true;
            break;
        }
        case "-h":
        case "-help": {
            printArguments();
            return;
        }
        case "-p": {
            port = Integer.parseInt(args[++n]);
            break;
        }
        case "-plugin-storage": {
            System.out.println("Warning: -plugin-storage is not implemented!");
            break;
        }
        case "-purger": {
            purger = true;
            break;
        }
        case "-v":
        case "-version":
            // Simply return as the version info is already printed above
            return;
        }
    }

    ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
    context = new FileSystemXmlApplicationContext(new String[] { "sonews.xml" }, context);

    // Enable storage backend
    StorageProvider sprov = context.getBean("storageProvider", StorageProvider.class);
    StorageManager.enableProvider(sprov);

    ChannelLineBuffers.allocateDirect();

    // Add shutdown hook
    Runtime.getRuntime().addShutdownHook(new ShutdownHook());

    // Start the listening daemon
    if (port <= 0) {
        port = Config.inst().get(Config.PORT, 119);
    }

    NNTPDaemon daemon = context.getBean(NNTPDaemon.class);
    daemon.setPort(port);
    daemon.start();

    // Start Connections purger thread...
    Connections.getInstance().start();

    // Start feeds
    if (feed) {
        FeedManager.startFeeding();
    }

    if (purger) {
        Purger purgerDaemon = new Purger();
        purgerDaemon.start();
    }

    // Wait for main thread to exit (setDaemon(false))
    daemon.join();
}

From source file:com.ironiacorp.persistence.SqlUtil.java

/**
 * Check if the database driver is loaded.
 * /*from  w  w  w .j  a v  a  2 s .  c om*/
 * @return True if the driver is loaded, False otherwise.
 */
public static boolean isDriverLoaded(String driver) {
    // Check if the driver has been already loaded
    Enumeration<Driver> drivers = DriverManager.getDrivers();
    while (drivers.hasMoreElements()) {
        Driver d = drivers.nextElement();
        if (d.getClass().getName().equals(driver)) {
            return true;
        }
    }
    return false;
}

From source file:org.jumpmind.db.util.BasicDataSourceFactory.java

public static void prepareDriver(String clazzName) throws Exception {
    Driver driver = (Driver) Class.forName(clazzName).newInstance();
    synchronized (DriverManager.class) {
        Enumeration<Driver> drivers = DriverManager.getDrivers();
        while (drivers.hasMoreElements()) {
            Driver driver2 = (Driver) drivers.nextElement();
            /* /* w  ww  . j a  v  a 2  s  .  c  o  m*/
             * MySQL and Maria DB drivers cannot co-exist because
             * they use the same JDBC URL.
             */
            if ((driver.getClass().getName().equals("com.mysql.jdbc.Driver")
                    && driver2.getClass().getName().equals("org.mariadb.jdbc.Driver"))
                    || (driver.getClass().getName().equals("org.mariadb.jdbc.Driver")
                            && driver2.getClass().getName().equals("com.mysql.jdbc.Driver"))) {
                DriverManager.deregisterDriver(driver2);
            }
        }
    }
}

From source file:com.ironiacorp.persistence.SqlUtil.java

/**
 * Unload the database driver (if it's loaded).
 *///w ww. j a v a  2  s. c om
public static void unloadDriver(String driver) {
    if (!SqlUtil.isDriverLoaded(driver)) {
        return;
    }

    Enumeration<Driver> drivers = DriverManager.getDrivers();
    while (drivers.hasMoreElements()) {
        Driver d = drivers.nextElement();
        if (d.getClass().getName().equals(driver)) {
            try {
                DriverManager.deregisterDriver(d);
            } catch (SQLException e) {
            }
        }
    }
}

From source file:org.synchronoss.cloud.nio.multipart.example.utils.ContextFinalizer.java

@Override
public void onApplicationEvent(ContextClosedEvent event) {

    if (log.isInfoEnabled())
        log.info("onApplicationEvent: " + event);

    Enumeration<Driver> drivers = DriverManager.getDrivers();
    Driver driver = null;//  w  w w  .j av  a2 s .c o  m
    while (drivers.hasMoreElements()) {
        try {
            driver = drivers.nextElement();
            DriverManager.deregisterDriver(driver);
            if (log.isWarnEnabled())
                log.warn(String.format("Driver %s unregistered", driver));
        } catch (SQLException e) {
            if (log.isWarnEnabled())
                log.warn(String.format("Error unregistering driver %s", driver), e);
        }
    }
    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
    Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
    for (Thread thread : threadArray) {
        if (thread.getName().contains("Abandoned connection cleanup thread")) {
            synchronized (thread) {
                thread.stop(); //don't complain, it works
            }
        }
    }
    if (log.isInfoEnabled())
        log.info("Finished processing onApplicationEvent");
}

From source file:com.wavemaker.runtime.server.CleanupListener.java

@Override
public void contextDestroyed(ServletContextEvent event) {
    try {/*from   w w  w .j a  va2  s.  c  o  m*/
        // remove from the system DriverManager the JDBC drivers registered
        // by this web app
        for (Enumeration<Driver> e = CastUtils.cast(DriverManager.getDrivers()); e.hasMoreElements();) {
            Driver driver = e.nextElement();
            if (driver.getClass().getClassLoader() == getClass().getClassLoader()) {
                DriverManager.deregisterDriver(driver);
            }
        }

        LogFactory.releaseAll();

        // flush all of the Introspector's internal caches
        Introspector.flushCaches();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.longfalcon.web.listener.ContextListener.java

@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
    // Thanks StackOverflow
    // This manually deregisters JDBC driver, which prevents Tomcat 7 from complaining about memory leaks wrto this class
    Enumeration<Driver> drivers = DriverManager.getDrivers();
    while (drivers.hasMoreElements()) {
        Driver driver = drivers.nextElement();
        try {/*from w ww. j  a  va 2s  . c o  m*/
            DriverManager.deregisterDriver(driver);
            _log.info(String.format("deregistering jdbc driver: %s", driver));
        } catch (SQLException e) {
            _log.error(String.format("Error deregistering driver %s", driver), e);
        }

    }
}

From source file:eionet.meta.ContainerContextClosedHandler.java

/**
 * Unregisters mysql connections./*from w  w w .j  a  v  a 2  s  .  c om*/
 *
 * @param servletContextEvent
 *            servlet context
 */
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
    Enumeration<Driver> drivers = DriverManager.getDrivers();

    Driver driver = null;

    // clear drivers
    while (drivers.hasMoreElements()) {
        try {
            driver = drivers.nextElement();
            DriverManager.deregisterDriver(driver);
        } catch (SQLException ex) {
            // deregistration failed
            LOGGER.warn(ex.getMessage());
        }
    }

    // MySQL driver leaves around a thread. This static method cleans it up.
    try {
        // throw new InterruptedException();
        AbandonedConnectionCleanupThread.shutdown();
    } catch (InterruptedException ex) {
        // again failure
        LOGGER.warn(ex.getMessage());
    }
}

From source file:org.davidmendoza.fileUpload.utils.ContextFinalizer.java

@Override
public void onApplicationEvent(ContextClosedEvent e) {
    log.info("Stopping connections");
    Enumeration<Driver> drivers = DriverManager.getDrivers();
    Driver d = null;//  w w  w .j av  a2s .co  m
    while (drivers.hasMoreElements()) {
        try {
            d = drivers.nextElement();
            DriverManager.deregisterDriver(d);
            log.warn(String.format("Driver %s deregistered", d));
        } catch (SQLException ex) {
            log.warn(String.format("Error deregistering driver %s", d), ex);
        }
    }
    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
    Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
    for (Thread t : threadArray) {
        if (t.getName().contains("Abandoned connection cleanup thread")) {
            synchronized (t) {
                t.stop(); //don't complain, it works
            }
        }
    }
    log.info("Finished stopping connections");
}