Search in sources :

Example 21 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project dubbo by alibaba.

the class DatabaseStatusChecker method check.

public Status check() {
    boolean ok;
    try {
        Connection connection = dataSource.getConnection();
        try {
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet resultSet = metaData.getTypeInfo();
            try {
                ok = resultSet.next();
            } finally {
                resultSet.close();
            }
            if (message == null) {
                message = metaData.getURL() + " (" + metaData.getDatabaseProductName() + " " + metaData.getDatabaseProductVersion() + ", " + getIsolation(metaData.getDefaultTransactionIsolation()) + ")";
            }
            if (version == 0) {
                version = metaData.getDatabaseMajorVersion();
            }
        } finally {
            connection.close();
        }
    } catch (Throwable e) {
        logger.error(e.getMessage(), e);
        ok = false;
    }
    return new Status(!ok ? Status.Level.ERROR : (version < 5 ? Status.Level.WARN : Status.Level.OK), message);
}
Also used : Status(com.alibaba.dubbo.common.status.Status) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 22 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project jOOQ by jOOQ.

the class JDBCUtils method dialect.

/**
     * "Guess" the {@link SQLDialect} from a {@link Connection} instance.
     * <p>
     * This method tries to guess the <code>SQLDialect</code> of a connection
     * from the its connection URL as obtained by
     * {@link DatabaseMetaData#getURL()}. If the dialect cannot be guessed from
     * the URL (e.g. when using an JDBC-ODBC bridge), further actions may be
     * implemented in the future.
     *
     * @see #dialect(String)
     */
