Share on Google+Share on Google+

column count using metadata

Advertisement
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

 

Advertisements

Posted on: May 31, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.