Example usage for java.sql DatabaseMetaData getDatabaseProductVersion

List of usage examples for java.sql DatabaseMetaData getDatabaseProductVersion

Introduction

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

Prototype

String getDatabaseProductVersion() throws SQLException;

Source Link

Document

Retrieves the version number of this database product.

Usage

From source file:org.talend.metadata.managment.model.DBConnectionFillerImpl.java

@Override
public DatabaseConnection fillUIConnParams(IMetadataConnection metadataBean, DatabaseConnection connection) {
    DatabaseConnection newConnection = null;
    if (connection == null) {
        newConnection = ConnectionFactory.eINSTANCE.createDatabaseConnection();
    }/*w w w  . ja va 2s.co  m*/
    if (super.fillUIConnParams(metadataBean, newConnection == null ? connection : newConnection) == null) {
        return null;
    }
    DatabaseConnection dbconn = null;
    if (newConnection != null) {
        dbconn = newConnection;
    } else {
        dbconn = connection;
    }
    if (newConnection != null && dbconn != null) {
        dbconn.setDriverJarPath(metadataBean.getDriverJarPath());
        dbconn.setProductId(metadataBean.getProduct());
        dbconn.setDbmsId(metadataBean.getMapping());
        dbconn.setAdditionalParams(metadataBean.getAdditionalParams());
        dbconn.setDriverClass(metadataBean.getDriverClass());
        dbconn.setDatabaseType(metadataBean.getDbType());
        dbconn.setName(metadataBean.getLabel());
        dbconn.setLabel(metadataBean.getLabel());
        dbconn.setVersion(metadataBean.getVersion());
        // MOD klliu bug 21074 2011-05-19
        dbconn.setUiSchema(metadataBean.getUiSchema());
        dbconn.setSQLMode(metadataBean.isSqlMode());
        dbconn.setSID(metadataBean.getDatabase());
        // MOD copy parameter from metadata to dbconn
        for (Map.Entry<String, Object> parameter : metadataBean.getOtherParameters().entrySet()) {
            dbconn.getParameters().put(parameter.getKey(),
                    parameter.getValue() == null ? StringUtils.EMPTY : parameter.getValue().toString());
        }
    }
    java.sql.Connection sqlConnection = null;
    try {
        sqlConnection = MetadataConnectionUtils.createConnection(metadataBean).getObject();
        // MetadataConnectionUtils.setMetadataCon(metadataBean);
        // fill some base parameter
        if (newConnection != null) {
            fillMetadataParams(metadataBean, newConnection);
        }

        DatabaseMetaData dbMetadata = null;
        // Added by Marvin Wang on Mar. 13, 2013 for loading hive jars dynamically, refer to TDI-25072.
        if (EDatabaseTypeName.HIVE.getXmlName().equalsIgnoreCase(metadataBean.getDbType())) {
            dbMetadata = HiveConnectionManager.getInstance().extractDatabaseMetaData(metadataBean);
        } else {
            // software
            dbMetadata = ExtractMetaDataUtils.getInstance().getDatabaseMetaData(sqlConnection, dbconn, false);
        }

        if (dbMetadata != null) {
            // MOD sizhaoliu TDQ-6316 The 2 tagged values should be added for all database including Hive
            String productName = dbMetadata.getDatabaseProductName() == null ? PluginConstant.EMPTY_STRING
                    : dbMetadata.getDatabaseProductName();
            String productVersion = dbMetadata.getDatabaseProductVersion() == null ? PluginConstant.EMPTY_STRING
                    : dbMetadata.getDatabaseProductVersion();
            TaggedValueHelper.setTaggedValue(dbconn, TaggedValueHelper.DB_PRODUCT_NAME, productName);
            TaggedValueHelper.setTaggedValue(dbconn, TaggedValueHelper.DB_PRODUCT_VERSION, productVersion);

            boolean isHive = dbconn.getDatabaseType().equals(EDatabaseTypeName.HIVE.getDisplayName());
            boolean isHiveJdbc = dbconn.getDatabaseType()
                    .equals(EDatabaseTypeName.GENERAL_JDBC.getDisplayName()) && dbconn.getDriverClass() != null
                    && dbconn.getDriverClass().equals(EDatabase4DriverClassName.HIVE.getDriverClass());
            boolean isImpala = dbconn.getDatabaseType().equals(EDatabaseTypeName.IMPALA.getDisplayName());
            boolean isImpalaJdbc = dbconn.getDatabaseType().equals(EDatabaseTypeName.IMPALA.getDisplayName())
                    && dbconn.getDriverClass() != null
                    && dbconn.getDriverClass().equals(EDatabase4DriverClassName.IMPALA.getDriverClass());
            if (!isHive && !isHiveJdbc && !isImpala && !isImpalaJdbc) {
                String identifierQuote = dbMetadata.getIdentifierQuoteString();
                ConnectionHelper.setIdentifierQuoteString(identifierQuote == null ? "" : identifierQuote, //$NON-NLS-1$
                        dbconn);
            }
        }
    } catch (SQLException e) {
        log.error(e, e);
    } catch (ClassNotFoundException e) {
        CommonExceptionHandler.process(e);
    } catch (InstantiationException e) {
        CommonExceptionHandler.process(e);
    } catch (IllegalAccessException e) {
        CommonExceptionHandler.process(e);
    } finally {
        if (sqlConnection != null) {
            ConnectionUtils.closeConnection(sqlConnection);
        }
        if (driver != null && MetadataConnectionUtils.isDerbyRelatedDb(metadataBean.getDriverClass(),
                metadataBean.getDbType())) {
            try {
                driver.connect("jdbc:derby:;shutdown=true", null); //$NON-NLS-1$
            } catch (SQLException e) {
                // exception of shutdown success. no need to catch.
            }
        }
    }
    if (newConnection != null) {
        return newConnection;
    } else {
        return connection;
    }
}

