Java中ResultSetMetaData 元數據的具體使用

1. 前言

ResultSetMetaData 叫元數據,是數據庫 列對象,以列為單位封裝為對象

元數據,指的是其包含列名,列值,列類型,列長度等等有用信息

2. 常用方法介紹

ResultSetMetaData 常用方法:

1).  metaData.getColumnName(i)         獲取該列的原始名字
2).  metaData.getColumnLabel(i)        獲取該列的別名
3).  metaData.getColumnClassName(i)    獲取該列的(在java中的)數據類型
4).  metaData.getColumnType(i)         獲取該列的(在數據庫中的)數據類型對應的序號
5).  metaData.getColumnTypeName(i)     獲取該列的(在數據庫中的)數據類型
6).  metaData.getScale(i)              獲取該列中小數點右邊的位數
7).  metaData.getColumnDisplaySize(i)  獲取該列的長度
8).  metaData.isAutoIncrement(i)       判斷該列的值是否自動遞增
9).  metaData.isNullable(i)            判斷該列的值是否為null
10).  metaData.getTableName(i)          獲取表名

3. 代碼演示

先準備好一張表,如下圖:

代碼:

public class Test {

    private static final String URL = "jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";

    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        // sql 語句 是使用瞭 別名的
        PreparedStatement preparedStatement = connection.prepareStatement("select id as ID, username as USERNAME, birthday as BIRTHDAY, sex as SEX, address as ADDRESS, money as MONEY  from user ");
        ResultSet resultSet = preparedStatement.executeQuery();
        // 獲取元數據對象
        ResultSetMetaData metaData = resultSet.getMetaData();
        // 獲取一共有多少列
        int columnCount = metaData.getColumnCount();
        // 將數據封裝為Map
        List<Map<String, Object>> list = new ArrayList<>();
        while (resultSet.next()) {
            Map<String, Object> columnMap = new HashMap<>();
            // 註:列名的索引 起始是 1 不是 0
            for (int i = 1; i <= columnCount; i++) {
                System.out.println("getColumnName(i): " + metaData.getColumnName(i));
                System.out.println("getColumnLabel(i): " + metaData.getColumnLabel(i));
                System.out.println("getColumnClassName(i): " + metaData.getColumnClassName(i));
                System.out.println("getColumnType(i): " + metaData.getColumnType(i));
                System.out.println("getColumnTypeName(i): " + metaData.getColumnTypeName(i));
                System.out.println("getScale(i): " + metaData.getScale(i));
                System.out.println("isNullable(i): " + metaData.isNullable(i));
                System.out.println("isAutoIncrement(i): " + metaData.isAutoIncrement(i));
                System.out.println("getTableName(i): " + metaData.getTableName(i));
                System.out.println();

                String key = metaData.getColumnName(i);
                Object value = resultSet.getObject(key);
                columnMap.put(key, value);
            }
            list.add(columnMap);
        }
        System.out.println();
        System.out.println(list);
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
}

4. 結果圖(一部分)

getColumnName(i): id
getColumnLabel(i): ID
getColumnClassName(i): java.lang.Integer
getColumnType(i): 4
getColumnTypeName(i): INT
getScale(i): 0
isNullable(i): 0
isAutoIncrement(i): true   // id 自增 因此為true
getTableName(i): user

getColumnName(i): username
getColumnLabel(i): USERNAME
getColumnClassName(i): java.lang.String
getColumnType(i): 12
getColumnTypeName(i): VARCHAR
getScale(i): 0
isNullable(i): 0
isAutoIncrement(i): false
getTableName(i): user

getColumnName(i): birthday
getColumnLabel(i): BIRTHDAY
getColumnClassName(i): java.time.LocalDateTime
getColumnType(i): 93
getColumnTypeName(i): DATETIME
getScale(i): 0
isNullable(i): 1
isAutoIncrement(i): false
getTableName(i): user

getColumnName(i): sex
getColumnLabel(i): SEX
getColumnClassName(i): java.lang.String
getColumnType(i): 1
getColumnTypeName(i): CHAR
getScale(i): 0
isNullable(i): 1
isAutoIncrement(i): false
getTableName(i): user

getColumnName(i): address
getColumnLabel(i): ADDRESS
getColumnClassName(i): java.lang.String
getColumnType(i): 12
getColumnTypeName(i): VARCHAR
getScale(i): 0
isNullable(i): 1
isAutoIncrement(i): false
getTableName(i): user

getColumnName(i): money
getColumnLabel(i): MONEY
getColumnClassName(i): java.math.BigDecimal
getColumnType(i): 3
getColumnTypeName(i): DECIMAL
getScale(i): 3     // 在數據庫中該列值是 decimal 且是3位小數 因此得出 3
isNullable(i): 1
isAutoIncrement(i): false
getTableName(i): user

list:結果:
[
{birthday=2021-02-27T17:47:08, address=北京, money=10.580, sex=男, id=41, username=老王}, 
{birthday=2021-03-02T15:09:37, address=北京, money=10.580, sex=女, id=42, username=小二王}, 
{birthday=2021-03-04T11:34:34, address=北京, money=10.580, sex=女, id=43, username=小二王}, 
{birthday=2021-03-04T12:04:06, address=北京, money=10.580, sex=男, id=45, username=大王}, 
{birthday=2021-03-07T17:37:26, address=北京, money=10.580, sex=男, id=46, username=老王}, 
{birthday=2021-03-08T11:44, address=北京, money=10.580, sex=女, id=48, username=小馬}, 
{birthday=null, address=null, money=null, sex=男, id=50, username=kkooop}
]

5. 源碼

https://gitee.com/Lgold/learning/tree/df1887c456aa4a140839104de0408f9dedb67ca4/src/main/java/com/king/learning/ResultSetMetaData

到此這篇關於Java中ResultSetMetaData 元數據的具體使用的文章就介紹到這瞭,更多相關ResultSetMetaData 元數據內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: