Java DataSource getDatabaseName(DataSource dataSource)

Here you can find the source of getDatabaseName(DataSource dataSource)

Description

get Database Name

License

Apache License

Declaration

public static String getDatabaseName(DataSource dataSource) 

Method Source Code

//package com.java2s;
/*//from www . j  a v  a2 s.c  o m
 *    Copyright 2009-2011 The MyBatis Team
 *
 *    Licensed 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.
 */

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;

public class Main {
    private static Map<String, String> NAME_TYPE = new HashMap<String, String>();

    public static String getDatabaseName(DataSource dataSource) {
        Connection con = null;
        try {
            con = dataSource.getConnection();
            if (con == null) {
                throw new RuntimeException(
                        "Connection returned by DataSource [" + dataSource
                                + "] was null");
            }

            DatabaseMetaData metaData = con.getMetaData();
            if (metaData == null) {
                throw new RuntimeException(
                        "DatabaseMetaData returned by Connection [" + con
                                + "] was null");
            }

            String productName = metaData.getDatabaseProductName();
            return NAME_TYPE.get(productName);
        } catch (SQLException e) {
            throw new RuntimeException(
                    "Could not get database product name", e);
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    // ignored
                }
            }
        }
    }
}

Related

  1. executeQuery(DataSource dataSource, String... queries)
  2. getConnection(CommonDataSource dataSource)
  3. getConnection(DataSource dataSource)
  4. getConnection(final DataSource ds, final boolean autoCommit)
  5. getConnectionFromDataSource(DataSource ds)
  6. getDataSource(String jndi)
  7. getDataSource(String jndiName)
  8. getDataSource(String service)
  9. getDataSourceCache(String hostName)