To create a ResultSet
you should to create a Statement
or PrepapredStatement
:
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) {
}
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) {
}
if (result.next()) {
//yes result not empty
}
There are several type of information you can get from your ResultSet
like String, int, boolean, float, Blob
, ... to get information you had to use a loop or a simple if :
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");
}