Buscar..


Introducción

Un objeto ResultSet mantiene un cursor que apunta a su fila actual de datos. Inicialmente el cursor se coloca antes de la primera fila. El siguiente método mueve el cursor a la siguiente fila, y debido a que devuelve falso cuando no hay más filas en el objeto ResultSet, se puede usar en un bucle while para recorrer el resultado.

Conjunto resultante

Para crear un ResultSet , debe crear una Statement o una Statement PrepapredStatement :

Crear ResultSet con declaración

try {
    Class.forName(driver);
    Connection connection = DriverManager.getConnection(
            "jdbc:somedb://localhost/databasename", "username", "password");
    Statement statement = connection.createStatement();
    ResultSet result = statement.executeQuery("SELECT * FROM my_table");

} catch (ClassNotFoundException | SQLException e) {
}

Crear ResultSet con PrepapredStatement

try {
    Class.forName(driver);
    Connection connection = DriverManager.getConnection(
            "jdbc:somedb://localhost/databasename", "username", "password");
    PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM my_table");
    ResultSet result = preparedStatement.executeQuery();

} catch (ClassNotFoundException | SQLException e) {
}

Compruebe si su ResultSet tiene información o no

if (result.next()) {
   //yes result not empty                
}

Obtener información de ResultSet

Hay varios tipos de información que puede obtener de su ResultSet como String, int, boolean, float, Blob , ... para obtener información que tuvo que usar un bucle o un simple si:

if (result.next()) {
    //get int from your result set
    result.getInt("id");
    //get string from your result set
    result.getString("username");
    //get boolean from your result set
    result.getBoolean("validation");
    //get double from your result set
    result.getDouble("price");
}


Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow