Ricerca…
introduzione
Un oggetto ResultSet mantiene un cursore che punta alla sua attuale riga di dati. Inizialmente il cursore è posizionato prima della prima riga. Il prossimo metodo sposta il cursore sulla riga successiva e poiché restituisce false quando non ci sono più righe nell'oggetto ResultSet, può essere utilizzato in un ciclo while per scorrere il risultato
ResultSet
Per creare un ResultSet
è necessario creare una Statement
o PrepapredStatement
:
Creare ResultSet con Statement
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) {
}
Creare 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) {
}
Controlla se il tuo ResultSet ha informazioni o no
if (result.next()) {
//yes result not empty
}
Ottieni informazioni da ResultSet
Ci sono diversi tipi di informazioni che puoi ottenere dal tuo ResultSet
come String, int, boolean, float, Blob
, ... per ottenere informazioni hai bisogno di usare un ciclo o un semplice se:
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
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow