本篇内容介绍了“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}
]