In JDBC metadata operations, which method is used to retrieve information about table structure, column names, and their data types?
AgetDatabaseMetaData()
BgetMetaData() on ResultSet
CgetColumnCount() and getColumnName()
DBoth B and C together
Correct Answer:
D. Both B and C together
EXPLANATION
getMetaData() on ResultSet returns ResultSetMetaData which provides column information. getColumnCount() and getColumnName() are methods of ResultSetMetaData used to retrieve structure details.
A Java application requires connection pooling to handle 1000+ concurrent database requests efficiently. Which JDBC component should be implemented?
ADirect DriverManager.getConnection() calls
BDataSource interface with connection pool implementation like HikariCP or C3P0
CCreating new Connection for each request manually
DUsing Statement caching only
Correct Answer:
B. DataSource interface with connection pool implementation like HikariCP or C3P0
EXPLANATION
DataSource with connection pooling frameworks (HikariCP, C3P0) efficiently manages connections by reusing them, reducing overhead. Direct getConnection() calls create new connections each time, which is inefficient for high concurrency.
In JDBC 2024-25, which exception is thrown when attempting to use a closed Connection object?
ASQLException
BSQLClientInfoException
CSQLInvalidAuthorizationSpecException
DSQLNonTransientConnectionException
Correct Answer:
A. SQLException
EXPLANATION
A SQLException (specifically a SQLNonTransientConnectionException as a subclass) is thrown when operations are attempted on a closed Connection. SQLException is the general exception for database access errors.
When using ResultSet in JDBC, which cursor type allows bidirectional movement through rows but does not reflect database changes?
ATYPE_FORWARD_ONLY
BTYPE_SCROLL_INSENSITIVE
CTYPE_SCROLL_SENSITIVE
DTYPE_DYNAMIC
Correct Answer:
B. TYPE_SCROLL_INSENSITIVE
EXPLANATION
TYPE_SCROLL_INSENSITIVE allows movement in both directions (previous, next, absolute) but doesn't reflect changes made to the database after the ResultSet was created. TYPE_SCROLL_SENSITIVE would reflect changes.
A developer needs to execute the same SQL query multiple times with different parameters. Which JDBC feature should be used to optimize performance?
AStatement with loop execution
BPreparedStatement with parameterized queries
CCallableStatement for all queries
DMultiple Connection objects
Correct Answer:
B. PreparedStatement with parameterized queries
EXPLANATION
PreparedStatement pre-compiles the query and allows parameter binding, reducing overhead and improving performance for repeated executions. It also prevents SQL injection.
In a JDBC application, which interface is responsible for executing SQL queries and returning ResultSet objects?
AStatement
BConnection
CDriver
DDataSource
Correct Answer:
A. Statement
EXPLANATION
The Statement interface is used to execute SQL queries. Connection creates the statement, Driver manages connections, and DataSource provides connection pooling, but Statement is the actual executor.
In a JDBC application using batch updates, what happens if one statement in the batch fails?
AAll statements are executed, and failure is reported
BAll statements are rolled back automatically
COnly failed statement is skipped
DBehavior depends on database configuration
Correct Answer:
D. Behavior depends on database configuration
EXPLANATION
executeBatch() behavior on failure varies by database and driver. BatchUpdateException is thrown, containing update counts for each statement. Explicit transaction control is recommended.
Which JDBC feature allows monitoring of database metadata like table structure and column information?
ADatabaseMetaData interface
BResultSetMetaData interface
CParameterMetaData interface
DStatementMetaData interface
Correct Answer:
A. DatabaseMetaData interface
EXPLANATION
DatabaseMetaData (obtained from Connection.getMetaData()) provides information about the entire database like tables, columns, keys, and supported features.
A query execution takes 5 seconds consistently. Which JDBC optimization technique should NOT be used for this scenario?
AConnection pooling
BBatch processing for multiple inserts
CUsing indexes on frequently queried columns
DIncreasing setFetchSize() value
Correct Answer:
D. Increasing setFetchSize() value
EXPLANATION
If query execution itself is slow (5 seconds), the bottleneck is in database operations, not data transfer. Increasing setFetchSize() only optimizes data retrieval, not query execution.