ComenzarEmpieza gratis

Data in a ResultSet

Now that CityBook Libraries can execute queries, you need to extract the actual data from the ResultSet. A ResultSet works like a cursor that moves row by row, and you retrieve column values using getter methods.

Extract the title and publication_year from each book in the results. All necessary imports are available for you.

Este ejercicio forma parte del curso

Querying a PostgreSQL Database in Java

Ver curso

Instrucciones del ejercicio

  • Move through each row in the ResultSet.
  • Retrieve the title and publication_year columns using the appropriate getter methods.

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

public class Main {
    public static void main(String[] args) throws SQLException {
        try (Connection conn = DriverManager.getConnection(Credentials.URL, Credentials.USER, Credentials.PASSWORD);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM books")) {
            // Iterate through each row
            while (rs.____()) {
                // Retrieve column values
                System.out.println(rs.____("title"));
                System.out.println(rs.____("publication_year"));
            }
        }
    }
}
Editar y ejecutar código