Count rows and get column name

Count rows and get column name

Posted in : Core Java Posted on : October 18, 2010 at 4:14 PM Comments : [ 0 ]
This section contains the detail about the Count rows and get column name in java.

Column count using metadata

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://";

String username = "root";
String password = "root";

Connection conn = DriverManager.getConnection(url, username, password);

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);



Output :

C:\Program Files\Java\jdk1.6.0_18\bin>java Countcolumn
Number of Columns=2

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image

Tutorial Topics