From source file:org.kawanfw.sql.servlet.DatabaseMetaDataExecutor.java

/**
 * //from ww w .ja  v a2 s .c om
 * Calls a remote metadata method from the PC <br>
 * 
 * @throws IOException
 *             all network, etc. errors
 * @throws ClassNotFoundException
 * @throws IllegalAccessException
 * @throws InstantiationException
 * @throws NoSuchMethodException
 * @throws SecurityException
 * @throws InvocationTargetException
 * @throws IllegalArgumentException
 */
private void callMetaDataFunction(HttpServletRequest request, OutputStream out, Connection connection)
        throws SQLException, IOException, ClassNotFoundException, InstantiationException,
        IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException

{

    // The method name
    String methodName = request.getParameter(Parameter.METHOD_NAME);
    // methodName = HtmlConverter.fromHtml(methodName);

    // The parms name
    String paramsTypes = request.getParameter(Parameter.PARAMS_TYPES);
    String paramsValues = request.getParameter(Parameter.PARAMS_VALUES);

    // Make sure all values are not null and trimed

    methodName = this.getTrimValue(methodName);
    paramsTypes = this.getTrimValue(paramsTypes);
    paramsValues = this.getTrimValue(paramsValues);

    debug("actionInvokeRemoteMethod:methodName       : " + methodName);

    // paramsTypes = HtmlConverter.fromHtml(paramsTypes);
    // paramsValues = HtmlConverter.fromHtml(paramsValues);

    List<String> listParamsTypes = ListOfStringTransport.fromJson(paramsTypes);
    List<String> listParamsValues = ListOfStringTransport.fromJson(paramsValues);

    debug("actionInvokeRemoteMethod:listParamsTypes      : " + listParamsTypes);
    debug("actionInvokeRemoteMethod:listParamsValues     : " + listParamsValues);

    DatabaseMetaData databaseMetaData = connection.getMetaData();

    // Trap DatabaseMetaData.getTables() & DatabaseMetaData.getUDTs()
    // that have special array String[] or int[] parameters
    if (methodName.equals("getTables") || methodName.equals("getUDTs") || methodName.equals("getPrimaryKeys")) {
        DatabaseMetaDataSpecial databaseMetaDataSpecial = new DatabaseMetaDataSpecial(databaseMetaData,
                methodName, listParamsValues);
        ResultSet rs = databaseMetaDataSpecial.execute();
        dumpResultSetOnServletOutStream(rs);
        return;
    }

    @SuppressWarnings("rawtypes")
    Class[] argTypes = new Class[listParamsTypes.size()];
    Object[] values = new Object[listParamsValues.size()];

    for (int i = 0; i < listParamsTypes.size(); i++) {
        String value = listParamsValues.get(i);

        String javaType = listParamsTypes.get(i);
        JavaValueBuilder javaValueBuilder = new JavaValueBuilder(javaType, value);

        argTypes[i] = javaValueBuilder.getClassOfValue();
        values[i] = javaValueBuilder.getValue();

        // Trap NULL values
        if (values[i].equals("NULL")) {
            values[i] = null;
        }

        debug("argTypes[i]: " + argTypes[i]);
        debug("values[i]  : " + values[i]);
    }

    Class<?> c = Class.forName("java.sql.DatabaseMetaData");
    Object theObject = databaseMetaData;

    // Invoke the method
    Method main = null;
    Object resultObj = null;

    // Get the Drvier Info
    String database = "";
    String productVersion = "";
    String DriverName = "";
    String DriverVersion = "";
    String driverInfo = Tag.PRODUCT;

    // try {
    // database = databaseMetaData.getDatabaseProductName();
    // productVersion = databaseMetaData.getDatabaseProductVersion();
    // DriverName = databaseMetaData.getDriverName();
    // DriverVersion= databaseMetaData.getDriverVersion();
    // driverInfo += database + " " + productVersion + " " + DriverName +
    // " " + DriverVersion;
    // } catch (Exception e1) {
    // ServerLogger.getLogger().log(Level.WARNING, Tag.PRODUCT +
    // "Impossible to get User Driver info.");
    // }

    database = databaseMetaData.getDatabaseProductName();
    productVersion = databaseMetaData.getDatabaseProductVersion();
    DriverName = databaseMetaData.getDriverName();
    DriverVersion = databaseMetaData.getDriverVersion();
    driverInfo += database + " " + productVersion + " " + DriverName + " " + DriverVersion;

    String methodParams = getMethodParams(values);

    try {
        main = c.getDeclaredMethod(methodName, argTypes);
    } catch (SecurityException e) {
        throw new SecurityException(driverInfo + " - Security - Impossible to get declared DatabaseMetaData."
                + methodName + "(" + methodParams + ")");
    } catch (NoSuchMethodException e) {
        throw new NoSuchMethodException(
                driverInfo + " - No Such Method - Impossible get declared DatabaseMetaData." + methodName + "("
                        + methodParams + ")");
    }

    try {
        resultObj = main.invoke(theObject, values);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(
                driverInfo + " - Impossible to call DatabaseMetaData." + methodName + "(" + methodParams + ")");
    } catch (IllegalAccessException e) {
        throw new IllegalAccessException(driverInfo + " - Impossible to access DatabaseMetaData method."
                + methodName + "(" + methodParams + ")");
    } catch (InvocationTargetException e) {
        throw new InvocationTargetException(e, driverInfo + " - Impossible to invoke DatabaseMetaData method."
                + methodName + "(" + methodParams + ")");
    }

    if (resultObj instanceof ResultSet) {
        ResultSet rs = (ResultSet) resultObj;
        dumpResultSetOnServletOutStream(rs);

    } else {
        // All other formats are handled in String
        String result = null;
        if (resultObj != null)
            result = resultObj.toString();
        debug("actionInvokeRemoteMethod:result: " + result);
        result = HtmlConverter.toHtml(result);

        //out.println(TransferStatus.SEND_OK);
        //out.println(result);
        ServerSqlManager.writeLine(out, TransferStatus.SEND_OK);
        ServerSqlManager.writeLine(out, result);
    }

}

