A batch update operation fails partially. How can you identify which statements failed in the batch?
ACheck the exception message
BUse the int array returned by executeBatch() to check individual update counts
CRe-execute the batch statement by statement
DUse savepoints for each batch statement
Correct Answer:
B. Use the int array returned by executeBatch() to check individual update counts
EXPLANATION
executeBatch() returns an int array where each element represents the update count for the corresponding statement. A value of -3 (Statement.EXECUTE_FAILED) indicates failure for that statement.
Which method is used to check if a CallableStatement has a return value from a stored procedure?
AhasResult()
BwasNull()
CgetReturnValue()
DgetObject(int parameterIndex)
Correct Answer:
B. wasNull()
EXPLANATION
The wasNull() method checks if the last value retrieved from a CallableStatement was NULL. To get the return value, you typically use getInt(), getString(), etc., and then check wasNull().
What is the default transaction isolation level in JDBC when autocommit is disabled?
ATRANSACTION_READ_UNCOMMITTED
BTRANSACTION_READ_COMMITTED
CTRANSACTION_REPEATABLE_READ
DDepends on the database driver
Correct Answer:
D. Depends on the database driver
EXPLANATION
The default isolation level depends on the specific database and JDBC driver configuration. Different databases have different defaults (MySQL is REPEATABLE_READ, Oracle is READ_COMMITTED).
A developer wants to prevent SQL injection attacks while executing dynamic queries. Which approach is most secure?
AUse String concatenation with input validation
BUse PreparedStatement with setString() methods
CUse regular expressions to filter input
DUse encrypted password fields in queries
Correct Answer:
B. Use PreparedStatement with setString() methods
EXPLANATION
PreparedStatement with parameterized queries is the most secure approach as it separates SQL logic from data. The database treats parameters as data, not executable code, preventing SQL injection.
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.
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=?"