Solved Counting rows in mySQL Database

Discussion in 'Plugin Development' started by Tyler Christensen, Aug 24, 2014.

Thread Status:
Not open for further replies.
  1. I need to know how to count the number of rows in a table. And then get how many rows are in the table to an int. Could someone help? i dont have any code yet xD but i do know some sql with prepaired statements in Bukkit/Java.
     
  2. Offline

    mechoriet

    Code:java
    1. int count = 0;
    2.  
    3. while (rs.next()) {
    4. ++count;
    5. // Get data from the current row and use it
    6. }
    7.  
    8. if (count == 0) {
    9. System.out.println("No records found");
    10. }



    this is a example how to count the rows maybe not the best method to do it but its a way make a prepared statment with a WHERE `int` = "";
     
    Tyler Christensen likes this.
  3. Offline

    Zupsub

    There's a SQL statement for that:
    "SELECT COUNT(*) FROM TABLE table"
    Don't know exactly what you do...
     
  4. To add to the previous two answers (both correct). Which one you use depends on what you are doing.

    If you want a count of the whole table or just a count, doing SELECT COUNT(*) as Zupsub suggests will be faster, you then read the value from the ResultSet. If you just want a count of specific data without retrieving it (say all rows with the field name set to "john", SELECT COUNT(*) FROM tableName WHERE name="john" will provide that.

    If you want a count of a query you made (say, how many friends names came back). mechoriot's method will suffice, but should not be used if all you want is just a count and nothing else.
     
    Zupsub likes this.
  5. tehbeard well if i were to do the SELECT COUNT(*) FROM tablename how would i go about getting that int it makes and refrencing it to an int in my code?
     
  6. Offline

    Rocoty

    Tyler Christensen Well....through your resultset obviously. If you are asking how to get the correct row by name, your solution is to give the row a name in the query, like so:
    SELECT COUNT(*) AS `count` FROM tablename
     
  7. Result set LOL ty xD ik there was a way of doing it, justkingda.... flew by my mind xD
    Ty Rocoty and tehbeard and
    Zupsub

    Sorry, but how would i go about with the ResultSet to make how many rows there are to an int?

    Anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  8. Offline

    Zupsub

    Doesn't it has a getInt or nextInt method? Use "*" as row name.
     
  9. i recived errors while doing something like that... :/
     
  10. Offline

    mechoriet

    Tyler Christensen if you have a stacktrace or some error post it plz so we can help you the best ways
     
  11. You can use the column index instead of name, so:

    Code:
    ResultSet rs = ....
    rs.next();//Read in the first row.
    int rowCount = rs.getInt(1);//Fetch value of first column of result as an int.
    rs.close();//Clean up time
    
     
  12. Offline

    Lurvik

    Another way is also to do this:

    Code:java
    1.  
    2. ResultSet rs = blahblah;
    3.  
    4. many code
    5. much java
    6.  
    7. int count = rs.getLast().getRow();
    8. rs.close();
    9.  


    Or if you want the count the first thing you do:

    Code:Java
    1. ResultSet = veryCode;
    2. if (rs.getLast().getRow() > 20)
    3. {
    4. rs.getFirst();
    5. while (rs.hasNext()) doSomething;
    6. }
     
  13. Lurvik there is no such thing as hasNext() though But similar answer to what im looking for... here is what i have now:

    And i still think most of you don't get the point of what im doing... in my database, i have several rows... all i want to do is count them... i dont want to get the name of one row, or get it's id or whatever, i want to know how many rows are in the database, then however many there are, make that to an int in java. heres what i have so far from what i know...

    Code:java
    1. openCon();
    2. PreparedStatement sqll = connection.prepareStatement("SELECT COUNT(*) FROM 'Hub_inheritance';");
    3. ResultSet rs = sqll.executeQuery();
    4. int r = 0;
    5. while(rs.next()){
    6. r++;
    7. }
     
  14. Offline

    Lurvik

    r will always be 1 if you test that.

    Code:java
    1. Statement stmt = connection.createStatement();
    2. ResultSet rs = stmt.execute("SELECT COUNT(*) FROM database.table, database2.table2");
    3. int = rs.getInt("COUNT(*)");


    That should do what you want.
     
    Tyler Christensen likes this.
  15. TYYYYYYYYY Lurvik You da besttttttt!!!!!!!!!
     
Thread Status:
Not open for further replies.

Share This Page