From source file:jef.database.DbMetaData.java

/**
 * ??//from w  ww. ja v a  2  s. c  om
 * 
 * @return Map<String,String> [key] is
 *         <ul>
 *         <li>DatabaseProductName</li>
 *         <li>DatabaseProductVersion</li>
 *         <li>DriverName</li>
 *         <li>DriverVersion</li>
 *         </ul>
 */
public Map<String, String> getDbVersion() throws SQLException {
    Connection conn = getConnection(false);
    DatabaseMetaData databaseMetaData = conn.getMetaData();
    Map<String, String> map = new SimpleMap<String, String>();
    map.put("DriverName", databaseMetaData.getDriverName());
    map.put("DriverVersion",
            databaseMetaData.getDriverVersion() + " " + databaseMetaData.getDatabaseMinorVersion());
    map.put("DatabaseProductName", databaseMetaData.getDatabaseProductName());
    map.put("DatabaseProductVersion",
            databaseMetaData.getDatabaseProductVersion() + " " + databaseMetaData.getDatabaseMinorVersion());

    String otherVersionSQL = info.profile.getProperty(DbProperty.OTHER_VERSION_SQL);
    if (otherVersionSQL != null) {
        for (String sql : StringUtils.split(otherVersionSQL, ";")) {
            if (StringUtils.isBlank(sql))
                continue;
            Statement st = conn.createStatement();
            ResultSet rs = null;
            try {
                rs = st.executeQuery(sql);
                while (rs.next()) {
                    map.put(rs.getString(1), rs.getString(2));
                }
            } finally {
                DbUtils.close(rs);
                DbUtils.close(st);
            }
        }
    }
    releaseConnection(conn);
    return map;
}

