Results 1 to 12 of 12
  1. #1
    Kenan_89 is offline Member
    Join Date
    Jan 2012
    Posts
    25
    Rep Power
    0

    Default Count number of rows

    Hello I began with java database programming and I have a question.
    I want to save the number of rows in my table in an integer.
    My code looks like:

    ResultSet count_id = statement.executeQuery("SELECT COUNT (*) FROM customers");
    if(count_id.next()){
    int xy = count_id.getInt(1);
    }

    Unfortunately this doesn`t work. Have someone an idea what the problem
    could be?

    Sry if I make mistakes in english. It is not my mother tongue.:)
    Last edited by Kenan_89; 01-25-2012 at 02:50 PM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Count number of rows

    What doesn't work? What do you expect? What do you get?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Count number of rows

    Try COUNT(*) instead of COUNT (*)

  4. #4
    Kenan_89 is offline Member
    Join Date
    Jan 2012
    Posts
    25
    Rep Power
    0

    Default Re: Count number of rows

    I want to save the number of rows in my table into an integer.
    For example:
    table Customer
    ID: Name: Age:
    1 name1 22
    2 name2 23
    3 name3 40

    In this example my table would have 3 rows.

    Try COUNT(*) inseatd of COUNT (*)
    I tried it but still not works.
    Last edited by Kenan_89; 01-25-2012 at 02:49 PM.

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Count number of rows

    What doesn't work?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    Kenan_89 is offline Member
    Join Date
    Jan 2012
    Posts
    25
    Rep Power
    0

    Default Re: Count number of rows

    Eclipse doesn`t show me errors, but in oracle I can see my table and I know that it has 7 lines. With my code I showed in my
    post I get a solution of 3.

  7. #7
    s3ib is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default Re: Count number of rows

    What will happen instead of expected result?
    You will get an exception of somekind? which kind?
    Your int xy = 0 instead of 3?

    Mabye try SELECT COUNT(*) as rowCount FROM customers
    and int xy = count_id.getInt("rowCount");

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default Re: Count number of rows

    You are either not looking at the same database, the same table, or the system you are looking at the table in that is giving "7" as an answer has uncommitted data...or the Java code running the above count has uncommitted deletes.

  9. #9
    2dmurali is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default Re: Count number of rows

    try
    {
    Connection con=Connect.getConnect();
    Statement st=con.createStatement();
    ResultSet rs=st.executeQuery("select count(*) from availability");
    rs.next();
    System.out.println(rs.getInt(1));
    }
    catch(Exception e)
    {}

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default Re: Count number of rows

    How is that different ot what the OP has already posted?
    Well, apart from the eating of exceptions, and failure to check if next().

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default Re: Count number of rows

    Maybe that table exists in an entirely different schema; the OP should print out the stack trace of any Exception (if one was thrown).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default Re: Count number of rows

    The OP is getting a result back, though (at least that's how I read post #6).
    Of course if that's not the case I hope they will enlighten me!

Similar Threads

  1. how to count number of pages printed?
    By absmarty in forum New To Java
    Replies: 10
    Last Post: 01-31-2012, 06:20 PM
  2. Replies: 8
    Last Post: 01-21-2012, 01:14 AM
  3. Replies: 4
    Last Post: 03-25-2011, 01:06 PM
  4. day number count
    By droidus in forum New To Java
    Replies: 14
    Last Post: 03-23-2011, 10:15 PM
  5. Array count number Occurances
    By gwithey in forum New To Java
    Replies: 2
    Last Post: 04-17-2009, 08:34 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
  •