您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Java中ResultSetMetaData元數(shù)據(jù)如何使用”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
ResultSetMetaData 叫元數(shù)據(jù),是數(shù)據(jù)庫 列對象,以列為單位封裝為對象。
元數(shù)據(jù),指的是其包含列名,列值,列類型,列長度等等有用信息。
ResultSetMetaData 常用方法:
1). metaData.getColumnName(i) 獲取該列的原始名字
2). metaData.getColumnLabel(i) 獲取該列的別名
3). metaData.getColumnClassName(i) 獲取該列的(在java中的)數(shù)據(jù)類型
4). metaData.getColumnType(i) 獲取該列的(在數(shù)據(jù)庫中的)數(shù)據(jù)類型對應的序號
5). metaData.getColumnTypeName(i) 獲取該列的(在數(shù)據(jù)庫中的)數(shù)據(jù)類型
6). metaData.getScale(i) 獲取該列中小數(shù)點右邊的位數(shù)
7). metaData.getColumnDisplaySize(i) 獲取該列的長度
8). metaData.isAutoIncrement(i) 判斷該列的值是否自動遞增
9). metaData.isNullable(i) 判斷該列的值是否為null
10). metaData.getTableName(i) 獲取表名
先準備好一張表,如下圖:
代碼:
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(); // 獲取元數(shù)據(jù)對象 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(); } }
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ù)庫中該列值是 decimal 且是3位小數(shù) 因此得出 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}
]
“Java中ResultSetMetaData元數(shù)據(jù)如何使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網(wǎng)站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。