JDBC获取数据库表字段信息

本文介绍了两种通过JDBC获取数据库表字段信息的方法:使用DatabaseMetadata的getColumns()方法和ResultSetMetaData。getColumns()方法适用于多数数据库,但在Oracle中需要特殊配置。ResultSetMetaData则更为通用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

随着项目的需要,对于数据库支持要求越多越好,最好是Generic JDBC Connection。为此,笔者要求项目程序内只允许使用JDBC接口。在此条件下如何获取表的字段信息?有哪几种方式?都适用吗?

字段信息     

       字段在表里就是一个Column,关于ColumnJDBC里面有20多个参数来描述它,称为元数据,Metadata。包括:

1.                TABLE_CAT String => table catalog (may be null)

2.                TABLE_SCHEM String => table schema (may be null)

3.                TABLE_NAME String => table name

4.                COLUMN_NAME String => column name

5.                DATA_TYPE int => SQL type from java.sql.Types

6.                TYPE_NAME String => Data source dependent type name, for a UDT the type name is fully qualified

7.                COLUMN_SIZE int => column size.

8.                BUFFER_LENGTH is not used.

9.                DECIMAL_DIGITS int => the number of fractional digits. Null is returned for data types where DECIMAL_DIGITS is not applicable.

10.            NUM_PREC_RADIX int => Radix (typically either 10 or 2)

11.            NULLABLE int => is NULL allowed.

o                                            columnNoNulls - might not allow NULL values

o                                            columnNullable - definitely allows NULL values

o                                            columnNullableUnknown - nullability unknown

12.            REMARKS String => comment describing column (may be null)

13.            COLUMN_DEF String => default value for the column, which should be interpreted as a string when the value is enclosed in single quotes (may be null)

14.            ……

    通常关注的是COLUMN_NAME、DATA_TYPE、TYPE_NAME等。

getColumns()获取字段信息

       JDBC里有DatabaseMetadata接口,通过它可以拿到数据库的元数据,也就是数据库的相关描述信息。果然,getColumns()就可以拿到表所有的字段信息:

Connection conn = dbms.getConnection();

DatabaseMetaData dmd = conn.getMetaData();

ResultSet rs = dmd.getColumns( null, “%”, strTableName, “%”);

Whiel( rs.next() )

{

    String strFieldName = Rs.getString( 4 );

    String strFieldType = Rs.getString( 5 );

}

       这种方式的特点是不需要去访问表内数据,看上去也很简洁。在mysql, postgresql中都很顺利,但很可惜,在连Oracle(JDBC 10.2.0.4)的时候,rs.next()false了。查看Oracle JDBCdoc,这也是支持的该接口的,并没有任何特别的说明。那奇怪了。再在OracleJDBC的文档上看到该版本Bug列表

The following table lists the JDBC bugs addressed in this patch set:

5892966

No columns from getColumns() when includeSynonyms=true

原来这里有个特殊的,当includeSysonyms设为true, getColumns是没有返回的。这个OracleOracle就有点不太符合JDBC的标准了。Oracle文档有这一段话:

By default, the getColumns method does not retrieve information about the columns if a synonym is specified. To enable the retrieval of information if a synonym is specified, you must call the setIncludeSynonyms method on the connection as follows:

( (oracle.jdbc.driver.OracleConnection)conn ).setIncludeSynonyms(true)

This will cause all subsequent getColumns method call on the connection to include synonyms. This is similar to setRemarksReporting. Alternatively, you can set the includeSynonymsconnection property. This is similar to the remarksReporting connection property.

这段话基本上也是讲了这个问题。针对Oracle特别的设置这是项目所不允许的,怎么办?

 

ResultSetMetaData获取字段信息

除了直接查看数据库元数据,还可以通过访问表数据来获取记录集元数据。利用ResultMetaData来获取字段信息是比较好的方式,无论表内是否有数据都可返回字段信息,同时测试发现在实验数据库当中都是可行的。

       Connection conn = dbms.getConnection();

       DatabaseMetaData dmd = conn.getMetaData();

      

       Statement st = conn.createStatement();

       String sql = "SELECT * FROM "+table;

       ResultSet rs = st.executeQuery(sql);

       ResultSetMetaData rsmd = rs.getMetaData();

             

       forint i=1; i<=rsmd.getColumnCount(); i++ )

       {

           String field = rsmd.getColumnName(i);

           fields.add( field );

          

           String type = Integer.toString( rsmd.getColumnType(i) ); //5--DATA_TYPE int => SQL type from java.sql.Types

           SqlType sqlT = SqlMapper.mapId( type );

           type = sqlT.sName;

           fieldTypes.add( type );

       }

 

 

注:

①测试数据库:Mckoi 1.0.3, MySQL 5.0, PostgreSQL 8.3, Oracle 10g r2, Oracle 10g XE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值