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:cz.jirutka.spring.data.jdbc.sql.SQL2008SqlGenerator.java

@Override
public boolean isCompatible(DatabaseMetaData metadata) throws SQLException {
    String productName = metadata.getDatabaseProductName();
    int majorVersion = metadata.getDatabaseMajorVersion();

    return "Apache Derby".equals(productName) || "Oracle".equals(productName) && majorVersion >= 12
            || "Microsoft SQL Server".equals(productName) && majorVersion >= 11; // >= 2012
}

From source file:cz.jirutka.spring.data.jdbc.sql.LimitOffsetSqlGenerator.java

@Override
public boolean isCompatible(DatabaseMetaData metadata) throws SQLException {
    return SUPPORTED_PRODUCTS.contains(metadata.getDatabaseProductName());
}

From source file:fi.luontola.cqrshotel.JdbcConfiguration.java

@PostConstruct
public void statusReport() throws SQLException {
    try (Connection connection = DataSourceUtils.getConnection(dataSource)) {
        DatabaseMetaData metaData = connection.getMetaData();
        log.info("Database: {} {}", metaData.getDatabaseProductName(), metaData.getDatabaseProductVersion());
        log.info("User: {}", metaData.getUserName());
        log.info("Connection URL: {} (configuration was {})", metaData.getURL(), dataSourceProperties.getUrl());
        log.info("Flyway locations: {}", flywayProperties.getLocations());
    }//from  w  w w . ja v  a 2  s .  c om
}

From source file:de.iritgo.aktario.jdbc.GetDatabaseVersion.java

/**
 * Perform the command./*from   w w w .  j a  v  a  2 s . c o m*/
 *
 * @return The database name and version.
 */
public Object performWithResult() {
    JDBCManager jdbcManager = (JDBCManager) Engine.instance().getManager("persist.JDBCManager");
    DataSource dataSource = jdbcManager.getDefaultDataSource();

    Connection connection = null;

    try {
        if (version == null) {
            connection = dataSource.getConnection();

            DatabaseMetaData meta = connection.getMetaData();

            version = meta.getDatabaseProductName() + " " + meta.getDatabaseProductVersion();
        }

        return version;
    } catch (SQLException x) {
        Log.logError("persist", "Insert", "Unable to get database meta data: " + x);
    } finally {
        DbUtils.closeQuietly(connection);
    }

    return null;
}

From source file:web.mvc.servlets.MyServlet.java

private void fillDataFromDataSource(List<InfoItem> data, DriverManagerDataSource dataSource)
        throws SQLException {
    try (Connection connect = dataSource.getConnection()) {
        DatabaseMetaData metaData = connect.getMetaData();
        data.add(new InfoItem("database name", metaData.getDatabaseProductName()));
        data.add(new InfoItem("database version", metaData.getDatabaseProductVersion()));
        data.add(new InfoItem("driver name", metaData.getDriverName()));
        data.add(new InfoItem("driver version", metaData.getDriverVersion()));
    }//from  w ww . j ava 2 s.  co m
}

From source file:ru.runa.wfe.commons.dao.ConstantDao.java

public Integer getDatabaseVersion() throws Exception {
    // we won't handle connection error
    org.hibernate.classic.Session session = sessionFactory.getCurrentSession();
    Connection connection = session.connection();
    DatabaseMetaData metaData = connection.getMetaData();
    log.info("Running with " + metaData.getDatabaseProductName() + " " + metaData.getDatabaseProductVersion());
    try {/*  w w  w  . j ava 2s .c o m*/
        SQLQuery query = session.createSQLQuery("SELECT VALUE FROM WFE_CONSTANTS WHERE NAME=:name");
        query.setString("name", DATABASE_VERSION_VARIABLE_NAME);
        return TypeConversionUtil.convertTo(Integer.class, query.uniqueResult());
    } catch (Exception e) {
        log.warn("Unable to get database version", e);
        return null;
    }
}

From source file:org.apache.hawq.pxf.plugins.jdbc.JdbcPlugin.java

protected Connection openConnection() throws ClassNotFoundException, SQLException {
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Open JDBC: driver=%s,url=%s,user=%s,pass=%s,table=%s", jdbcDriver, dbUrl, user,
                pass, tblName));/*from   w w  w .  j  a  v a 2s  . c  o  m*/
    }
    if (dbConn == null || dbConn.isClosed()) {
        Class.forName(jdbcDriver);
        if (user != null) {
            dbConn = DriverManager.getConnection(dbUrl, user, pass);
        } else {
            dbConn = DriverManager.getConnection(dbUrl);
        }
        DatabaseMetaData meta = dbConn.getMetaData();
        dbProduct = meta.getDatabaseProductName();
    }
    return dbConn;
}

From source file:nl.nn.adapterframework.jdbc.dbms.DbmsSupportFactory.java

