Results 1 to 5 of 5
  1. #1
    arunkumarinfo is offline Member
    Join Date
    Jan 2009
    Posts
    15
    Rep Power
    0

    Thumbs down how we can get the count value in a variable

    whether it is posssible to get the count value in a variable .please reply.

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Yes. I assume you mean the count of records in a database.

    select count(*) from SomeTable where SomeCondition

    Execute this query and use the ResultSet to retrieve the count.

  3. #3
    JavaForever is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    In any method use an integer as a count this is a sample code:
    public void.....{
    int count=0;
    try
    {
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM any table");

    while(rs.next())
    {
    count++;
    }
    System.out.println(count);
    then you will get the number of records.

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    What if the table contain 1,000,000 records? I've worked with tables that had 40 million, and they were archived annually. Looping through all those records could take minutes to hours.
    [CODE]
    public int getRecordCount {
    int count=0;
    try {
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT count(*) FROM table");
    if (rs.next()) {
    count = rs.getInt(1);
    }
    }
    return count;
    }

  5. #5
    JavaForever is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default Try this

    rs.last();
    int count= rs.getRow();

    this will get the current row number, which is the last in your table, i hope it will work.

Similar Threads

  1. to get count value as a variable
    By arunkumarinfo in forum JDBC
    Replies: 2
    Last Post: 03-30-2009, 01:32 AM
  2. Replies: 1
    Last Post: 05-29-2008, 06:36 PM
  3. Getting row count
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 08:49 AM
  4. how to count 2 inserts together?
    By kim85 in forum New To Java
    Replies: 0
    Last Post: 01-20-2008, 11:25 AM
  5. Getting row count from executeQuery()
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-05-2007, 02:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •