JDBC--使用ResultSetMetaData 對象處理結果集元數據

可用於獲取關於 ResultSet 對象中列的類型和屬性信息的對象:java

  • 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學習筆記:mysql

1. 獲取數據庫鏈接    http://my.oschina.net/daowuming/blog/704243sql

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/704553this

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

7. 處理事務與隔離級別    http://my.oschina.net/daowuming/blog/704611code

8. 批量處理    http://my.oschina.net/daowuming/blog/704641對象

9. 數據庫鏈接池    http://my.oschina.net/daowuming/blog/704700

10. 調用函數與存儲過程    http://my.oschina.net/daowuming/blog/704813

相關文章
相關標籤/搜索