jdbc获取mysql 列信息_JDBC获取数据库表字段信息

本文介绍了在使用JDBC时如何获取MySQL和Oracle数据库表的字段信息,包括通过DatabaseMetaData的getColumns()方法以及利用ResultSetMetaData。在Oracle中,由于getColumns()方法与synonyms相关的问题,需要特殊处理。文章提供了针对Oracle问题的解决方案,并展示了通过ResultSetMetaData获取字段信息的方法。

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

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

字段信息

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

1.TABLE_CATString => table catalog (may be null)

2.TABLE_SCHEMString => table schema (may be null)

3.TABLE_NAMEString => table name

4.COLUMN_NAMEString => column name

5.DATA_TYPEint => SQL type from java.sql.Types

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

7.COLUMN_SIZEint => column size.

8.BUFFER_LENGTHis not used.

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

10.NUM_PREC_RADIXint => Radix (typically either 10 or 2)

11.NULLABLEint => is NULL allowed.

ocolumnNoNulls - might not allow NULL values

ocolumnNullable - definitely allows NULL values

ocolumnNullableUnknown - nullability unknown

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

13.COLUMN_DEFString => 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 JDBC的doc,这也是支持的该接口的,并没有任何特别的说明。那奇怪了。再在OracleJDBC的文档上看到该版本Bug列表

The following table lists the JDBC bugs addressed in this patch set:5892966No columns from getColumns() when includeSynonyms=true

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

By default, thegetColumnsmethod 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 thesetIncludeSynonymsmethod on the connection as follows:

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

This will cause all subsequentgetColumnsmethod call on the connection to include synonyms. This is similar tosetRemarksReporting. Alternatively, you can set theincludeSynonymsconnection property. This is similar to theremarksReportingconnection 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();

for(inti=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、付费专栏及课程。

余额充值