Java 11 Certification Practice Questions (Answers below)
Topic: Database Applications with JDBC
Connect to and perform database SQL operations, process query results using JDBC API
Q1.Given the following code.
Choose the options that can be filled in to make the code compile?
var sql = "SELECT * FROM users";
try (var conn = DriverManager.getConnection(args[0]); var ps = conn.prepareStatement(sql)) {
ResultSet rs = ps.executeQuery();
boolean val = ps.execute();
int i = ps.executeUpdate();
}
Choices
A. executeQuery, execute, executeUpdate
B. executeQuery, executeUpdate, execute
C. executeUpdate, execute, executeQuery
D. executeUpdate, executeQuery, execute
E. execute, executeQuery, executeUpdate
F. execute, executeUpdate, executeQuery
Answer
A is the correct answer. The executeQuery() method execute statements that returns a result set by fetching some data from the database ie; only select statements. The execute() method can run either a query or an update and returns a boolean. The executeUpdate() method can be used to modify data in a table and it returns the number of rows that were inserted, deleted, or changed.
Reference
https://docs.oracle.com/en/java/javase/11/docs/api/java.sql/java/sql/PreparedStatement.html
https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html