Java Programming — JDBC
Java OOP, collections, multithreading
46 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 46 questions in JDBC
Q.1 Medium JDBC
In JDBC 2024-25, which exception is thrown when attempting to use a closed Connection object?
A SQLException
B SQLClientInfoException
C SQLInvalidAuthorizationSpecException
D SQLNonTransientConnectionException
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.

Take Test
Q.2 Medium JDBC
When using ResultSet in JDBC, which cursor type allows bidirectional movement through rows but does not reflect database changes?
A TYPE_FORWARD_ONLY
B TYPE_SCROLL_INSENSITIVE
C TYPE_SCROLL_SENSITIVE
D TYPE_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.

Take Test
Q.3 Medium JDBC
A developer needs to execute the same SQL query multiple times with different parameters. Which JDBC feature should be used to optimize performance?
A Statement with loop execution
B PreparedStatement with parameterized queries
C CallableStatement for all queries
D Multiple 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.

Take Test
Q.4 Medium JDBC
Which JDBC feature allows monitoring of database metadata like table structure and column information?
A DatabaseMetaData interface
B ResultSetMetaData interface
C ParameterMetaData interface
D StatementMetaData 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.

Take Test
Q.5 Medium JDBC
What is the correct way to handle resource management in JDBC with Java 7+?
A try-catch-finally blocks with explicit close()
B try-with-resources statement (try-with parentheses)
C Using finally blocks only
D Automatic garbage collection handles it
Correct Answer:  B. try-with-resources statement (try-with parentheses)
EXPLANATION

try-with-resources automatically closes AutoCloseable resources (Connection, Statement, ResultSet), preventing connection leaks elegantly.

Take Test
Advertisement
Q.6 Medium JDBC
In JDBC 2024-25, which data type mapping is incorrect for Java to SQL?
A int → INTEGER
B String → VARCHAR
C boolean → BOOLEAN
D byte[] → CHAR
Correct Answer:  D. byte[] → CHAR
EXPLANATION

byte[] should map to BLOB (Binary Large Object), not CHAR. CHAR is for single character types.

Take Test
Q.7 Medium JDBC
Which method in ResultSet is used to check if a column value is NULL?
A isNull()
B wasNull()
C checkNull()
D getNullValue()
Correct Answer:  B. wasNull()
EXPLANATION

wasNull() returns true if the last retrieved value was NULL in the database, checked after calling a getter method like getString() or getInt().

Take Test
Q.8 Medium JDBC
In a multi-threaded JDBC application, what should be the approach for Connection object usage?
A Share single Connection object across all threads
B Each thread should use its own Connection from the pool
C Create new Connection for each database operation
D Use static Connection objects
Correct Answer:  B. Each thread should use its own Connection from the pool
EXPLANATION

Connection objects are not thread-safe. Each thread should obtain its own Connection from a thread-safe connection pool to avoid race conditions.

Take Test
Q.9 Medium JDBC
What does the setMaxRows() method in Statement do?
A Limits the number of rows returned by a query
B Sets the maximum number of connections
C Defines batch size for updates
D Controls transaction timeout
Correct Answer:  A. Limits the number of rows returned by a query
EXPLANATION

setMaxRows() limits the number of rows returned by executeQuery(), useful for memory management when dealing with large result sets.

Take Test
Q.10 Medium JDBC
Consider a scenario where a developer uses getConnection() without closing it. What is the potential impact?
A Database performance remains unaffected
B Connection leak occurs, exhausting available database connections
C Automatic rollback happens
D Query execution becomes slower temporarily
Correct Answer:  B. Connection leak occurs, exhausting available database connections
EXPLANATION

Unclosed connections remain allocated and unavailable for other operations, eventually exhausting the connection pool and causing connection exhaustion errors.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips