Java JDBC Database Metadata getDbType(Connection connection)

Here you can find the source of getDbType(Connection connection)

Description

Determine the type of Database, based on the DB connection.

License

BSD License

Parameter

Parameter Description
connection current DB Connection

Exception

Parameter Description
SQLException an exception

Return

a DB keyword/type (see DatabaseUtils.DBMS_* constants)

Declaration

public static String getDbType(Connection connection) throws SQLException 

Method Source Code

//package com.java2s;
/**//from  ww w.  j av  a 2 s.c  o  m
 * The contents of this file are subject to the license and copyright
 * detailed in the LICENSE and NOTICE files at the root of the source
 * tree and available online at
 *
 * http://www.dspace.org/license/
 */

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

import java.sql.SQLException;

import java.util.Locale;

public class Main {
    public static final String DBMS_POSTGRES = "postgres";
    public static final String DBMS_ORACLE = "oracle";
    public static final String DBMS_H2 = "h2";

    /**
     * Determine the type of Database, based on the DB connection.
     * 
     * @param connection current DB Connection
     * @return a DB keyword/type (see DatabaseUtils.DBMS_* constants)
     * @throws SQLException
     */
    public static String getDbType(Connection connection) throws SQLException {
        DatabaseMetaData meta = connection.getMetaData();
        String prodName = meta.getDatabaseProductName();
        String dbms_lc = prodName.toLowerCase(Locale.ROOT);
        if (dbms_lc.contains("postgresql")) {
            return DBMS_POSTGRES;
        } else if (dbms_lc.contains("oracle")) {
            return DBMS_ORACLE;
        } else if (dbms_lc.contains("h2")) // Used for unit testing only
        {
            return DBMS_H2;
        } else {
            return dbms_lc;
        }
    }
}

Related

  1. getDatabaseMetaData(Connection con)
  2. getDatabaseType(DatabaseMetaData metaData)
  3. getDBMSName(Connection conn)
  4. getDBProduct(Connection conn)
  5. getDBTables(DatabaseMetaData metaData, String dataBase, String user)
  6. getDBUsers(DatabaseMetaData metaData)
  7. getDriverSpecificSettings(Connection connection, String defaultUnionColumnValue)
  8. getForeignKeyDeferrability(int code)
  9. getJDBCMajorVersion(Connection conn)