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:TestDatabaseMetaDataToolDatabaseInformation.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    DatabaseMetaData meta = conn.getMetaData();
    // Oracle (and some other vendors) do not support
    // some the following methods; therefore, we need
    // to use try-catch block.
    try {//from   ww w .jav a 2  s .  c  o m
        int majorVersion = meta.getDatabaseMajorVersion();
        System.out.println("major Version: " + majorVersion);
    } catch (Exception e) {
        System.out.println("major Version: unsupported feature");
    }

    try {
        int minorVersion = meta.getDatabaseMinorVersion();
        System.out.println("minorVersion" + minorVersion);
    } catch (Exception e) {
        System.out.println("minorVersion unsupported feature");
    }

    String productName = meta.getDatabaseProductName();
    String productVersion = meta.getDatabaseProductVersion();
    System.out.println("productName" + productName);
    System.out.println("productVersion" + productVersion);
    conn.close();
}

From source file:org.bonitasoft.engine.services.Vendor.java

/**
 * Get database vendor from databases metadatas
 *///from   w w w .j a  va 2s.c o m
public static Vendor fromDatabaseMetadata(DatabaseMetaData metadata) throws SQLException {
    if (metadata != null) {
        String productName = metadata.getDatabaseProductName();
        if (containsIgnoreCase(productName, "Oracle")) {
            return ORACLE;
        }
        if (containsIgnoreCase(productName, "Microsoft SQL Server")) {
            return SQLSERVER;
        }
    }
    return OTHER;
}

From source file:org.hyperic.hq.bizapp.server.session.SysStats.java

static Properties getDBStats(Connection connection) {
    Properties props = new Properties();
    DatabaseMetaData md;

    try {//from  www .  ja v  a2  s.c om
        md = connection.getMetaData();
        props.setProperty("hq.db.product.name", md.getDatabaseProductName());
        props.setProperty("hq.db.product.ver", md.getDatabaseProductVersion());
        props.setProperty("hq.db.driver.name", md.getDriverName());
        props.setProperty("hq.db.driver.ver", md.getDriverVersion());
    } catch (SQLException e) {
        _log.warn("Error get db stats");
        return props;
    }

    return props;
}

From source file:com.paladin.sys.db.DBManager.java

/**
 * init DataSource by spring// w  w  w.  ja  v  a 2s .  co  m
 */
private static final void initDataSourceFromXml(String _xmpPath, String _beanName) {
    try {
        ApplicationContext factory = new ClassPathXmlApplicationContext(new String[] { _xmpPath });
        dataSource = (DataSource) factory.getBean(_beanName);
        Connection conn = getConnection();
        DatabaseMetaData mdm = conn.getMetaData();
        log.info("Connected to " + mdm.getDatabaseProductName() + " " + mdm.getDatabaseProductVersion());
        closeConnection();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.apache.torque.adapter.AdapterFactory.java

/**
 * Creates a new instance of the Torque database adapter based on
 * the JDBC meta-data//  w w w.ja  va 2s  .  co m
 *
 * @param con a database connection
 * @return An instance of a Torque database adapter, or null if
 *         no adapter could be detected.
 * @throws InstantiationException if the adapter could not be
 *         instantiated
 * @throws SQLException if there are problems getting the JDBC meta data
 */
public static Adapter autoDetectAdapter(Connection con) throws InstantiationException, SQLException {
    DatabaseMetaData dmd = con.getMetaData();
    String dbmsName = dmd.getDatabaseProductName();

    Class<? extends Adapter> adapterClass = adapters.get(dbmsName);

    if (adapterClass == null) {
        throw new InstantiationException("Could not detect adapter for database: " + dbmsName);
    }

    log.info("Mapped database product " + dbmsName + " to adapter " + adapterClass.getSimpleName());

    try {
        Adapter adapter = adapterClass.newInstance();
        return adapter;
    } catch (IllegalAccessException e) {
        throw new InstantiationException("Could not instantiate adapter for database: " + dbmsName
                + ": Assure that adapter classes are in your classpath");
    }
}

From source file:org.kawanfw.test.util.SqlTestRunnerConsoleSpecial.java

/**
 * @param connection/*from w  w  w .ja  va2s. co  m*/
 * @throws Exception
 * @throws SQLException
 */
public static void testSqlEngine(Connection connection) throws Exception, SQLException {

    DatabaseMetaData databaseMetaData = connection.getMetaData();

    MessageDisplayer.display("");
    MessageDisplayer.display(
            "databaseMetaData.getDatabaseProductName()   : " + databaseMetaData.getDatabaseProductName());

    try {

        MessageDisplayer.display(
                "databaseMetaData.supportsStoredProcedures(): " + databaseMetaData.supportsStoredProcedures());

        MessageDisplayer.display("databaseMetaData.supportsStoredFunctionsUsingCallSyntax(): "
                + databaseMetaData.supportsStoredFunctionsUsingCallSyntax());

    } catch (Throwable e) {
        MessageDisplayer.display(e.toString());
    }

}

From source file:org.rhq.core.util.jdbc.JDBCUtil.java

/**
 * Generate the correct SQL statement to obtain the next value from a sequence/table
 * generator for the passed table. The passed connection gets closed in case of an error.
 * @param conn  A valid database connection
 * @param tableName The name of the table to use
 * @return A statement that obtains the next value for the passed table 
 *///  w w  w  . j  av  a2 s .c om
public static String getNextValSql(Connection conn, String tableName) {
    String nextvalSql;
    try {
        DatabaseMetaData meta = conn.getMetaData();
        String name = meta.getDatabaseProductName().toLowerCase();
        if (name.contains("postgres")) {
            nextvalSql = POSTGRES_NEXTVAL_SQL;
        } else if (name.contains("oracle")) {
            nextvalSql = ORACLE_NEXTVAL_SQL;
        } else if (name.contains("h2")) {
            nextvalSql = H2_NEXTVAL_SQL;
        } else {
            JDBCUtil.safeClose(conn);
            throw new IllegalStateException("Unsupported database type: " + name);
        }
    } catch (Exception e) {
        JDBCUtil.safeClose(conn);
        throw new IllegalStateException("Failed to determine database type.");
    }

    nextvalSql = String.format(nextvalSql, tableName);
    return nextvalSql;
}

From source file:com.pactera.edg.am.metamanager.extractor.util.GenSqlUtil.java

/**
 * ????Oracle?Teradata?SQL//from  w  w w  . ja v a 2  s .c  om
 * 
 * @return
 * @throws IOException
 *             ????Bean
 * @throws SQLException
 *             ?????
 */
private static Map<String, String> findDatabaseSQL() {
    Connection conn = null;
    String databaseName = instance.databaseName;
    try {
        if (databaseName == null) {
            DataSource dataSource = ExtractorContextLoader.getDataSource();
            conn = dataSource.getConnection();
            DatabaseMetaData meta = conn.getMetaData();
            instance.databaseName = meta.getDatabaseProductName();
            databaseName = instance.databaseName;
        }
        String location = instance.getLocation(databaseName);
        if (location == null) {
            throw new SQLFileNotLoadException("???" + databaseName
                    + "SQLspring/context-service.xmlGenSqlUtil");
        }

        if (instance.sqlHolder.get(databaseName) == null) {
            Map<String, String> sqls = readSQL(location);
            instance.sqlHolder.put(databaseName, sqls);
        }
        return instance.sqlHolder.get(databaseName);
    } catch (SQLException e) {
        databaseName = (databaseName == null) ? "Unkown" : databaseName;
        String s = "??[" + databaseName + "]SQL";
        log.error(s, e);
        throw new SQLFileNotLoadException(s, e);
    } catch (IOException e) {
        databaseName = (databaseName == null) ? "Unkown" : databaseName;
        String s = "??[" + databaseName + "]SQL";
        log.error(s, e);
        throw new SQLFileNotLoadException(s, e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:org.springframework.batch.support.DatabaseTypeTestUtils.java

public static DataSource getMockDataSource(String databaseProductName, String databaseVersion)
        throws Exception {
    DatabaseMetaData dmd = mock(DatabaseMetaData.class);
    DataSource ds = mock(DataSource.class);
    Connection con = mock(Connection.class);
    when(ds.getConnection()).thenReturn(con);
    when(con.getMetaData()).thenReturn(dmd);
    when(dmd.getDatabaseProductName()).thenReturn(databaseProductName);
    if (databaseVersion != null) {
        when(dmd.getDatabaseProductVersion()).thenReturn(databaseVersion);
    }//from   w w  w .  j  a  v a2s  .  c o  m
    return ds;
}

From source file:com.googlecode.flyway.core.dbsupport.DbSupportFactory.java

/**
 * Retrieves the name of the database product.
 *
 * @param connection The connection to use to query the database.
 * @return The name of the database product. Ex.: Oracle, MySQL, ...
 *///from  ww w . j  a va2 s. c om
private static String getDatabaseProductName(Connection connection) {
    try {
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        if (databaseMetaData == null) {
            throw new FlywayException("Unable to read database metadata while it is null!");
        }
        return databaseMetaData.getDatabaseProductName();
    } catch (SQLException e) {
        throw new FlywayException("Error while determining database product name", e);
    }
}