Example usage for java.sql DatabaseMetaData getDatabaseProductName

List of usage examples for java.sql DatabaseMetaData getDatabaseProductName

Introduction

In this page you can find the example usage for java.sql DatabaseMetaData getDatabaseProductName.

Prototype

String getDatabaseProductName() throws SQLException;

Source Link

Document

Retrieves the name of this database product.

Usage

From source file:org.wso2.mobile.utils.persistence.EMMDBInitializer.java

public static String getDatabaseType(Connection conn) throws Exception {
    String type = null;/*from   w  w w  . j av a2s .  c  om*/
    try {
        if (conn != null && (!conn.isClosed())) {
            DatabaseMetaData metaData = conn.getMetaData();
            String databaseProductName = metaData.getDatabaseProductName();
            if (databaseProductName.matches("(?i).*hsql.*")) {
                type = "hsql";
            } else if (databaseProductName.matches("(?i).*derby.*")) {
                type = "derby";
            } else if (databaseProductName.matches("(?i).*mysql.*")) {
                type = "mysql";
            } else if (databaseProductName.matches("(?i).*oracle.*")) {
                type = "oracle";
            } else if (databaseProductName.matches("(?i).*microsoft.*")) {
                type = "mssql";
            } else if (databaseProductName.matches("(?i).*h2.*")) {
                type = "h2";
            } else if (databaseProductName.matches("(?i).*db2.*")) {
                type = "db2";
            } else if (databaseProductName.matches("(?i).*postgresql.*")) {
                type = "postgresql";
            } else if (databaseProductName.matches("(?i).*openedge.*")) {
                type = "openedge";
            } else if (databaseProductName.matches("(?i).*informix.*")) {
                type = "informix";
            } else {
                String msg = "Unsupported database: " + databaseProductName
                        + ". Database will not be created automatically by the WSO2 EMM Server. "
                        + "Please create the database using appropriate database scripts for "
                        + "the database.";
                throw new Exception(msg);
            }
        }
    } catch (SQLException e) {
        String msg = "Failed to create emm database." + e.getMessage();
        log.fatal(msg, e);
        throw new Exception(msg, e);
    }
    return type;
}

From source file:org.wso2.carbon.social.sql.SocialDBInitilizer.java

public static String getDatabaseType(Connection conn) throws Exception {
    String type = null;/*from  www .  jav a2  s.co  m*/
    try {
        if (conn != null && (!conn.isClosed())) {
            DatabaseMetaData metaData = conn.getMetaData();
            String databaseProductName = metaData.getDatabaseProductName();
            if (databaseProductName.matches("(?i).*hsql.*")) {
                type = "hsql";
            } else if (databaseProductName.matches("(?i).*derby.*")) {
                type = "derby";
            } else if (databaseProductName.matches("(?i).*mysql.*")) {
                type = "mysql";
            } else if (databaseProductName.matches("(?i).*oracle.*")) {
                type = "oracle";
            } else if (databaseProductName.matches("(?i).*microsoft.*")) {
                type = "mssql";
            } else if (databaseProductName.matches("(?i).*h2.*")) {
                type = "h2";
            } else if (databaseProductName.matches("(?i).*db2.*")) {
                type = "db2";
            } else if (databaseProductName.matches("(?i).*postgresql.*")) {
                type = "postgresql";
            } else if (databaseProductName.matches("(?i).*openedge.*")) {
                type = "openedge";
            } else if (databaseProductName.matches("(?i).*informix.*")) {
                type = "informix";
            } else {
                String msg = "Unsupported database: " + databaseProductName
                        + ". Database will not be created automatically by the WSO2 ES. "
                        + "Please create the database using appropriate database scripts for "
                        + "the SOCIAL database.";
                throw new Exception(msg);
            }
        }
    } catch (SQLException e) {
        String msg = "Failed to create Social database." + e.getMessage();
        log.fatal(msg, e);
        throw new Exception(msg, e);
    }
    return type;
}

From source file:TerminalMonitor.java

static public void showVersion(DatabaseMetaData meta) {
    try {/*  w w w .  j a va2 s . c  o m*/
        System.out.println("TerminalMonitor v2.0");
        System.out.println("DBMS: " + meta.getDatabaseProductName() + " " + meta.getDatabaseProductVersion());
        System.out.println("JDBC Driver: " + meta.getDriverName() + " " + meta.getDriverVersion());
    } catch (SQLException e) {
        System.out.println("Failed to get version info: " + e.getMessage());
    }
}

From source file:org.snaker.engine.access.jdbc.JdbcHelper.java

/**
 * ???/*from  w w  w.j a  v  a2  s.  c o m*/
 * @param conn ?
 * @return Dialect 
 * @throws Exception
 */
