使用DataBaseMetaData获取mysql表字段注释
?
- DatabaseMetaData?metaData?=?connection.getMetaData();?
- ResultSet?rs?=?null;?
- ?rs?=?metaData.getColumns(dbName,?null,?tableName,?"%");
- while?(rs.next())?{
- ?????????????QueryColumn?column=new?QueryColumn(rs.getString("COLUMN_NAME"),?rs.getString("TYPE_NAME"));
- ?????????????column.setComment(rs.getString("REMARKS"));
- ????????????????columnList.add(column);
- ????????????}
metaData.getColumns mysql实现接口
DatabaseMetaDataUsingInfoSchema#getColumns
- ?sqlBuf.append("CASE?WHEN?LCASE(DATA_TYPE)='date'?THEN?10?WHEN?LCASE(DATA_TYPE)='time'?THEN?8?WHEN?LCASE(DATA_TYPE)='datetime'?THEN?19?WHEN?LCASE(DATA_TYPE)='timestamp'?THEN?19?WHEN?CHARACTER_MAXIMUM_LENGTH?IS?NULL?THEN?NUMERIC_PRECISION?WHEN?CHARACTER_MAXIMUM_LENGTH?>?2147483647?THEN?2147483647?ELSE?CHARACTER_MAXIMUM_LENGTH?END?AS?COLUMN_SIZE,?"?+?MysqlIO.getMaxBuf()?+?"?AS?BUFFER_LENGTH,"?+?"NUMERIC_SCALE?AS?DECIMAL_DIGITS,"?+?"10?AS?NUM_PREC_RADIX,"?+?"CASE?WHEN?IS_NULLABLE='NO'?THEN?"?+?0?+?"?ELSE?CASE?WHEN?IS_NULLABLE='YES'?THEN?"?+?1?+?"?ELSE?"?+?2?+?"?END?END?AS?NULLABLE,"?+?"COLUMN_COMMENT?AS?REMARKS,"?+?"COLUMN_DEFAULT?AS?COLUMN_DEF,"?+?"0?AS?SQL_DATA_TYPE,"?+?"0?AS?SQL_DATETIME_SUB,"?+?"CASE?WHEN?CHARACTER_OCTET_LENGTH?>?"?+?2147483647?+?"?THEN?"?+?2147483647?+?"?ELSE?CHARACTER_OCTET_LENGTH?END?AS?CHAR_OCTET_LENGTH,"?+?"ORDINAL_POSITION,"?+?"IS_NULLABLE,"?+?"NULL?AS?SCOPE_CATALOG,"?+?"NULL?AS?SCOPE_SCHEMA,"?+?"NULL?AS?SCOPE_TABLE,"?+?"NULL?AS?SOURCE_DATA_TYPE,"?+?"IF?(EXTRA?LIKE?'%auto_increment%','YES','NO')?AS?IS_AUTOINCREMENT,?"?+?"IF?(EXTRA?LIKE?'%GENERATED%','YES','NO')?AS?IS_GENERATEDCOLUMN?FROM?INFORMATION_SCHEMA.COLUMNS?WHERE?");
使用DataBaseMetaData获取oracle表字段注释
oracle是需要在连接JDBC获取connection时通过RemarksReporting来设置是否能获取comments.
而RemarksReporting默认为false.在获取oracle的表字段信息时需要设置为True.
- Properties?props?=newProperties();
- props.put("remarksReporting","true");
- Class.forName("oracle.jdbc.driver.OracleDriver");
- dbConn?=?DriverManager.getConnection(url,?props);
- DatabaseMetaData?dbmd?=?dbConn.getMetaData();
对于使用使用连接池时,可先判断连接数据库类型,将connection转换为OracleConnection,因为setRemarking在OracleConnection实现了。
|