From source file:de.innovationgate.webgate.api.jdbc.WGDatabaseImpl.java

public String getServerName() throws WGAPIException {

    try {//from w  w  w .j a  v  a2 s .c  om
        DatabaseMetaData metaData = getSession().doReturningWork(new ReturningWork<DatabaseMetaData>() {
            public DatabaseMetaData execute(Connection connection) throws SQLException {
                return connection.getMetaData();
            }
        });
        return metaData.getDatabaseProductName() + " " + metaData.getDatabaseProductVersion();
    } catch (HibernateException e) {
        throw new WGBackendException("Error retrieving server name", e);
    } catch (SQLException e) {
        throw new WGBackendException("Error retrieving server name", e);
    }
}

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

/**
 * This method is called when the dictionary first sees any connection.
 * It is used to initialize dictionary metadata if needed. If you
 * override this method, be sure to call
 * <code>super.connectedConfiguration</code>.
 *//*  w w w  .j av a2 s.  c o  m*/
public void connectedConfiguration(Connection conn) throws SQLException {
    if (!connected) {
        DatabaseMetaData metaData = null;
        try {
            metaData = conn.getMetaData();

            databaseProductName = nullSafe(metaData.getDatabaseProductName());
            databaseProductVersion = nullSafe(metaData.getDatabaseProductVersion());
            setMajorVersion(metaData.getDatabaseMajorVersion());
            setMinorVersion(metaData.getDatabaseMinorVersion());
            try {
                // JDBC3-only method, so it might throw an
                // AbstractMethodError
                int JDBCMajorVersion = metaData.getJDBCMajorVersion();
                isJDBC3 = JDBCMajorVersion >= 3;
                isJDBC4 = JDBCMajorVersion >= 4;
            } catch (Throwable t) {
                // ignore if not JDBC3
            }
        } catch (Exception e) {
            if (log.isTraceEnabled())
                log.trace(e.toString(), e);
        }

        if (log.isTraceEnabled()) {
            log.trace(DBDictionaryFactory.toString(metaData));

            if (isJDBC3) {
                try {
                    log.trace(_loc.get("connection-defaults", new Object[] { conn.getAutoCommit(),
                            conn.getHoldability(), conn.getTransactionIsolation() }));
                } catch (Throwable t) {
                    log.trace("Unable to trace connection settings", t);
                }
            }
        }

        // Configure the naming utility
        if (supportsDelimitedIdentifiers == null) // not explicitly set
            configureNamingUtil(metaData);

        // Auto-detect generated keys retrieval support unless user specified it.
        if (supportsGetGeneratedKeys == null) {
            supportsGetGeneratedKeys = (isJDBC3) ? metaData.supportsGetGeneratedKeys() : false;
        }
        if (log.isInfoEnabled()) {
            log.info(_loc.get("dict-info", new Object[] { metaData.getDatabaseProductName(), getMajorVersion(),
                    getMinorVersion(), metaData.getDriverName(), metaData.getDriverVersion() }));
        }
    }
    connected = true;
}