public static Dialect getDialect(Connection conn) throws Exception {
    DatabaseMetaData databaseMetaData = conn.getMetaData();
    String databaseProductName = databaseMetaData.getDatabaseProductName();
    String dbType = databaseTypeMappings.getProperty(databaseProductName);
    if (StringHelper.isEmpty(dbType))
        return null;
    if (dbType.equalsIgnoreCase("mysql"))
        return new MySqlDialect();
    else if (dbType.equalsIgnoreCase("oracle"))
        return new OracleDialect();
    else if (dbType.equalsIgnoreCase("postgres"))
        return new PostgresqlDialect();
    else if (dbType.equalsIgnoreCase("mssql"))
        return new SQLServerDialect();
    else if (dbType.equalsIgnoreCase("db2"))
        return new Db2Dialect();
    else if (dbType.equalsIgnoreCase("h2"))
        return new H2Dialect();
    else
        return null;
}

From source file:org.wso2.carbon.identity.core.persistence.IdentityDBInitializer.java

public static String getDatabaseType(Connection conn) throws IdentityRuntimeException {
    String type = null;//from w ww. j  a v  a 2 s .  co m
    try {
        if (conn != null && (!conn.isClosed())) {
            DatabaseMetaData metaData = conn.getMetaData();
            String databaseProductName = metaData.getDatabaseProductName();
            if (databaseProductName.matches("(?i).*hsql.*")) {
                type = "hsql";
            } else if (databaseProductName.matches("(?i).*derby.*")) {
                type = "derby";
            } else if (databaseProductName.matches("(?i).*mysql.*")) {
                type = "mysql";
            } else if (databaseProductName.matches("(?i).*oracle.*")) {
                type = "oracle";
            } else if (databaseProductName.matches("(?i).*microsoft.*")) {
                type = "mssql";
            } else if (databaseProductName.matches("(?i).*h2.*")) {
                type = "h2";
            } else if (databaseProductName.matches("(?i).*db2.*")) {
                type = "db2";
            } else if (databaseProductName.matches("(?i).*postgresql.*")) {
                type = "postgresql";
            } else if (databaseProductName.matches("(?i).*openedge.*")) {
                type = "openedge";
            } else if (databaseProductName.matches("(?i).*informix.*")) {
                type = "informix";
            } else {
                String msg = "Unsupported database: " + databaseProductName
                        + ". Database will not be created automatically by the WSO2 Identity Server. "
                        + "Please create the database using appropriate database scripts for "
                        + "the database.";
                throw new IdentityRuntimeException(msg);
            }
        }
    } catch (SQLException e) {
        String msg = "Failed to create identity database." + e.getMessage();
        throw new IdentityRuntimeException(msg, e);
    }
    return type;
}

From source file:ca.nrc.cadc.db.DBUtil.java

private static void testDS(DataSource ds, boolean keepOpen) throws SQLException {
    Connection con = null;//from   w w  w.j a va 2 s . co  m
    try {
        con = ds.getConnection();
        DatabaseMetaData meta = con.getMetaData();
        if (!log.getEffectiveLevel().equals(Level.DEBUG))
            return;
        log.debug("connected to server: " + meta.getDatabaseProductName() + " " + meta.getDatabaseMajorVersion()
                + "." + meta.getDatabaseMinorVersion() + " driver: " + meta.getDriverName() + " "
                + meta.getDriverMajorVersion() + "." + meta.getDriverMinorVersion());
    } finally {
        if (!keepOpen && con != null)
            try {
                con.close();
            } catch (Exception ignore) {
            }
    }
}

From source file:org.wso2.carbon.utils.dbcreator.DatabaseCreator.java

/**
 * computes relatational database type using database name
 *
 * @return String//w  w w.  j a  va  2  s . c o m
 * @throws Exception*
 */
public static String getDatabaseType(Connection conn) throws Exception {
    String type = null;
    try {
        if (conn != null && (!conn.isClosed())) {
            DatabaseMetaData metaData = conn.getMetaData();
            String databaseProductName = metaData.getDatabaseProductName();
            //                if (databaseProductName.matches("(?i).*hsql.*")) {
            //                    type = "hsql";
            //                } else if (databaseProductName.matches("(?i).*derby.*")) {
            //                    type = "derby";
            //                } else if (databaseProductName.matches("(?i).*mysql.*")) {
            //                    type = "mysql";
            //                } else if (databaseProductName.matches("(?i).*oracle.*")) {
            //                    type = "oracle";
            //                } else if (databaseProductName.matches("(?i).*microsoft.*")) {
            //                    type = "mssql";
            //                } else if (databaseProductName.matches("(?i).*h2.*")) {
            //                    type = "h2";
            //                } else if (databaseProductName.matches("(?i).*db2.*")) {
            //                    type = "db2";
            //                } else if (databaseProductName.matches("(?i).*postgresql.*")) {
            //                    type = "postgresql";
            //                } else if (databaseProductName.matches("(?i).*openedge.*")) {
            //                    type = "openedge";
            //                } else {
            //                    String msg = "Unsupported database: " + databaseProductName +
            //                            ". Database will not be created automatically by the WSO2 Registry. " +
            //                            "Please create the database using appropriate database scripts for " +
            //                            "the database.";
            //                    throw new Exception(msg);
            //                }
            type = getDatabaseType(databaseProductName); // avoid code duplication 
        }
    } catch (SQLException e) {
        String msg = "Failed to create registry database." + e.getMessage();
        log.fatal(msg, e);
        throw new Exception(msg, e);
    }
    return type;
}

