亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java中ResultSetMetaData 元數(shù)據(jù)的具體使用

 更新時(shí)間:2023年04月25日 16:06:40   作者:搏·夢(mèng)  
本文主要介紹了Java中ResultSetMetaData 元數(shù)據(jù)的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

1. 前言

ResultSetMetaData 叫元數(shù)據(jù),是數(shù)據(jù)庫(kù) 列對(duì)象,以列為單位封裝為對(duì)象。

元數(shù)據(jù),指的是其包含列名,列值,列類型,列長(zhǎng)度等等有用信息。

2. 常用方法介紹

ResultSetMetaData 常用方法:

1).  metaData.getColumnName(i)         獲取該列的原始名字
2).  metaData.getColumnLabel(i)        獲取該列的別名
3).  metaData.getColumnClassName(i)    獲取該列的(在java中的)數(shù)據(jù)類型
4).  metaData.getColumnType(i)         獲取該列的(在數(shù)據(jù)庫(kù)中的)數(shù)據(jù)類型對(duì)應(yīng)的序號(hào)
5).  metaData.getColumnTypeName(i)     獲取該列的(在數(shù)據(jù)庫(kù)中的)數(shù)據(jù)類型
6).  metaData.getScale(i)              獲取該列中小數(shù)點(diǎn)右邊的位數(shù)
7).  metaData.getColumnDisplaySize(i)  獲取該列的長(zhǎng)度
8).  metaData.isAutoIncrement(i)       判斷該列的值是否自動(dòng)遞增
9).  metaData.isNullable(i)            判斷該列的值是否為null
10).  metaData.getTableName(i)          獲取表名

3. 代碼演示

先準(zhǔn)備好一張表,如下圖:

代碼:

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 語(yǔ)句 是使用了 別名的
        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();
        // 獲取元數(shù)據(jù)對(duì)象
        ResultSetMetaData metaData = resultSet.getMetaData();
        // 獲取一共有多少列
        int columnCount = metaData.getColumnCount();
        // 將數(shù)據(jù)封裝為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. 結(jié)果圖(一部分)

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     // 在數(shù)據(jù)庫(kù)中該列值是 decimal 且是3位小數(shù) 因此得出 3
isNullable(i): 1
isAutoIncrement(i): false
getTableName(i): user

list:結(jié)果:
[
{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

到此這篇關(guān)于Java中ResultSetMetaData 元數(shù)據(jù)的具體使用的文章就介紹到這了,更多相關(guān)ResultSetMetaData 元數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java在ElasticSearch中使用LocalDatetime類型

    Java在ElasticSearch中使用LocalDatetime類型

    最近在開發(fā)一個(gè)搜索功能的需求的時(shí)候,遇到了LocalDatetime類型不能保存到ElasticSearch中的問(wèn)題,這篇文章主要介紹了Java在ElasticSearch中使用LocalDatetime類型
    2023-10-10
  • 從?JVM?中深入探究?Synchronized作用及原理

    從?JVM?中深入探究?Synchronized作用及原理

    這篇文章主要為大家介紹了從?JVM?中深入探究?Synchronized作用及原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • SpringBoot集成Sharding Jdbc使用復(fù)合分片的實(shí)踐

    SpringBoot集成Sharding Jdbc使用復(fù)合分片的實(shí)踐

    數(shù)據(jù)庫(kù)分庫(kù)分表中間件是采用的 apache sharding。本文主要介紹了SpringBoot集成Sharding Jdbc使用復(fù)合分片的實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-09-09
  • Java中的Semaphore計(jì)數(shù)信號(hào)量詳細(xì)解析

    Java中的Semaphore計(jì)數(shù)信號(hào)量詳細(xì)解析

    這篇文章主要介紹了Java中的Semaphore計(jì)數(shù)信號(hào)量詳細(xì)解析,Semaphore?是一個(gè)計(jì)數(shù)信號(hào)量,必須由獲取它的線程釋放,常用于限制可以訪問(wèn)某些資源的線程數(shù)量,例如通過(guò)?Semaphore?限流,需要的朋友可以參考下
    2023-11-11
  • Java設(shè)計(jì)模式之策略模式原理與用法實(shí)例詳解

    Java設(shè)計(jì)模式之策略模式原理與用法實(shí)例詳解

    這篇文章主要介紹了Java設(shè)計(jì)模式之策略模式原理與用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Java策略模式的概念、原理、定義及使用方法,并總結(jié)了相關(guān)的優(yōu)缺點(diǎn),具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2018-04-04
  • jstack和線程dump實(shí)例解析

    jstack和線程dump實(shí)例解析

    這篇文章主要介紹了jstack和線程dump實(shí)例解析,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • JavaWeb開發(fā)入門第二篇Tomcat服務(wù)器配置講解

    JavaWeb開發(fā)入門第二篇Tomcat服務(wù)器配置講解

    JavaWeb開發(fā)入門第二篇主要介紹了Tomcat服務(wù)器配置的方法教大家如何使用Tomcat服務(wù)器,感興趣的小伙伴們可以參考一下
    2016-04-04
  • java泛型類的定義與使用詳解

    java泛型類的定義與使用詳解

    這篇文章主要為大家詳細(xì)介紹了java泛型類定義與使用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 使用XSD校驗(yàn)Mybatis的SqlMapper配置文件的方法(1)

    使用XSD校驗(yàn)Mybatis的SqlMapper配置文件的方法(1)

    這篇文章以前面對(duì)SqlSessionFactoryBean的重構(gòu)為基礎(chǔ),簡(jiǎn)單的介紹了相關(guān)操作知識(shí),然后在給大家分享使用XSD校驗(yàn)Mybatis的SqlMapper配置文件的方法,感興趣的朋友參考下吧
    2016-11-11
  • [Java]詳解Socket和ServerSocket學(xué)習(xí)筆記

    [Java]詳解Socket和ServerSocket學(xué)習(xí)筆記

    即時(shí)類應(yīng)用或者即時(shí)類的游戲,HTTP協(xié)議很多時(shí)候無(wú)法滿足于我們的需求,這會(huì),Socket對(duì)于我們來(lái)說(shuō)就非常實(shí)用了。本篇文章主要介紹了Socket和ServerSocket,有興趣的可以了解一下。
    2016-12-12

最新評(píng)論