List of usage examples for java.sql DatabaseMetaData getDatabaseProductName
String getDatabaseProductName() throws SQLException;
From source file:org.jumpmind.db.platform.JdbcDatabasePlatformFactory.java
protected static String[] determineDatabaseNameVersionSubprotocol(DataSource dataSource) { Connection connection = null; String[] nameVersion = new String[3]; try {/* w w w . j av a 2 s .c o m*/ connection = dataSource.getConnection(); DatabaseMetaData metaData = connection.getMetaData(); nameVersion[0] = metaData.getDatabaseProductName(); nameVersion[1] = Integer.toString(metaData.getDatabaseMajorVersion()); final String PREFIX = "jdbc:"; String url = metaData.getURL(); if (StringUtils.isNotBlank(url) && url.length() > PREFIX.length()) { url = url.substring(PREFIX.length()); if (url.indexOf(":") > 0) { url = url.substring(0, url.indexOf(":")); } } nameVersion[2] = url; /* * if the productName is PostgreSQL, it could be either PostgreSQL * or Greenplum */ /* query the metadata to determine which one it is */ if (nameVersion[0].equalsIgnoreCase("PostgreSql")) { if (isGreenplumDatabase(connection)) { nameVersion[0] = DatabaseNamesConstants.GREENPLUM; nameVersion[1] = Integer.toString(getGreenplumVersion(connection)); } else if (isRedshiftDatabase(connection)) { nameVersion[0] = DatabaseNamesConstants.REDSHIFT; } } /* * if the productName is MySQL, it could be either MysSQL or MariaDB * query the metadata to determine which one it is */ if (nameVersion[0].equalsIgnoreCase(DatabaseNamesConstants.MYSQL)) { if (isMariaDBDatabase(connection)) { nameVersion[0] = DatabaseNamesConstants.MARIADB; } } if (nameVersion[0].toLowerCase().indexOf(DatabaseNamesConstants.DB2) != -1) { if (nameVersion[0].toUpperCase().indexOf("Z") != -1) { nameVersion[0] = DatabaseNamesConstants.DB2ZOS; } else if (nameVersion[0].indexOf("400") != -1) { nameVersion[0] = DatabaseNamesConstants.DB2AS400; } else { nameVersion[0] = DatabaseNamesConstants.DB2; } } if (nameVersion[0].equalsIgnoreCase("AS") && nameVersion[2].equalsIgnoreCase("db2")) { nameVersion[0] = DatabaseNamesConstants.DB2AS400; } if (nameVersion[0].toLowerCase().startsWith(DatabaseNamesConstants.FIREBIRD)) { if (isFirebirdDialect1(connection)) { nameVersion[0] = DatabaseNamesConstants.FIREBIRD_DIALECT1; } } log.info("Detected database '" + nameVersion[0] + "', version '" + nameVersion[1] + "', protocol '" + nameVersion[2] + "'"); return nameVersion; } catch (SQLException ex) { throw new SqlException("Error while reading the database metadata: " + ex.getMessage(), ex); } finally { if (connection != null) { try { connection.close(); } catch (SQLException ex) { // we ignore this one } } } }
From source file:org.hyperic.util.jdbc.DBUtil.java
/** * Given a Connection object, this method returns a constant indicating what * type of database the Connection is connected to. * // w w w. ja va 2 s . co m * @param conn The connection whose database type the caller wished to * ascertain. * @return One of the DATABASE_XXX constants defined in this class. */ public static int getDBType(Connection conn) throws SQLException { Class connClass = conn.getClass(); Integer dbTypeInteger = (Integer) _dbTypes.get(connClass); int dbType = DATABASE_UNKNOWN; if (dbTypeInteger == null) { DatabaseMetaData dbMetaData = conn.getMetaData(); String dbName = dbMetaData.getDatabaseProductName().toLowerCase(); String dbVersion = dbMetaData.getDatabaseProductVersion().toLowerCase(); log.debug("getDBType: dbName='" + dbName + "', version='" + dbVersion + "'"); if (dbName.indexOf("postgresql") != -1) { if (dbVersion.startsWith("7.")) { dbType = DATABASE_POSTGRESQL_7; } else if (dbVersion.startsWith("8.")) { dbType = DATABASE_POSTGRESQL_8; } else if (dbVersion.startsWith("9.")) { dbType = DATABASE_POSTGRESQL_9; } } else if (dbName.indexOf("oracle") != -1) { if (dbVersion.startsWith("oracle8")) { dbType = DATABASE_ORACLE_8; } else if (dbVersion.startsWith("oracle9")) { dbType = DATABASE_ORACLE_9; } else if (dbVersion.startsWith("oracle database 10g")) { dbType = DATABASE_ORACLE_10; } else if (dbVersion.startsWith("oracle database 11g")) { dbType = DATABASE_ORACLE_11; } } else if (dbName.indexOf("mysql") != -1) { dbType = DATABASE_MYSQL5; } _dbTypes.put(connClass, new Integer(dbType)); } else { dbType = dbTypeInteger.intValue(); } return dbType; }
From source file:org.springframework.jdbc.core.StatementCreatorUtils.java
/** * Set the specified PreparedStatement parameter to null, * respecting database-specific peculiarities. *///w w w. j a va2s . com private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, @Nullable String typeName) throws SQLException { if (sqlType == SqlTypeValue.TYPE_UNKNOWN || sqlType == Types.OTHER) { boolean useSetObject = false; Integer sqlTypeToUse = null; if (!shouldIgnoreGetParameterType) { try { sqlTypeToUse = ps.getParameterMetaData().getParameterType(paramIndex); } catch (SQLException ex) { if (logger.isDebugEnabled()) { logger.debug("JDBC getParameterType call failed - using fallback method instead: " + ex); } } } if (sqlTypeToUse == null) { // Proceed with database-specific checks sqlTypeToUse = Types.NULL; DatabaseMetaData dbmd = ps.getConnection().getMetaData(); String jdbcDriverName = dbmd.getDriverName(); String databaseProductName = dbmd.getDatabaseProductName(); if (databaseProductName.startsWith("Informix") || (jdbcDriverName.startsWith("Microsoft") && jdbcDriverName.contains("SQL Server"))) { // "Microsoft SQL Server JDBC Driver 3.0" versus "Microsoft JDBC Driver 4.0 for SQL Server" useSetObject = true; } else if (databaseProductName.startsWith("DB2") || jdbcDriverName.startsWith("jConnect") || jdbcDriverName.startsWith("SQLServer") || jdbcDriverName.startsWith("Apache Derby")) { sqlTypeToUse = Types.VARCHAR; } } if (useSetObject) { ps.setObject(paramIndex, null); } else { ps.setNull(paramIndex, sqlTypeToUse); } } else if (typeName != null) { ps.setNull(paramIndex, sqlType, typeName); } else { ps.setNull(paramIndex, sqlType); } }
From source file:org.wso2.extension.siddhi.store.rdbms.util.RDBMSTableUtils.java
/** * Utility method used for looking up DB metadata information from a given datasource. * * @param ds the datasource from which the metadata needs to be looked up. * @return a list of DB metadata.//from w w w . ja v a 2s .com */ public static Map<String, Object> lookupDatabaseInfo(DataSource ds) { Connection conn = null; try { conn = ds.getConnection(); DatabaseMetaData dmd = conn.getMetaData(); Map<String, Object> result = new HashMap<>(); result.put(DATABASE_PRODUCT_NAME, dmd.getDatabaseProductName()); result.put(VERSION, Double.parseDouble(dmd.getDatabaseMajorVersion() + "." + dmd.getDatabaseMinorVersion())); return result; } catch (SQLException e) { throw new RDBMSTableException("Error in looking up database type: " + e.getMessage(), e); } finally { cleanupConnection(null, null, conn); } }
From source file:com.trackplus.ddl.DataReader.java
private static void logDatabaseMetaDataInfo(DatabaseInfo databaseInfo, Connection connection) throws DDLException { DatabaseMetaData databaseMetaData = null; try {/* w ww . ja v a 2 s . c o m*/ databaseMetaData = connection.getMetaData(); int majorVersion = databaseMetaData.getDatabaseMajorVersion(); int minorVersion = databaseMetaData.getDatabaseMinorVersion(); String productName = databaseMetaData.getDatabaseProductName(); String productVersion = databaseMetaData.getDatabaseProductVersion(); int driverMajorVersion = databaseMetaData.getDriverMajorVersion(); int driverMinorVersion = databaseMetaData.getDriverMinorVersion(); LOGGER.debug("DB DRIVER=" + databaseInfo.getDriver()); LOGGER.debug("DB URL=" + databaseInfo.getUrl()); LOGGER.debug("DB USER=" + databaseInfo.getUser()); String password = databaseInfo.getPassword() == null ? null : databaseInfo.getPassword().replaceAll(".", "*"); LOGGER.debug("DB PASSWORD=" + password + "\n"); LOGGER.debug("DB majorVersion=" + majorVersion); LOGGER.debug("DB minorVersion=" + minorVersion); LOGGER.debug("DB productName=" + productName); LOGGER.debug("DB productVersion=" + productVersion); LOGGER.debug("DB driverMajorVersion=" + driverMajorVersion); LOGGER.debug("DB driverMinorVersion=" + driverMinorVersion); } catch (SQLException e) { throw new DDLException(e.getMessage(), e); } }
From source file:info.extensiblecatalog.OAIToolkit.db.DButil.java
public static String showInfo() { StringBuffer sb = new StringBuffer(); DatabaseMetaData meta = getMetadata(); Connection conn = null;//from ww w. j ava 2 s . com try { conn = getConnection(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } sb.append("Connection: "); if (null == conn) { sb.append(" (is null)"); } else { sb.append(" (is ok)"); } if (null == meta) { sb.append(", [unknown server]"); } else { try { sb.append(", server: " + meta.getDatabaseProductName() + " " + meta.getDatabaseProductVersion()); } catch (SQLException e) { e.printStackTrace(); prglog.error("[PRG] " + e); } } sb.append(", user: '" + user + "'"); sb.append(", password: '" + password + "'"); sb.append(", database: '" + database + "'"); return sb.toString(); }
From source file:org.diffkit.util.DKSqlUtil.java
public static Map<String, ?> getDatabaseInfo(Connection connection_) throws SQLException { if (connection_ == null) return null; DatabaseMetaData dbMeta = connection_.getMetaData(); if (dbMeta == null) return null; Map<String, Object> info = new HashMap<String, Object>(); info.put(DATABASE_MAJOR_VERSION_KEY, new Integer(dbMeta.getDatabaseMajorVersion())); info.put(DATABASE_MINOR_VERSION_KEY, new Integer(dbMeta.getDatabaseMinorVersion())); info.put(DATABASE_PRODUCT_NAME_KEY, dbMeta.getDatabaseProductName()); info.put(DATABASE_PRODUCT_VERSION_KEY, dbMeta.getDatabaseProductVersion()); return info;/* w ww .jav a 2s. c o m*/ }
From source file:net.bull.javamelody.internal.model.JavaInformations.java
private static void appendDataBaseVersion(StringBuilder result, Connection connection) throws SQLException { final DatabaseMetaData metaData = connection.getMetaData(); // Scurit: pour l'instant on n'indique pas metaData.getUserName() result.append(metaData.getURL()).append('\n'); result.append(metaData.getDatabaseProductName()).append(", ").append(metaData.getDatabaseProductVersion()) .append('\n'); result.append("Driver JDBC:\n").append(metaData.getDriverName()).append(", ") .append(metaData.getDriverVersion()); }
From source file:org.apache.openjpa.jdbc.sql.DBDictionaryFactory.java
/** * Return a string containing all the property values of the given * database metadata./*from www .j a va2s . com*/ */ public static String toString(DatabaseMetaData meta) throws SQLException { String lineSep = J2DoPrivHelper.getLineSeparator(); StringBuilder buf = new StringBuilder(4096); try { buf.append("catalogSeparator: ").append(meta.getCatalogSeparator()).append(lineSep) .append("catalogTerm: ").append(meta.getCatalogTerm()).append(lineSep) .append("databaseProductName: ").append(meta.getDatabaseProductName()).append(lineSep) .append("databaseProductVersion: ").append(meta.getDatabaseProductVersion()).append(lineSep) .append("driverName: ").append(meta.getDriverName()).append(lineSep).append("driverVersion: ") .append(meta.getDriverVersion()).append(lineSep).append("extraNameCharacters: ") .append(meta.getExtraNameCharacters()).append(lineSep).append("identifierQuoteString: ") .append(meta.getIdentifierQuoteString()).append(lineSep).append("numericFunctions: ") .append(meta.getNumericFunctions()).append(lineSep).append("procedureTerm: ") .append(meta.getProcedureTerm()).append(lineSep).append("schemaTerm: ") .append(meta.getSchemaTerm()).append(lineSep).append("searchStringEscape: ") .append(meta.getSearchStringEscape()).append(lineSep).append("sqlKeywords: ") .append(meta.getSQLKeywords()).append(lineSep).append("stringFunctions: ") .append(meta.getStringFunctions()).append(lineSep).append("systemFunctions: ") .append(meta.getSystemFunctions()).append(lineSep).append("timeDateFunctions: ") .append(meta.getTimeDateFunctions()).append(lineSep).append("url: ").append(meta.getURL()) .append(lineSep).append("userName: ").append(meta.getUserName()).append(lineSep) .append("defaultTransactionIsolation: ").append(meta.getDefaultTransactionIsolation()) .append(lineSep).append("driverMajorVersion: ").append(meta.getDriverMajorVersion()) .append(lineSep).append("driverMinorVersion: ").append(meta.getDriverMinorVersion()) .append(lineSep).append("maxBinaryLiteralLength: ").append(meta.getMaxBinaryLiteralLength()) .append(lineSep).append("maxCatalogNameLength: ").append(meta.getMaxCatalogNameLength()) .append(lineSep).append("maxCharLiteralLength: ").append(meta.getMaxCharLiteralLength()) .append(lineSep).append("maxColumnNameLength: ").append(meta.getMaxColumnNameLength()) .append(lineSep).append("maxColumnsInGroupBy: ").append(meta.getMaxColumnsInGroupBy()) .append(lineSep).append("maxColumnsInIndex: ").append(meta.getMaxColumnsInIndex()) .append(lineSep).append("maxColumnsInOrderBy: ").append(meta.getMaxColumnsInOrderBy()) .append(lineSep).append("maxColumnsInSelect: ").append(meta.getMaxColumnsInSelect()) .append(lineSep).append("maxColumnsInTable: ").append(meta.getMaxColumnsInTable()) .append(lineSep).append("maxConnections: ").append(meta.getMaxConnections()).append(lineSep) .append("maxCursorNameLength: ").append(meta.getMaxCursorNameLength()).append(lineSep) .append("maxIndexLength: ").append(meta.getMaxIndexLength()).append(lineSep) .append("maxProcedureNameLength: ").append(meta.getMaxProcedureNameLength()).append(lineSep) .append("maxRowSize: ").append(meta.getMaxRowSize()).append(lineSep) .append("maxSchemaNameLength: ").append(meta.getMaxSchemaNameLength()).append(lineSep) .append("maxStatementLength: ").append(meta.getMaxStatementLength()).append(lineSep) .append("maxStatements: ").append(meta.getMaxStatements()).append(lineSep) .append("maxTableNameLength: ").append(meta.getMaxTableNameLength()).append(lineSep) .append("maxTablesInSelect: ").append(meta.getMaxTablesInSelect()).append(lineSep) .append("maxUserNameLength: ").append(meta.getMaxUserNameLength()).append(lineSep) .append("isCatalogAtStart: ").append(meta.isCatalogAtStart()).append(lineSep) .append("isReadOnly: ").append(meta.isReadOnly()).append(lineSep) .append("nullPlusNonNullIsNull: ").append(meta.nullPlusNonNullIsNull()).append(lineSep) .append("nullsAreSortedAtEnd: ").append(meta.nullsAreSortedAtEnd()).append(lineSep) .append("nullsAreSortedAtStart: ").append(meta.nullsAreSortedAtStart()).append(lineSep) .append("nullsAreSortedHigh: ").append(meta.nullsAreSortedHigh()).append(lineSep) .append("nullsAreSortedLow: ").append(meta.nullsAreSortedLow()).append(lineSep) .append("storesLowerCaseIdentifiers: ").append(meta.storesLowerCaseIdentifiers()) .append(lineSep).append("storesLowerCaseQuotedIdentifiers: ") .append(meta.storesLowerCaseQuotedIdentifiers()).append(lineSep) .append("storesMixedCaseIdentifiers: ").append(meta.storesMixedCaseIdentifiers()) .append(lineSep).append("storesMixedCaseQuotedIdentifiers: ") .append(meta.storesMixedCaseQuotedIdentifiers()).append(lineSep) .append("storesUpperCaseIdentifiers: ").append(meta.storesUpperCaseIdentifiers()) .append(lineSep).append("storesUpperCaseQuotedIdentifiers: ") .append(meta.storesUpperCaseQuotedIdentifiers()).append(lineSep) .append("supportsAlterTableWithAddColumn: ").append(meta.supportsAlterTableWithAddColumn()) .append(lineSep).append("supportsAlterTableWithDropColumn: ") .append(meta.supportsAlterTableWithDropColumn()).append(lineSep) .append("supportsANSI92EntryLevelSQL: ").append(meta.supportsANSI92EntryLevelSQL()) .append(lineSep).append("supportsANSI92FullSQL: ").append(meta.supportsANSI92FullSQL()) .append(lineSep).append("supportsANSI92IntermediateSQL: ") .append(meta.supportsANSI92IntermediateSQL()).append(lineSep) .append("supportsCatalogsInDataManipulation: ") .append(meta.supportsCatalogsInDataManipulation()).append(lineSep) .append("supportsCatalogsInIndexDefinitions: ") .append(meta.supportsCatalogsInIndexDefinitions()).append(lineSep) .append("supportsCatalogsInPrivilegeDefinitions: ") .append(meta.supportsCatalogsInPrivilegeDefinitions()).append(lineSep) .append("supportsCatalogsInProcedureCalls: ").append(meta.supportsCatalogsInProcedureCalls()) .append(lineSep).append("supportsCatalogsInTableDefinitions: ") .append(meta.supportsCatalogsInTableDefinitions()).append(lineSep) .append("supportsColumnAliasing: ").append(meta.supportsColumnAliasing()).append(lineSep) .append("supportsConvert: ").append(meta.supportsConvert()).append(lineSep) .append("supportsCoreSQLGrammar: ").append(meta.supportsCoreSQLGrammar()).append(lineSep) .append("supportsCorrelatedSubqueries: ").append(meta.supportsCorrelatedSubqueries()) .append(lineSep).append("supportsDataDefinitionAndDataManipulationTransactions: ") .append(meta.supportsDataDefinitionAndDataManipulationTransactions()).append(lineSep) .append("supportsDataManipulationTransactionsOnly: ") .append(meta.supportsDataManipulationTransactionsOnly()).append(lineSep) .append("supportsDifferentTableCorrelationNames: ") .append(meta.supportsDifferentTableCorrelationNames()).append(lineSep) .append("supportsExpressionsInOrderBy: ").append(meta.supportsExpressionsInOrderBy()) .append(lineSep).append("supportsExtendedSQLGrammar: ") .append(meta.supportsExtendedSQLGrammar()).append(lineSep).append("supportsFullOuterJoins: ") .append(meta.supportsFullOuterJoins()).append(lineSep).append("supportsGroupBy: ") .append(meta.supportsGroupBy()).append(lineSep).append("supportsGroupByBeyondSelect: ") .append(meta.supportsGroupByBeyondSelect()).append(lineSep).append("supportsGroupByUnrelated: ") .append(meta.supportsGroupByUnrelated()).append(lineSep) .append("supportsIntegrityEnhancementFacility: ") .append(meta.supportsIntegrityEnhancementFacility()).append(lineSep) .append("supportsLikeEscapeClause: ").append(meta.supportsLikeEscapeClause()).append(lineSep) .append("supportsLimitedOuterJoins: ").append(meta.supportsLimitedOuterJoins()).append(lineSep) .append("supportsMinimumSQLGrammar: ").append(meta.supportsMinimumSQLGrammar()).append(lineSep) .append("supportsMixedCaseIdentifiers: ").append(meta.supportsMixedCaseIdentifiers()) .append(lineSep).append("supportsMixedCaseQuotedIdentifiers: ") .append(meta.supportsMixedCaseQuotedIdentifiers()).append(lineSep) .append("supportsMultipleResultSets: ").append(meta.supportsMultipleResultSets()) .append(lineSep).append("supportsMultipleTransactions: ") .append(meta.supportsMultipleTransactions()).append(lineSep) .append("supportsNonNullableColumns: ").append(meta.supportsNonNullableColumns()) .append(lineSep).append("supportsOpenCursorsAcrossCommit: ") .append(meta.supportsOpenCursorsAcrossCommit()).append(lineSep) .append("supportsOpenCursorsAcrossRollback: ").append(meta.supportsOpenCursorsAcrossRollback()) .append(lineSep).append("supportsOpenStatementsAcrossCommit: ") .append(meta.supportsOpenStatementsAcrossCommit()).append(lineSep) .append("supportsOpenStatementsAcrossRollback: ") .append(meta.supportsOpenStatementsAcrossRollback()).append(lineSep) .append("supportsOrderByUnrelated: ").append(meta.supportsOrderByUnrelated()).append(lineSep) .append("supportsOuterJoins: ").append(meta.supportsOuterJoins()).append(lineSep) .append("supportsPositionedDelete: ").append(meta.supportsPositionedDelete()).append(lineSep) .append("supportsPositionedUpdate: ").append(meta.supportsPositionedUpdate()).append(lineSep) .append("supportsSchemasInDataManipulation: ").append(meta.supportsSchemasInDataManipulation()) .append(lineSep).append("supportsSchemasInIndexDefinitions: ") .append(meta.supportsSchemasInIndexDefinitions()).append(lineSep) .append("supportsSchemasInPrivilegeDefinitions: ") .append(meta.supportsSchemasInPrivilegeDefinitions()).append(lineSep) .append("supportsSchemasInProcedureCalls: ").append(meta.supportsSchemasInProcedureCalls()) .append(lineSep).append("supportsSchemasInTableDefinitions: ") .append(meta.supportsSchemasInTableDefinitions()).append(lineSep) .append("supportsSelectForUpdate: ").append(meta.supportsSelectForUpdate()).append(lineSep) .append("supportsStoredProcedures: ").append(meta.supportsStoredProcedures()).append(lineSep) .append("supportsSubqueriesInComparisons: ").append(meta.supportsSubqueriesInComparisons()) .append(lineSep).append("supportsSubqueriesInExists: ") .append(meta.supportsSubqueriesInExists()).append(lineSep).append("supportsSubqueriesInIns: ") .append(meta.supportsSubqueriesInIns()).append(lineSep) .append("supportsSubqueriesInQuantifieds: ").append(meta.supportsSubqueriesInQuantifieds()) .append(lineSep).append("supportsTableCorrelationNames: ") .append(meta.supportsTableCorrelationNames()).append(lineSep).append("supportsTransactions: ") .append(meta.supportsTransactions()).append(lineSep).append("supportsUnion: ") .append(meta.supportsUnion()).append(lineSep).append("supportsUnionAll: ") .append(meta.supportsUnionAll()).append(lineSep).append("usesLocalFilePerTable: ") .append(meta.usesLocalFilePerTable()).append(lineSep).append("usesLocalFiles: ") .append(meta.usesLocalFiles()).append(lineSep).append("allProceduresAreCallable: ") .append(meta.allProceduresAreCallable()).append(lineSep).append("allTablesAreSelectable: ") .append(meta.allTablesAreSelectable()).append(lineSep) .append("dataDefinitionCausesTransactionCommit: ") .append(meta.dataDefinitionCausesTransactionCommit()).append(lineSep) .append("dataDefinitionIgnoredInTransactions: ") .append(meta.dataDefinitionIgnoredInTransactions()).append(lineSep) .append("doesMaxRowSizeIncludeBlobs: ").append(meta.doesMaxRowSizeIncludeBlobs()) .append(lineSep).append("supportsBatchUpdates: ").append(meta.supportsBatchUpdates()); } catch (Throwable t) { // maybe abstract method error for jdbc 3 metadata method, or // other error buf.append(lineSep).append("Caught throwable: ").append(t); } return buf.toString(); }
From source file:cz.jirutka.spring.data.jdbc.sql.Oracle9SqlGenerator.java
@Override public boolean isCompatible(DatabaseMetaData metadata) throws SQLException { return "Oracle".equals(metadata.getDatabaseProductName()); }