JAVA中获取字段信息的方法

              一、JDBC之DatabaseMetaData之获取表信息以及列信息

    (1) DatabaseMetaData实例的获取

        Connection conn = DriverManager.getConnection("DataBase");
        DatabaseMetaData dbmd = Conn.getMetaData();

 

   (2) 获得当前数据库中表的信息

        dbmd.getTables(String catalog,String schema,String tableName,String[] types),

       该方法带有四个参数,它们表示的含义如下:
        String catalog:要获得表所在的编目。Null表示所有编目。

        String schema:要获得表所在的模式。Null表示所有模式。

        String tableName:指出要返回表名与该参数匹配的那些表,

        String types:一个指出返回何种表的数组。

        可能的数组项是:"TABLE"、"VIEW"、"SYSTEM TABLE", "GLOBAL TEMPORARY","LOCAL  TEMPORARY","ALIAS","SYSNONYM"。

        通过getTables()方法返回的结果集中的每个表都有下面是10字段的描述信息。

        1.TABLE_CAT        (String)   => 表所在的编目(可能为空)  

        2.TABLE_SCHEM (String)   => 表所在的模式(可能为空) 

        3.TABLE_NAME    (String)   => 表的名称

        4.TABLE_TYPE     (String)    => 表的类型。

                典型的有 "TABLE", "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL  TEMPORARY", "ALIAS", "SYNONYM". 

        5.REMARKS          (String)       => 解释性的备注

        6.TYPE_CAT          (String)      =>编目类型(may be null) 

        7.TYPE_SCHEM   (String)      => 模式类型(may be null) 

        8.TYPE_NAME      (String)      => 类型名称(may be null) 

        9.SELF_REFERENCING_COL_NAME    (String) => name of the designated "identifier" column of a typed table (may be null) 

       10.REF_GENERATION   (String)    => specifies how values in SELF_REFERENCING_COL_NAME are created.

                   它的值有:"SYSTEM"、"USER"、"DERIVED",也可能为空。

  (3) 获得某个表的列信息

        dbmd.getColumns(String catalog,String schama,String tablename,String columnPattern)

        通过getColumns()方法返回的结果集中的每一列都有下面是23个字段段的描述信息。而且在结果集中直接使用下面字段前面的序号即可获取字段值。

        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 => (数据库中的列对应的类型。如DATE,STRING等)

        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.

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

       13.COLUMN_DEF String => default value for the column, (may be null)

       14.SQL_DATA_TYPE int => unused

       15.SQL_DATETIME_SUB int => unused

       16.CHAR_OCTET_LENGTH int => for char types the maximum number of bytes in the column

       17.ORDINAL_POSITION int => index of column in table (starting at 1)

       18.IS_NULLABLE String => ISO rules are used to determine the nullability for a column.

       19.SCOPE_CATLOG String => catalog of table that is the scope of a reference attribute (null if DATA_TYPE isn't REF)

        20.SCOPE_SCHEMA String => schema of table that is the scope of a reference attribute (null if the DATA_TYPE isn't REF)

        21.SCOPE_TABLE String => table name that this the scope of a reference attribure (null if the DATA_TYPE isn't REF)

        22.SOURCE_DATA_TYPE short => source type of a distinct type or user-generated Ref type, SQL type from java.sql.Types

       23.IS_AUTOINCREMENT String => Indicates whether this column is auto incremented

        二、Select类型之获取表中列信息

String sql = "select * from " + "TABLE_NAME";//假设此时有许多表
PreparedStatement stmt = null;
try {
stmt = connect.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
ResultSetMetaData data = rs.getMetaData();
while (rs.next()) {
for (int i = 1; i <= data.getColumnCount(); i++) {
String columnTypeName = data.getColumnTypeName(i);
String columnName = data.getColumnName(i);
}
}
}
} catch (Exception e) {
} finally {
stmt.close();
}*注意在不使用游标时应关闭。

 

Java获取字段文名可以通过注解的方式来实现。在定义类的字段时,使用注解来添加文名的信息。这样,在需要获取文名的地方,就可以通过反射来获取字段的注解信息,从而获取文名。 首先,在定义类的字段时,可以使用注解来标记字段文名。例如,定义一个person类,其有一个字段为name,可以使用注解@FieldName("姓名")来标记字段文名。 ```java public class Person { @FieldName("姓名") private String name; // ... } ``` 然后,在需要获取字段文名的地方,可以通过反射来获取注解信息。可以使用`Field`类的`getDeclaredFields`方法获取类的所有字段,再通过`getAnnotation`方法获取注解的实例,从而获取文名。 ```java public static String getChineseName(Field field) { FieldName fieldName = field.getAnnotation(FieldName.class); if (fieldName != null) { return fieldName.value(); } return null; } ``` 最后,可以通过调用`getChineseName`方法,传入字段的`Field`对象,来获取字段文名。 ```java public static void main(String[] args) { Field[] fields = Person.class.getDeclaredFields(); for (Field field : fields) { String chineseName = getChineseName(field); if (chineseName != null) { System.out.println(field.getName() + "的文名为:" + chineseName); } } } ``` 以上就是在Java获取字段文名的方法。通过使用注解来标记字段文名,并通过反射获取注解信息,可以很方便地获取字段文名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值