Java JDBC Driver loadDriver(String driverClassName, String jarFileName)

Here you can find the source of loadDriver(String driverClassName, String jarFileName)

Description

load Driver

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    private static Driver loadDriver(String driverClassName, String jarFileName) throws Exception 

Method Source Code


//package com.java2s;
/*//  ww  w.j  a va2  s . co  m
 *   Copyright 2008-2011 Follett Software Company 
 *
 *   This file is part of PerfMon4j(tm).
 *
 *    Perfmon4j is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License, version 3,
 *    as published by the Free Software Foundation.  This program is distributed
 *    WITHOUT ANY WARRANTY OF ANY KIND, WITHOUT AN IMPLIED WARRANTY OF MERCHANTIBILITY,
 *    OR FITNESS FOR A PARTICULAR PURPOSE.  You should have received a copy of the GNU Lesser General Public 
 *    License, Version 3, along with this program.  If not, you can obtain the LGPL v.s at 
 *    http://www.gnu.org/licenses/
 *    
 *    perfmon4j@fsc.follett.com
 *    David Deuchert
 *    Follett Software Company
 *    1391 Corporate Drive
 *    McHenry, IL 60050
 * 
*/

import java.io.File;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

import java.sql.Driver;

public class Main {
    @SuppressWarnings("unchecked")
    private static Driver loadDriver(String driverClassName, String jarFileName) throws Exception {
        Class<Driver> driverClazz;

        if (jarFileName != null) {
            File driverFile = new File(jarFileName);
            if (!driverFile.exists()) {
                throw new FileNotFoundException("File: " + jarFileName + " NOT FOUND");
            }
            URL url;
            try {
                url = driverFile.toURI().toURL();
            } catch (MalformedURLException e) {
                throw new Exception("Unable to convert to URL - file: " + jarFileName, e);
            }
            ClassLoader loader = new URLClassLoader(new URL[] { url },
                    Thread.currentThread().getContextClassLoader());
            driverClazz = (Class<Driver>) Class.forName(driverClassName, true, loader);
        } else {
            driverClazz = (Class<Driver>) Class.forName(driverClassName, true,
                    Thread.currentThread().getContextClassLoader());
        }

        return driverClazz.newInstance();
    }
}

Related

  1. getStatement()
  2. getUnderlyingDriver(String url)
  3. hasDriver()
  4. isDriverLoaded(String driver)
  5. loadDriver(String driver)
  6. printAllDrivers()