CHAPTER 2
JDBC Programming
2.1 The JDBC Connectivity Model
Question: What is the purpose of the setFetchSize() method in JDBC?
Answer:The setFetchSize() method specifies the number of rows to fetch from the database in a single round trip.
Question: How do you use savepoints in JDBC?
Answer:Use the setSavepoint() method to mark a point in a transaction and rollback(Savepoint savepoint) to
roll back to it.
Question: What is the purpose of DatabaseMetaData in JDBC?
Answer:DatabaseMetaData provides information about the database, such as its capabilities, tables, and drivers.
Question: What is the use of the getGeneratedKeys() method?
Answer:The getGeneratedKeys() method retrieves auto-generated keys (like primary keys) after an INSERT operation.
Question: What are row sets in JDBC?
Answer:Row sets are enhanced ResultSet objects that support features like disconnected operation and serialization.
Question: How do you perform a batch update in JDBC?
Answer:Add multiple SQL statements using addBatch() and execute them using executeBatch().
Question: What is the difference between commit() and rollback()?
Answer:commit() saves changes made in a transaction, while rollback() reverts changes.
Question: How do you check if a connection is valid in JDBC?
Answer:Use the isValid(int timeout) method of the Connection interface.
Question: What is the use of rowId in JDBC?
Answer:RowId represents a unique identifier for a row in the database and can be retrieved using getRowId().
Question: What is a Scrollable ResultSet?
Answer:A Scrollable ResultSet allows you to navigate forward and backward through the query results.
Question: How do you retrieve metadata about a ResultSet?
Answer:Use the getMetaData() method of the ResultSet object.
Question: What are the different types of statements in JDBC?
Answer:The types are Statement, PreparedStatement, and CallableStatement.
Question: How do you update data in a ResultSet?
Answer:Use ResultSet methods like updateString(), updateInt(), and updateRow() to modify data.
Question: What is connection pooling in JDBC?
Answer:Connection pooling is a technique to reuse database connections for better performance and resource management.
Question: How does JDBC support batch processing?
Answer:JDBC supports batch processing using the addBatch() and executeBatch() methods for bulk updates.
Question: How do you execute stored procedures in JDBC?
Answer:Use CallableStatement to execute stored procedures.
Question: What is the role of ParameterMetaData in JDBC?
Answer:ParameterMetaData provides information about parameters in a PreparedStatement.
Question: How can you retrieve database-specific keywords in JDBC?
Answer:Use the getSQLKeywords() method of the DatabaseMetaData object.
Question: How do you handle large binary data in JDBC?
Answer:Use Blob for binary data and Clob