Java JDBC Connection Create canConnect(Properties dbSettings)

Here you can find the source of canConnect(Properties dbSettings)

Description

Returns whether is possible to connect to the database server for a given connection settings.

License

Open Source License

Parameter

Parameter Description
dbSettings The datatabase connection settings

Exception

Parameter Description
SQLException an exception

Return

true if is possible to connect to the database server and false otherwise.

Declaration

public static void canConnect(Properties dbSettings) throws SQLException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) {2009,2011} {Software Design and Collaboration Laboratory (SDCL)
 *            , University of California, Irvine}.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  w w  w.  j a va  2 s.  c  om
 *    {Software Design and Collaboration Laboratory (SDCL)
 *   , University of California, Irvine} 
 *         - initial API and implementation and/or initial documentation
 *******************************************************************************/

import java.sql.Connection;
import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.Properties;

public class Main {
    /**
     * Returns whether is possible to connect to the database server for a given
     * connection settings.
     * 
     * @param dbSettings
     *            The datatabase connection settings
     * @return <code>true</code> if is possible to connect to the database
     *         server and <code>false</code> otherwise.
     * @throws SQLException 
     */
    public static void canConnect(Properties dbSettings) throws SQLException {
        String url = dbSettings.getProperty("hibernate.connection.url");
        String username = dbSettings.getProperty("hibernate.connection.username");
        String password = dbSettings.getProperty("hibernate.connection.password");
        Connection conn = DriverManager.getConnection(url, username, password);
        conn.close();
    }
}

Related

  1. clearTable(String tableName, String driver, String url, String user, String password)
  2. createConnection()
  3. createConnection(Properties props, String prefix)
  4. createDatabase(String dbConnectionString, String dbName, String user, String pass, String driver)