From source file:org.dashbuilder.dataprovider.backend.sql.JDBCUtils.java

public static Dialect dialect(Connection connection) {
    try {/*  w  w w  .j a v a 2  s.  c  o m*/
        DatabaseMetaData m = connection.getMetaData();
        String url = m.getURL();
        if (!StringUtils.isBlank(url)) {
            return dialect(url, m.getDatabaseMajorVersion());
        }
        String dbName = m.getDatabaseProductName();
        return dialect(dbName.toLowerCase());
    } catch (SQLException e) {
        e.printStackTrace();
        return DEFAULT;
    }
}

From source file:org.apache.openjpa.jdbc.sql.DBDictionaryFactory.java

/**
 * Create the dictionary using connection metadata to determine its type.
 *//*from  w ww . ja  va 2  s. c  om*/
public static DBDictionary newDBDictionary(JDBCConfiguration conf, DataSource ds, String props) {
    Connection conn = null;
    try {
        conn = ds.getConnection();
        DatabaseMetaData meta = conn.getMetaData();
        String dclass = dictionaryClassForString(meta.getDatabaseProductName(), conf);
        if (dclass == null)
            dclass = dictionaryClassForString(getProtocol(meta.getURL()), conf);
        if (dclass == null)
            dclass = DBDictionary.class.getName();
        return newDBDictionary(conf, dclass, props, conn);
    } catch (SQLException se) {
        throw new StoreException(se).setFatal(true);
    } finally {
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException se) {
            }
    }
}

From source file:org.apache.openjpa.jdbc.sql.DBDictionaryFactory.java

/**
 * Create the dictionary using the given class name and properties; the
 * connection may be null if not supplied to the factory.
 *//*from   w w  w  . ja v  a 2s .  co m*/
private static DBDictionary newDBDictionary(JDBCConfiguration conf, String dclass, String props,
        Connection conn) {
    DBDictionary dict = null;
    try {
        Class<?> c = Class.forName(dclass, true,
                AccessController.doPrivileged(J2DoPrivHelper.getClassLoaderAction(DBDictionary.class)));
        dict = (DBDictionary) AccessController.doPrivileged(J2DoPrivHelper.newInstanceAction(c));
    } catch (ClassNotFoundException cnfe) {
        // if the dictionary was not found, make another attempt
        // at loading the dictionary using the current thread.
        try {
            Class<?> c = Thread.currentThread().getContextClassLoader().loadClass(dclass);
            dict = (DBDictionary) AccessController.doPrivileged(J2DoPrivHelper.newInstanceAction(c));
        } catch (Exception e) {
            if (e instanceof PrivilegedActionException)
                e = ((PrivilegedActionException) e).getException();
            throw new UserException(e).setFatal(true);
        }
    } catch (Exception e) {
        if (e instanceof PrivilegedActionException)
            e = ((PrivilegedActionException) e).getException();
        throw new UserException(e).setFatal(true);
    }

    // warn if we could not locate the appropriate dictionary
    Log log = conf.getLog(JDBCConfiguration.LOG_JDBC);
    if (log.isWarnEnabled() && dict.getClass() == DBDictionary.class)
        log.warn(_loc.get("warn-generic"));

    if (log.isInfoEnabled()) {
        String infoString = "";
        if (conn != null) {
            try {
                DatabaseMetaData meta = conn.getMetaData();
                infoString = " (" + meta.getDatabaseProductName() + " " + meta.getDatabaseProductVersion()
                        + " ," + meta.getDriverName() + " " + meta.getDriverVersion() + ")";
            } catch (SQLException se) {
                if (log.isTraceEnabled())
                    log.trace(se.toString(), se);
            }
        }

        log.info(_loc.get("using-dict", dclass, infoString));
    }

    // set the dictionary's metadata
    Configurations.configureInstance(dict, conf, props, "DBDictionary");
    if (conn != null) {
        try {
            dict.connectedConfiguration(conn);
        } catch (SQLException se) {
            throw new StoreException(se).setFatal(true);
        }
    }
    return dict;
}