Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: select count(*)

  1. #1
    Junior Member
    Join Date
    May 2009
    Location
    Mumbai/ Bombay - India
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default select count(*)

    Scenario:
    I dont know much about JDBC. I want to find the number of records in a table.

    The SQL statement for it is : SELECT COUNT(NAME) FROM USERS

    Here NAME is an attribute of the USERS table.

    Problem:
    I have tried executeQuery and executeUpdate methods of the Statement class. But I have got only errors. Can someone help me?


  2. #2
    Little Star
    Join Date
    May 2009
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 7 Posts

    Default Re: select count(*)

    I haven't checked so I might be totaly wrong: but don't you need a "WHERE" statement?
    Example:
    SELECT count(name) FROM Users WHERE 1;

    If it's not that, then I would need to see how you do it in your code and what errors you get.

  3. #3
    Junior Member
    Join Date
    May 2009
    Location
    Mumbai/ Bombay - India
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: select count(*)

    Well..... thanks for your help Dalisra. I have not tried your solution. But I got the solution to my problem.

    The solution is as follows
    Statement st = con.createStatement(); // con is reference of type Connection.
    ResultSet rs = st.executeQuery("SELECT [B]COUNT(NAME) AS N1[/B] FROM USERS");
    rs.next();
    int number = rs.getInt("N1");
    System.out.println("Number of Users : " + number);
    rs.close();
    con.close();

    It works.
    The above code has to be in a method. The package java.sql has to be imported.
    Last edited by jacinto; June 1st, 2009 at 07:49 AM.

  4. #4
    Little Star
    Join Date
    May 2009
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 7 Posts

    Default Re: select count(*)

    Ahh, I am glad you found that out.

  5. #5
    Junior Member
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: select count(*)

    Your problem seems to occur at this point:
    "SELECT COUNT(NAME) AS N1 FROM USERS"

    You can perform your functionality as follow:
    Statement st = con.createStatement(); // con is reference of type Connection.
    ResultSet rs = st.executeQuery("SELECT COUNT(NAME) AS N1 FROM USERS");
    rs.next();
    int number = rs.getInt("N1");// the parameter is columnName
    //int number = rs.getInt(1); //the parameter is columnIndex;
    System.out.println("Number of
    System.out.println("Number of Users : " + number);
    rs.close();
    con.close();