JDBC Get Row Count

In this Tutorial we want to describe a code that make you to understand in JDBC Get Row Count.

JDBC Get Row Count

JDBC Get Row Count

     

The JDBC Get Row Count enables you to return the row count of a query. JDBC Get Row Count  provides you the name of the field, total number of row and data type of the column  to be retrieved from the table.

Understand with Example

In this Tutorial we want to describe a code that make you to understand in JDBC Get Row Count. The code help you to understand how to count the number of field and field name. The metadata in this code  provide you the property of the retrieved column. For this, Program include a class name JdbcGetRowCount, Inside this class we have a main method that include list of following steps -

 Import a package java.sql*,enables you to provide a network interface that help in communicating between the front end and the backend.

 Loading a driver is the next step to be followed by calling a class.forName( ),that accept driver class as argument.

The DriverManager.getConnection ( ) return you a connection object, the get Connection ( ) built a connection between url and database. The create Statement ( ) is used to create sql object. An object of connection is used to send and execute query in the database.

.execute query ( )  -   This is used to retrieve the record set from a table in the database and the record set from the table is stored in Result Set.   The select statement is used to retrieve the  record set from table.

 rs.get Metadata ( ) - This is used to return an object that can be used further to get information about the types and properties of the columns in a ResultSetMetaData object. The meta data call a getColumnCount ( ) return you the number of column in the table.

Finally the println print the Table Name, number of field and the field name.

Incase the exception occurred in the try block, subsequent the catch block check and handle the exception.

JdbcGetRowCount.java
import java.sql.*;

public class JdbcGetRowCount {

    public static void main(String args[]) {

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://localhost:3306/";
        String db = "komal";
        String driver = "com.mysql.jdbc.Driver";
        String user = "root";
        String pass = "root";

        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url + db, user, pass);
            st = con.createStatement();

            String sql = "select * from person";
            rs = st.executeQuery(sql);
            ResultSetMetaData metaData = rs.getMetaData();

            int rowCount = metaData.getColumnCount();

            System.out.println("Table Name : " + 
            metaData.getTableName(rowCount));
            System.out.println("No of Field : " + rowCount);
            System.out.print("Field Name :");
            for (int i = 0; i < rowCount; i++) {
                System.out.print("  " + metaData.getColumnLabel(i+1));
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output

Table Name : person
No of Field : 3
Field Name :  id  cname  dob

Download code