DatabaseMetaData使用(读hibernate源代码)

xiaoxiao2021-03-01  22

try {Connection conn = connections.getConnection();try {DatabaseMetaData meta = conn.getMetaData();databaseName = meta.getDatabaseProductName();databaseMajorVersion = getDatabaseMajorVersion(meta);log.info("RDBMS: " + databaseName + ", version: " + meta.getDatabaseProductVersion() );log.info("JDBC driver: " + meta.getDriverName() + ", version: " + meta.getDriverVersion() );metaSupportsScrollable = meta.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);metaSupportsBatchUpdates = meta.supportsBatchUpdates();(判断是否使用批量操作)if ( Environment.jvmSupportsGetGeneratedKeys() ) {try {Boolean result = (Boolean) DatabaseMetaData.class.getMethod("supportsGetGeneratedKeys", null).invoke(meta, null);metaSupportsGetGeneratedKeys = result.booleanValue();}catch (AbstractMethodError ame) {metaSupportsGetGeneratedKeys = false;}catch (Exception e) {metaSupportsGetGeneratedKeys = false;}}}finally {connections.closeConnection(conn);}}catch (SQLException sqle) {log.warn("Could not obtain connection metadata", sqle);}catch (UnsupportedOperationException uoe) {// user supplied JDBC connections}

详细说明(转载) - http://www.leftworld.net/online/j2sedoc/javaref/java.sql.databasemetadata_dsc.htm

java.sql.DatabaseMetaData public interface DatabaseMetaData 该类提供了关于数据库的整体信息。 这里的许多方法将在 ResultSets 中返回信息列表。可以使用一般的 ResultSet 方法如 getString 和 getInt 从这些 ResultSet 中检索数据。如果给定的元数据格式是不可用的,这些方法将抛出一个 SQLException。 有些方法采用 String 模式的参数。这些参数都具有诸如 fooPattern 的名称。在一个模式字符串中,"%" 表示匹配任意由 0 个或多个字符组成的子字符串,"_" 表示匹配任意一个字符。只返回与搜索模式匹配的元数据项。如果将一个搜索模式参数设置为 null 引用,这意味着将从搜索中删除该参数的标准。 如果驱动程序不支持元数据方法,将抛出一个 SQLException。在方法返回一个 ResultSet 的情况下,将返回一个 ResultSet ( 它可能为空 ) 或者抛出一个 SQLException。
转载请注明原文地址: https://www.6miu.com/read-3449936.html

最新回复(0)