- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the number of records in a table using JDBC?
The ResultSet class doesn’t provide any direct method to get the number of records in a table.
The beforeFirst() method navigates the pointer/curser of the ResultSet object to its default position before first.
In the same way the last() method positions the cursor at the last row of the ResultSet object.
Using these methods you can find the number of records in the current ResultSet object.
Example
Assume we have a table named customers table with contents as shown below:
+----+---------+-----+---------+----------------+ | ID | NAME | AGE | SALARY | ADDRESS | +----+---------+-----+---------+----------------+ | 1 | Amit | 25 | 3000.00 | Hyderabad | | 2 | Kalyan | 27 | 4000.00 | Vishakhapatnam | | 3 | Renuka | 30 | 5000.00 | Delhi | | 4 | Archana | 24 | 1500.00 | Mumbai | | 5 | Koushik | 30 | 9000.00 | Kota | +----+---------+-----+---------+----------------+
Following JDBC program establishes connection with the database, retrieves a ResultSet object holding the contents of the customers table and, gets the number of records in the current ResultSet object.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class SizeOfColumn { public static void main(String args[])throws Exception { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating a Statement object Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from customers"); int size =0; if (rs != null) { rs.beforeFirst(); rs.last(); size = rs.getRow(); } System.out.println("Number of records in the table represented by the ResultSet object is: "+size); } }
Output
Connection established...... Number of records in the table represented by the ResultSet object is: 5
- Related Articles
- How to get the number of columns of a table using JDBC?
- How to get the datatype of a column of a table using JDBC?
- How to get the size of a column of a table using JDBC?
- How to delete all records from a table in Oracle using JDBC API?
- How to get the table name of the current ResultSet using JDBC?
- How to get all table names from a database using JDBC?
- How to create a table in JDBC using another table?
- How to get the properties of a driver using JDBC?
- How to create a table in Oracle using JDBC?
- How to create a table in JavaDB using JDBC?
- How to drop a table from JavaDB using JDBC?
- How to create a table in a database using JDBC API?
- How to retrieve particular columns of a table using JDBC program?
- How to get the list of all databases using JDBC?
- How to reorder the columns of a table in JDBC?

Advertisements