Java Database Driver Load loadDriver(String driverClassName)

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

Description

Loads and registers a database driver class.

License

Apache License

Parameter

Parameter Description
driverClassName of driver to load

Return

boolean true if the driver was found, otherwise false

Declaration

public static boolean loadDriver(String driverClassName) 

Method Source Code

//package com.java2s;
/*//w ww . ja va 2  s  .co  m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Loads and registers a database driver class.
     * If this succeeds, it returns true, else it returns false.
     *
     * @param driverClassName of driver to load
     * @return boolean <code>true</code> if the driver was found, otherwise <code>false</code>
     */
    public static boolean loadDriver(String driverClassName) {

        //       try {
        //          Class.forName("com.mysql.jdbc.Driver");
        //         Connection connectMySQL =DriverManager.getConnection("jdbc:mysql://localhost:3306/myuser","root" ,"root" );
        //      } catch (Exception e1) {
        //         // TODO Auto-generated catch block
        //         e1.printStackTrace();
        //      }
        try {
            Class.forName(driverClassName).newInstance();
            return true;

        } catch (ClassNotFoundException e) {
            return false;

        } catch (IllegalAccessException e) {
            // Constructor is private, OK for DriverManager contract
            return true;

        } catch (InstantiationException e) {
            return false;

        } catch (Throwable e) {
            return false;
        }
    }
}

Related

  1. loadDriver()