chapter 15 - JDBC Flashcards
Which interface can be used to execute a stored procedure on the database?
The CallableStatement interface in Java is used to execute stored procedures on a database. It is part of the java.sql package and allows calling both stored procedures and functions.
JDBC stands for Java Database Connectivity and is used to connect Java applications to databases.
true
In JDBC, the Connection interface is used to send SQL statements to the database.
False (The Statement interface is used to send SQL statements, while Connection is used to establish a database connection.)
The DriverManager class is responsible for loading database drivers and establishing connections.
True
The ResultSet interface in JDBC is used to update the structure of the database tables.
False (ResultSet is used to retrieve and navigate query results, not update table structure.)
JDBC in Java 17 supports try-with-resources to automatically close resources such as Connection, Statement, and ResultSet.
True.
What does the execute() method of PreparedStatement do?
It executes the SQL statement and returns true if the result is a ResultSet, or false if the result is an update count or no result.
When should you use execute() instead of executeQuery() or executeUpdate()?
Use execute() when the SQL could return either a ResultSet or an update count — such as dynamic SQL or stored procedures.
After calling execute(), how do you access the result?
Use getResultSet() to access a ResultSet, or getUpdateCount() for the number of affected rows. Use getMoreResults() for additional results.
What type of SQL statements should you use executeQuery() for?
You should use executeQuery() for SQL statements that return data, typically SELECT queries. It returns a ResultSet containing the query results.
What happens if you use executeQuery() with an UPDATE, INSERT, or DELETE statement?
If you incorrectly use executeQuery() with a statement that doesn’t return a ResultSet (like UPDATE or INSERT), it throws an SQLException.
After calling executeQuery(), how do you retrieve the results?
The results are stored in a ResultSet object, which you can iterate through using methods like rs.next() and access columns via rs.getString(), rs.getInt(), etc.