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);
}
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;
}
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;
}
}
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);
}
}
}
}
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);
}
}
Aggregations