Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To retrieve the length (number of rows) of SQL results using Java, you can use the ResultSet.getRow() method. This method returns the current row number within the ResultSet. You can iterate through the ResultSet using a while loop to count the number of rows. Here's an example code snippet:

try {
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase", "username", "password");
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");

    int rowCount = 0;
    while (rs.next()) {
        rowCount++;
    }

    System.out.println("Number of rows: " + rowCount);

    rs.close();
    stmt.close();
    conn.close();
} catch (SQLException e) {
    e.printStackTrace();
}

In this example, we create a connection to a MySQL database and execute a SELECT query to retrieve all rows from a table called "mytable". We then use a while loop to iterate through the ResultSet and increment a rowCount variable for each row. Finally, we print out the number of rows to the console.