Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Java get number of rows in resultset 
 

In this section, you will learn how to retrieve the number of rows from the database table.

 

Java get number of rows in resultset

                         

In this section, you will learn how to retrieve the number of rows from the database table. As you know that the data is stored in the database table in the form of row and column. Therefore, to access the database, you need to create a database table.

Table structure of 'student'

create table student (
         id int not null auto_increment,
         Name  varchar(20),
         Score varchar(20),
         primary key(id)  );

Table is:

Now, in order to retrieve the number of rows from the database table, you need to create a connection between the database and the java class file. 

Class.forName(driver)- This will loads the driver. The driver is: com.mysql.jdbc.Driver.

getConnection(url, username, password)- This method create a connection by taking parameters of string type  url, username and password to connect to the database. Here the url is 'jdbc:mysql://localhost:3306/chart', username is 'root' and password is 'root'. In the url, the chart is the name of the database.

createStatement()- This method is used for sending sql statements to the specified database.

executeQuery()- This method executes the query.

By using rs.next(), you can extract the data from the resultset from the top and rs.last() will move you to the end of the resultset. The method  rs.getRow() get the row number of the last row and also shows you the number of rows in the table.

Here is the code of GetNumberOfRows.java

import java.sql.*;

public class GetNumberOfRows {
public static Connection getConnection() throws Exception {
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/chart";
    String username = "root";
    String password = "root";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
    public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      conn = getConnection();
      String query = "select id from student";
      stmt = conn.createStatement();

      rs = stmt.executeQuery(query);
      while (rs.next()) {
        String id = rs.getString(1);
       }
      rs.last();
      int rowCount = rs.getRow();
      System.out.println("Number of Rows=" + rowCount);
    catch (Exception e) {
      e.printStackTrace();
    finally {
      try {
        rs.close();
        stmt.close();
        conn.close();
      catch (SQLException e) {}
     }
  }
}

Output will be displayed as:

Download Source Code

                         

» View all related tutorials
Related Tags: java c memory time application object io runtime free method get order return this sso ai app with to run

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.