column count using metadata


 

column count using metadata

In this program ,using ResultSetMetaData we will count column of a table.

In this program ,using ResultSetMetaData we will count column of a table.

GET COLUMN COUNT USING "RESULTSETMETADATA"  

In this program ,we will get column count using "resultsetmetadata ".Using resultset interface and metadata interface ,we can get properties of column. The ResultSetMetaData object can be obtained by invoking getMetaData() method on ResultSet object. The function or method ,use for this is "getColumnCount()".we use employee database for this purpose--

import java.sql.*;
public class Countcolumn {

  public static void main(String[] args) throws Exception {

    String driver = "com.mysql.jdbc.Driver";
    
  String url = "jdbc:mysql://192.168.10.13:3306/ankdb";
    
  String username = "root";
    String password = "root";
    
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
   
  System.out.println("Connected");
    Statement st = conn.createStatement();
    
  ResultSet rs = st.executeQuery("SELECT * FROM employee");
    ResultSetMetaData rsmd = rs.getMetaData();
    int NumOfCol=rsmd.getColumnCount();
    System.out.println("Number of Columns="+NumOfCol);
   
  st.close();
    conn.close();
  }
}



OUTPUT

 

Ads