JDBC ResultSetMetaData

MetaData is data about the data. Here ResultSetMetaData means, the data about the ResultSet is called ResultSetMetaData.

ResultSetMetaData in JDBC :
  1. ◈ ResultSetMetaData is an interface, which is coming from java.sql package.
  2. ◈ If we want to read the data from database we need to use executeQuery() method. executeQuery() method returns ResultSet object, which consists the data rows.
  3. ◈ If we want to read the data from ResultSet object, then we should have the knowledge about the data stored in ResultSet.
  4. ◈ Because based on the type of data only, we need to use appropriate getXxx() method like below:
  5. 
                        ResultSet rs = stmt.executeQuery("select * from student");
                        int id = rs.getInt(1);
                        String name = rs.getString(2);
                       
  6. ◈ If we don’t know the exact data about the ResultSet, what type of get???() method we use?
  7. ◈ ResultSetMetaData provides the data about the ResultSet object.
  8. ◈ It will provide all necessary information about the data available in ResultSet.
  9. ◈ We can get the ResultSetMetaData object by calling getMetadata() method on ResultSet object.
  10. 
                          ResultSet rs = stmt.executeQuery("select * from student");
                          ResultSetMetaData resultSetMetaData = rs.getMetaData();
                        
ResultSetMetaData in JDBC Example :

                      package com.java.session.ninteen;

                      import java.sql.*;

                      public class JdbcResultSetMetaDataExample {
                          static final String DB_URL = "jdbc:mysql://localhost/emp";
                          static final String USER = "root";
                          static final String PASS = "PASSWORD";
                          static final String QUERY = "select * from employee";
                          public static void main(String[] args) throws Exception {
                              Connection connection = null;
                              connection = DriverManager.getConnection(DB_URL, USER, PASS);
                              Statement stmt = connection.createStatement();
                              ResultSet rs = stmt.executeQuery(QUERY);
                              ResultSetMetaData resultSetMetaData = rs.getMetaData();
                              int columnCount = resultSetMetaData.getColumnCount();
                              for (int i = 1; i <= columnCount; ++i) {
                                  System.out.println("***************");
                                  System.out.print("Column Name : " + resultSetMetaData.getColumnLabel(i) + " \n");
                                  System.out.print("Column Type : " + resultSetMetaData.getColumnType(i) + " \n");
                                  System.out.print("Column Class Name : " + resultSetMetaData.getColumnClassName(i) + " \n");
                                  System.out.print("Column Type Name :" + resultSetMetaData.getColumnTypeName(i) + " \n");
                                  System.out.println("Database Name : " + resultSetMetaData.getCatalogName(i));
                              }
                              rs.close();
                              stmt.close();
                              connection.close();
                          }
                      }