public static final SQLDialect dialect(Connection connection) {
    SQLDialect result = SQLDialect.DEFAULT;
    if (connection != null) {
        try {
            DatabaseMetaData m = connection.getMetaData();
            String url = m.getURL();
            result = dialect(url);
        } catch (SQLException ignore) {
        }
    }
    if (result == SQLDialect.DEFAULT) {
    // If the dialect cannot be guessed from the URL, take some other
    // measures, e.g. by querying DatabaseMetaData.getDatabaseProductName()
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) SQLDialect(org.jooq.SQLDialect) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 23 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project Openfire by igniterealtime.

the class DbConnectionManager method setMetaData.

/**
     * Uses a connection from the database to set meta data information about
     * what different JDBC drivers and databases support.
     *
     * @param con the connection.
     * @throws SQLException if an SQL exception occurs.
     */
private static void setMetaData(Connection con) throws SQLException {
    DatabaseMetaData metaData = con.getMetaData();
    // Supports transactions?
    transactionsSupported = metaData.supportsTransactions();
    // Supports subqueries?
    subqueriesSupported = metaData.supportsCorrelatedSubqueries();
    // the method call.
    try {
        scrollResultsSupported = metaData.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
    } catch (Exception e) {
        scrollResultsSupported = false;
    }
    // Supports batch updates
    batchUpdatesSupported = metaData.supportsBatchUpdates();
    // Set defaults for other meta properties
    streamTextRequired = false;
    maxRowsSupported = true;
    fetchSizeSupported = true;
    // Get the database name so that we can perform meta data settings.
    String dbName = metaData.getDatabaseProductName().toLowerCase();
    String driverName = metaData.getDriverName().toLowerCase();
    // Oracle properties.
    if (dbName.indexOf("oracle") != -1) {
        databaseType = DatabaseType.oracle;
        streamTextRequired = true;
        scrollResultsSupported = false;
        // The i-net AUGURO JDBC driver
        if (driverName.indexOf("auguro") != -1) {
            streamTextRequired = false;
            fetchSizeSupported = true;
            maxRowsSupported = false;
        }
    } else // Postgres properties
    if (dbName.indexOf("postgres") != -1) {
        databaseType = DatabaseType.postgresql;
        // Postgres blows, so disable scrolling result sets.
        scrollResultsSupported = false;
        fetchSizeSupported = false;
    } else // Interbase properties
    if (dbName.indexOf("interbase") != -1) {
        databaseType = DatabaseType.interbase;
        fetchSizeSupported = false;
        maxRowsSupported = false;
    } else // SQLServer
    if (dbName.indexOf("sql server") != -1) {
        databaseType = DatabaseType.sqlserver;
        // JDBC driver i-net UNA properties
        if (driverName.indexOf("una") != -1) {
            fetchSizeSupported = true;
            maxRowsSupported = false;
        }
    } else // MySQL properties
    if (dbName.indexOf("mysql") != -1) {
        databaseType = DatabaseType.mysql;
        transactionsSupported = false;
    /* TODO comment and test this, it should be supported since 5.0 */
    } else // HSQL properties
    if (dbName.indexOf("hsql") != -1) {
        databaseType = DatabaseType.hsqldb;
    // scrollResultsSupported = false; /* comment and test this, it should be supported since 1.7.2 */
    } else // DB2 properties.
    if (dbName.indexOf("db2") != 1) {
        databaseType = DatabaseType.db2;
    }
}
Also used : DatabaseMetaData(java.sql.DatabaseMetaData) MissingResourceException(java.util.MissingResourceException) SQLException(java.sql.SQLException)

Example 24 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project jfinal by jfinal.

the class DataDictionaryGenerator method rebuildColumnMetas.

protected void rebuildColumnMetas(List<TableMeta> tableMetas) {
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        DatabaseMetaData dbMeta = conn.getMetaData();
        for (TableMeta tableMeta : tableMetas) {
            // 重建整个 TableMeta.columnMetas
            tableMeta.columnMetas = new ArrayList<ColumnMeta>();
            // 通过查看 dbMeta.getColumns(...) 源码注释,还可以获取到更多 meta data
            ResultSet rs = dbMeta.getColumns(conn.getCatalog(), null, tableMeta.name, null);
            while (rs.next()) {
                ColumnMeta columnMeta = new ColumnMeta();
                // 名称
                columnMeta.name = rs.getString("COLUMN_NAME");
                // 类型
                columnMeta.type = rs.getString("TYPE_NAME");
                if (columnMeta.type == null) {
                    columnMeta.type = "";
                }
                // 长度
                int columnSize = rs.getInt("COLUMN_SIZE");
                if (columnSize > 0) {
                    columnMeta.type = columnMeta.type + "(" + columnSize;
                    // 小数位数
                    int decimalDigits = rs.getInt("DECIMAL_DIGITS");
                    if (decimalDigits > 0) {
                        columnMeta.type = columnMeta.type + "," + decimalDigits;
                    }
                    columnMeta.type = columnMeta.type + ")";
                }
                // 是否允许 NULL 值
                columnMeta.isNullable = rs.getString("IS_NULLABLE");
                if (columnMeta.isNullable == null) {
                    columnMeta.isNullable = "";
                }
                columnMeta.isPrimaryKey = "   ";
                String[] keys = tableMeta.primaryKey.split(",");
                for (String key : keys) {
                    if (key.equalsIgnoreCase(columnMeta.name)) {
                        columnMeta.isPrimaryKey = "PRI";
                        break;
                    }
                }
                // 默认值
                columnMeta.defaultValue = rs.getString("COLUMN_DEF");
                if (columnMeta.defaultValue == null) {
                    columnMeta.defaultValue = "";
                }
                // 备注
                columnMeta.remarks = rs.getString("REMARKS");
                if (columnMeta.remarks == null) {
                    columnMeta.remarks = "";
                }
                if (tableMeta.colNameMaxLen < columnMeta.name.length()) {
                    tableMeta.colNameMaxLen = columnMeta.name.length();
                }
                if (tableMeta.colTypeMaxLen < columnMeta.type.length()) {
                    tableMeta.colTypeMaxLen = columnMeta.type.length();
                }
                if (tableMeta.colDefaultValueMaxLen < columnMeta.defaultValue.length()) {
                    tableMeta.colDefaultValueMaxLen = columnMeta.defaultValue.length();
                }
                tableMeta.columnMetas.add(columnMeta);
            }
            rs.close();
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                LogKit.error(e.getMessage(), e);
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 25 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project head by mifos.

the class SystemInformationServiceFacadeWebTier method getServerInformation.

@Override
public String getServerInformation(ServletContext context, Locale locale) {
    try {
        DatabaseMetaData metaData = StaticHibernateUtil.getSessionTL().connection().getMetaData();
        final SystemInfo systemInfo = new SystemInfo(metaData, context, locale, true);
        return systemInfo.getApplicationServerInfo();
    } catch (HibernateException e) {
        throw new MifosRuntimeException(e);
    } catch (SQLException e) {
        throw new MifosRuntimeException(e);
    }
}
Also used : SystemInfo(org.mifos.application.admin.system.SystemInfo) HibernateException(org.hibernate.HibernateException) SQLException(java.sql.SQLException) DatabaseMetaData(java.sql.DatabaseMetaData) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Aggregations

DatabaseMetaData (java.sql.DatabaseMetaData)299 ResultSet (java.sql.ResultSet)176 Connection (java.sql.Connection)139 SQLException (java.sql.SQLException)116 Test (org.junit.Test)81 ResultSetMetaData (java.sql.ResultSetMetaData)41 Statement (java.sql.Statement)36 ArrayList (java.util.ArrayList)33 PreparedStatement (java.sql.PreparedStatement)29 Properties (java.util.Properties)24 PhoenixDatabaseMetaData (org.apache.phoenix.jdbc.PhoenixDatabaseMetaData)16 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)15 IOException (java.io.IOException)14 HashMap (java.util.HashMap)11 DataSource (javax.sql.DataSource)9 HashSet (java.util.HashSet)8 List (java.util.List)8 Savepoint (java.sql.Savepoint)6 GargoyleException (com.kyj.fx.voeditor.visual.exceptions.GargoyleException)5 TreeSet (java.util.TreeSet)5