public IDbmsSupport getDbmsSupport(Connection conn) {
    String product;/*from   ww  w  .j a  v  a  2s  .  co  m*/
    try {
        DatabaseMetaData md = conn.getMetaData();
        product = md.getDatabaseProductName();
    } catch (SQLException e1) {
        throw new RuntimeException("cannot obtain product from connection metadata", e1);
    }
    Properties supportMap = getDbmsSupportMap();
    if (supportMap != null) {
        if (StringUtils.isEmpty(product)) {
            log.warn("no product found from connection metadata");
        } else {
            if (!supportMap.containsKey(product)) {
                log.warn("product [" + product + "] not configured in dbmsSupportMap");
            } else {
                String dbmsSupportClass = supportMap.getProperty(product);
                if (StringUtils.isEmpty(dbmsSupportClass)) {
                    log.warn("product [" + product + "] configured empty in dbmsSupportMap");
                } else {
                    try {
                        if (log.isDebugEnabled())
                            log.debug("creating dbmsSupportClass [" + dbmsSupportClass + "] for product ["
                                    + product + "]");
                        return (IDbmsSupport) ClassUtils.newInstance(dbmsSupportClass);
                    } catch (Exception e) {
                        throw new RuntimeException("Cannot create dbmsSupportClass [" + dbmsSupportClass
                                + "] for product [" + product + "]", e);
                    }
                }
            }
        }
    } else {
        log.warn("no dbmsSupportMap specified, reverting to built in types");
        if (PRODUCT_NAME_ORACLE_.equals(product)) {
            log.debug("Setting databasetype to ORACLE");
            return new OracleDbmsSupport();
        }
        if (PRODUCT_NAME_MSSQLSERVER.equals(product)) {
            log.debug("Setting databasetype to MSSQLSERVER");
            return new MsSqlServerDbmsSupport();
        }
    }
    log.debug("Setting databasetype to GENERIC, productName [" + product + "]");
    return new GenericDbmsSupport();
}

From source file:com.bstek.bdf2.core.orm.jdbc.dialect.AbstractDialect.java

/**
 * ???/*from   www  .ja v a2 s. c om*/
 * @param connection ?
 * @param dbProductName ???
 * @param dbMajorVersion ??
 * @return ?????
 */
public boolean support(Connection connection, String dbProductName, String dbMajorVersion) {
    try {
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        String databaseProductName = databaseMetaData.getDatabaseProductName();
        int databaseMajorVersion = databaseMetaData.getDatabaseMajorVersion();
        boolean containsMysql = StringUtils.containsIgnoreCase(databaseProductName, dbProductName);
        if (StringUtils.isNotEmpty(dbMajorVersion)) {
            return containsMysql && Integer.valueOf(dbMajorVersion) == databaseMajorVersion;
        }
        return containsMysql;
    } catch (SQLException e) {
        return false;
    }
}

From source file:nl.b3p.gis.FeatureSchemaFactory.java

/**Creates a FeatureSchema from a table.
 * @param conn The connection to the database.
 * @param table The name of the table in the given database. From this table a FeatureSchema is created
 * @return a FeatureSchema//w  w  w.j  av  a  2  s  .c o m
 */
static public FeatureSchema createFeatureSchemaFromDbTable(Connection conn, String table,
        String[] dontAddColumns) throws SQLException, Exception {
    FeatureSchema fs = new FeatureSchema();
    List tableNames = SqlMetaDataUtils.getTableAndViewNames(conn);
    DatabaseMetaData dbmd = conn.getMetaData();
    if (tableNames.contains(table)) {
        ResultSet rs = dbmd.getColumns(null, null, table, null);
        for (int i = 0; rs.next(); i++) {
            String columnName = rs.getString("COLUMN_NAME");
            boolean add = true;
            if (dontAddColumns != null) {
                for (int d = 0; d < dontAddColumns.length && add; d++) {
                    if (dontAddColumns[d].equalsIgnoreCase(columnName)) {
                        add = false;
                    }
                }
            }
            if (add) {
                int dataType = rs.getInt("DATA_TYPE");
                AttributeType at = SqlMetaDataUtils.getAttributeTypeFromSqlType(dataType);
                //probably a Geom object, otherwise unknown type so don't add the attribute.
                if (at.equals(AttributeType.OBJECT)) {
                    if (dbmd.getDatabaseProductName().equalsIgnoreCase(SqlMetaDataUtils.PRODUCT_POSTGRES)) {
                        //the geom columns in postgis are stored in the "geometry_columns" table
                        PreparedStatement statement = null;
                        statement = conn.prepareStatement(
                                "SELECT * FROM geometry_columns g WHERE g.f_table_name = '" + table + "';");
                        ResultSet rsgeom = statement.executeQuery();
                        //if there is a geometry_columns record then this is a geometry
                        if (rsgeom.next()) {
                            int epsgCode = rsgeom.getInt("srid");
                            CoordinateSystem cs = new CoordinateSystem("EPSG:" + epsgCode, epsgCode, null);
                            fs.setCoordinateSystem(cs);

                            String type = rsgeom.getString("type");

                            fs.addAttribute(columnName, AttributeType.GEOMETRY);
                        }
                        statement.close();
                    } else {
                        log.info("create geometry feature not supported for " + dbmd.getDatabaseProductName());
                    }
                } else {
                    fs.addAttribute(columnName, at);
                }
            }
        }
        //set the coordinateSystem without the projection. Don't need it (yet)

    } else {
        log.error("Table " + table + " not found");
        throw new Exception("Table " + table + " not found");
    }
    return fs;
}