JDBC--使用ResultSetMetaData 对象处理结果集元数据

可用于获取关于 ResultSet 对象中列的类型和属性信息的对象:

  • getColumnName(int column):获取指定列的名称
  • getColumnCount():返回当前 ResultSet 对象中的列数。
  • getColumnTypeName(int column):检索指定列的数据库特定的类型名称。
  • getColumnDisplaySize(int column):指示指定列的最大标准宽度,以字符为单位。
  • isNullable(int column):指示指定列中的值是否可以为 null。

 

    /**
     * ResultSetMetaData
     * 是描述 ResultSet的元数据对象,即从中可以得到结果集中有多少列,列名是什么
     * 1.得到 ResultSetMetaData对象:调用 ResultSet的 getMetaData()方法
     * 2.常用方法
     * getColumnCount()
     *   --Returns the number of columns in this ResultSet object.
     * getColumnLabel(int column)
     *   --Gets the designated column's suggested title for use in
     *     printouts and displays.
     */
    @Test
    public void test11() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Map<String, Object> values = new HashMap<>();
        String sql = "SELECT Sno stuNo, Sname stuName, Ssex stuSex, " +
                     "Sage stuAge, Sdept stuDept, S_entrance entrance " +
                     "FROM Student WHERE Sno=?";
        try {
            connection = getConnection2();
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, "201215122");
            resultSet = preparedStatement.executeQuery();
            //得到ResultSetMetaData对象
            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
            //
            if(resultSet.next()){
                for(int i=0; i < resultSetMetaData.getColumnCount(); ++i) {
                    String columnLable = resultSetMetaData.getColumnLabel(i + 1);
                    Object columnValue = resultSet.getObject(columnLable);
                    values.put(columnLable, columnValue);
                }
            }

            for(Map.Entry<String, Object> entry : values.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                System.out.println(key + " : " + value);
            }
            
        } catch(Exception e) {
            e.printStackTrace();
        }finally {
            if(resultSet != null) {
                try {
                    resultSet.close();
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
            if(preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
            if(connection != null) {
                try {
                    connection .close();
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

//getConnection2()方法:
    public Connection getConnection2() throws Exception {
        //1.准备连接数据库的4个字符串
        //1.1 创建Properties对象
        Properties properties = new Properties();
        //1.2 获取jdbc.properties对应的输入流
        java.io.InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
        //1.3 加载文件
        properties.load(in);
        //1.4 给字符串赋值
        String driver = properties.getProperty("driver");
        String jdbcUrl = properties.getProperty("jdbcUrl");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        //2.加载数据库驱动程序(对应的Driver实现类中有注册驱动的静态代码块)
        Class.forName(driver);
        //3.通过DriverManager的getConnection方法获取数据库连接
        return DriverManager.getConnection(jdbcUrl, user, password);
    }

//jdbc.properties
driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/jdbctest
user=root
password=12345

 

 

JDBC学习笔记:

1. 获取数据库连接    http://my.oschina.net/daowuming/blog/704243

2. 通过Statement执行更新、查询操作    http://my.oschina.net/daowuming/blog/704384

3. 使用PrepareStatement    http://my.oschina.net/daowuming/blog/704432

4. 使用ResultSetMetaData 对象处理结果集元数据    ----当前----

5. 使用DatabaseMetaData获取数据库信息    http://my.oschina.net/daowuming/blog/704553

6. BLOB    http://my.oschina.net/daowuming/blog/704593

7. 处理事务与隔离级别    http://my.oschina.net/daowuming/blog/704611

8. 批量处理    http://my.oschina.net/daowuming/blog/704641

9. 数据库连接池    http://my.oschina.net/daowuming/blog/704700

10. 调用函数与存储过程    http://my.oschina.net/daowuming/blog/704813

转载于:https://my.oschina.net/daowuming/blog/704487

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值