Which JDBC driver type is platform-independent and does not require native code installation?
AType 1 Driver
BType 2 Driver
CType 3 Driver
DType 4 Driver
Correct Answer:
D. Type 4 Driver
EXPLANATION
Type 4 drivers (thin drivers) are pure Java drivers that communicate directly with the database using the database native protocol. They are platform-independent and do not require native code.
Which of the following is the correct way to close JDBC resources to prevent resource leaks?
AClose ResultSet, Statement, then Connection
BClose Connection only
CUse try-with-resources statement
DBoth A and C are correct
Correct Answer:
D. Both A and C are correct
EXPLANATION
Resources should be closed in reverse order of creation (ResultSet → Statement → Connection), or preferably using try-with-resources which auto-closes resources.
In JDBC, what does the ResultSet.TYPE_SCROLL_INSENSITIVE constant represent?
ANon-scrollable result set
BScrollable result set that doesn't reflect database changes
CForward-only result set
DBidirectional result set
Correct Answer:
B. Scrollable result set that doesn't reflect database changes
EXPLANATION
TYPE_SCROLL_INSENSITIVE creates scrollable ResultSet (can move forward/backward) but changes made to database after ResultSet creation are not reflected.
How would you implement batch processing in JDBC for multiple INSERT operations?
AUse addBatch() and executeBatch() methods
BExecute each insert in a loop separately
CUse multiple connections
DCreate multiple statements
Correct Answer:
A. Use addBatch() and executeBatch() methods
EXPLANATION
addBatch() adds SQL command to batch, executeBatch() executes all batched commands at once, improving performance significantly for multiple operations.
Consider code: String query = "SELECT * FROM users WHERE id=" + userId; stmt.executeQuery(query); What security issue exists?
APerformance degradation
BSQL Injection vulnerability
CMemory leak
DConnection timeout
Correct Answer:
B. SQL Injection vulnerability
EXPLANATION
This is vulnerable to SQL injection as user input is directly concatenated. Using PreparedStatement with parameters prevents this: "SELECT * FROM users WHERE id=?"
What does the setAutoCommit(false) method do in JDBC?
ADisables automatic transaction commit
BEnables automatic transaction commit
CRolls back current transaction
DCloses the connection
Correct Answer:
A. Disables automatic transaction commit
EXPLANATION
setAutoCommit(false) disables automatic commit, allowing multiple statements to be part of single transaction. Manual commit() or rollback() must be called.