Which SQL statement needs a ResultSet to process return data? Update, Select, Insert, Delete? In JDBC
1 Answers
0
votes
A ResultSet represents the database result set of a query. See https://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html
Here is an example:
Statement statement = connection.createStatement();
String sql = "select * from users";
ResultSet result = statement.executeQuery(sql);
You can use the ResultSet to get the data from the table
while(result.next()) {
String name = result.getString("name");
System.out.println(name);
}
...or to update an entry
result.absolute(5); // moves the cursor to the fifth row of rs
result.updateString("name", "Cthullhu");
result.updateRow();
After use, you should close the ResultSet and the Statement
result.close();
statement.close();
You use a ResultSet after a select query using execute on the statement.
If you have update, insert or delete you can use executeUpdate on the statement
See this introduction from Oracle: https://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html#creating_statements
executeQuery()will succeed and not throw an exception. It is usually select, but database system also have other statements or constructs that return a result set (this varies per database system). - Mark Rotteveel