How do we execute a PreparedStatement?

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();


A. executeQuery, execute, executeUpdate
B. executeQuery, executeUpdate, execute
C. executeUpdate, execute, executeQuery
D. executeUpdate, executeQuery, execute
E. execute, executeQuery, executeUpdate
F. execute, executeUpdate, executeQuery


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.


Leave a Reply

Your email address will not be published. Required fields are marked *