• 五、元数据ResultSetMetaData类


    参考:https://blog.csdn.net/lizhiqiang1217/article/details/90549424

    https://www.jianshu.com/p/36d5d76342f1

    • 获得对象:ResultSet.getMetaData()
      获得代表ResultSet对象元数据的ResultSetMetaData对象。

    ResultSetMetaData类常用方法:

    • getColumnName(int column):获取指定列的名称

    • getColumnCount():返回当前 ResultSet 对象中的列数。

    • getColumnTypeName(int column):检索指定列的数据库特定的类型名称。

    • getColumnDisplaySize(int column):指示指定列的最大标准宽度,以字符为单位。

    • isNullable(int column):指示指定列中的值是否可以为 null。

    • isAutoIncrement(int column):指示是否自动为指定列进行编号,这样这些列仍然是只读的。

    例:

     1  Connection conn = null;
     2         PreparedStatement ps = null;
     3         ResultSet rs = null;
     4         List<T> list = new ArrayList<>();
     5         try {
     6             conn = JdbcUtil.getConnection();
     7             ps = conn.prepareStatement(sql);
     8             //得到参数的个数
     9             ParameterMetaData pmd = ps.getParameterMetaData();
    10             //绑定参数
    11             for (int i = 0;i<pmd.getParameterCount();i++){
    12                 ps.setObject(i+1,parm[i]);
    13             }
    14             rs = ps.executeQuery();
    15             ResultSetMetaData rsmd = rs.getMetaData();
    16             while (rs.next()){
    17                 T bean =clazz.newInstance();//Departmens d = new Department();
    18                 for (int i = 0; i < rsmd.getColumnCount(); i++) {
    19                     //得到列名
    20                     String columnName = rsmd.getColumnName(i+1);
    21                     //获取列的值
    22                     Object value = rs.getObject(columnName);
    23                     //通过BeanUtil工具类将值当如到对象中
    24                     BeanUtils.setProperty(bean,columnName,value);
    25                 }
    26                 list.add(bean);
    27             }



  • 相关阅读:
    谈谈近两年维护的一个最最坑爹项目
    LintCode 丑数
    nova 配置文件
    python 网络编程
    python
    python
    cocos2d-js导弹跟踪算法(一边追着目标移动一边旋转角度)
    nginx和apache
    Service绑定模式
    类对象作为成员
  • 原文地址:https://www.cnblogs.com/qiaoxin11/p/12822521.html
Copyright © 2020-2023  润新知