JDBC Get Row Count

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

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

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

Advertisements

Ads
Share on Google+Share on Google+

JDBC Get Row Count

Posted on: November 17, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: JDBC Get Row Count   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
Manoj
July 19, 2011
errr...

do u know the diff b/w the ROW n COLUMN????
amatuerGuru
December 13, 2011
Row count

hi, thanks for the example. But i do not see the row count. You have counted column numbers.
Srinivas
August 6, 2012
JDBC Get Row Count

Title( Row count) but example belongs to column count. Misleading

Ads

 

Ads