Which JDBC feature allows multiple SQL statements to be sent to the database in a single round trip?
ABulk operations
BBatch processing
CTransaction management
DConnection pooling
Correct Answer:
B. Batch processing
EXPLANATION
Batch processing allows multiple SQL statements to be grouped and sent together using addBatch() and executeBatch() methods, improving performance by reducing network overhead.
What is the difference between update() and updateRow() methods in ResultSet?
Aupdate() modifies a single column, updateRow() commits all column changes
Bupdate() is for SELECT queries, updateRow() is for UPDATE queries
Cupdate() is for integers, updateRow() is for all data types
DThere is no difference; they are synonymous
Correct Answer:
A. update() modifies a single column, updateRow() commits all column changes
EXPLANATION
update*() methods (like updateInt(), updateString()) modify individual column values in the current row, while updateRow() commits all these changes to the database.
Which method is used to retrieve metadata about the database in JDBC?
AgetMetaData()
BgetDatabaseMetaData()
CgetDBMetaData()
DfetchMetaData()
Correct Answer:
B. getDatabaseMetaData()
EXPLANATION
The getDatabaseMetaData() method is called on a Connection object to retrieve DatabaseMetaData information about the database, such as table names, column information, etc.
What is the correct way to use try-with-resources for JDBC operations?
Atry(Connection con = DriverManager.getConnection(url)) { }
Btry { Connection con = DriverManager.getConnection(url); }
Ctry-with-resources { Connection con = DriverManager.getConnection(url); }
Dresource(Connection con = DriverManager.getConnection(url)) { }
Correct Answer:
A. try(Connection con = DriverManager.getConnection(url)) { }
EXPLANATION
The try-with-resources statement automatically closes resources implementing AutoCloseable. Since Connection implements AutoCloseable, it will be automatically closed.
Which exception is thrown when a database operation violates a unique constraint in JDBC?
ASQLException
BDataAccessException
CConstraintViolationException
DSQLIntegrityConstraintViolationException
Correct Answer:
D. SQLIntegrityConstraintViolationException
EXPLANATION
SQLIntegrityConstraintViolationException is a specific subclass of SQLException that is thrown when integrity constraint violations occur, such as unique constraint violations.
In JDBC, what is the purpose of the Class.forName() method?
ATo load and register the JDBC driver
BTo establish a database connection
CTo execute a SQL query
DTo fetch data from ResultSet
Correct Answer:
A. To load and register the JDBC driver
EXPLANATION
Class.forName() is used to load the JDBC driver class dynamically and register it with the DriverManager. For example: Class.forName("com.mysql.cj.jdbc.Driver");
What does the PreparedStatement interface provide that Statement does not?
ABetter error handling
BSQL injection prevention through parameterized queries
CFaster execution due to precompilation
DBoth B and C
Correct Answer:
D. Both B and C
EXPLANATION
PreparedStatement provides both parameterized queries (preventing SQL injection) and precompilation of SQL statements, resulting in better security and performance compared to Statement.
Which interface represents a single row of data retrieved from a database in JDBC?
AStatement
BResultSet
CConnection
DDatabaseMetaData
Correct Answer:
B. ResultSet
EXPLANATION
ResultSet interface represents the result set of a query. It contains the data returned from a database query and provides methods to access and traverse through the rows.