Showing 21–30 of 100 questions
in JDBC
Which JDBC driver type is considered the most portable across different databases?
A
Type 1 - JDBC-ODBC Bridge
B
Type 2 - Native API Driver
C
Type 3 - Network Protocol Driver
D
Type 4 - Thin Driver
Correct Answer:
D. Type 4 - Thin Driver
EXPLANATION
Type 4 drivers are pure Java implementations that directly communicate with the database, making them platform-independent and most portable.
In JDBC, what does the BatchUpdate feature allow developers to do?
A
Execute single queries faster
B
Execute multiple queries in a single batch for improved performance
C
Create backup of database
D
Monitor database connections
Correct Answer:
B. Execute multiple queries in a single batch for improved performance
EXPLANATION
addBatch() and executeBatch() methods allow multiple SQL statements to be grouped and executed together, reducing network traffic and improving performance.
Which method of Connection interface is used to start a transaction in JDBC?
A
setAutoCommit(false)
B
beginTransaction()
C
startTransaction()
D
createTransaction()
Correct Answer:
A. setAutoCommit(false)
EXPLANATION
setAutoCommit(false) disables auto-commit mode, allowing manual control over transactions through commit() and rollback() methods.
What is the correct sequence to establish a JDBC connection in Java?
A
Load Driver → Create Connection → Execute Query → Close Connection
B
Create Connection → Load Driver → Execute Query → Close Connection
C
Load Driver → Create Statement → Execute Query → Close Connection
D
Create Statement → Load Driver → Create Connection → Execute Query
Correct Answer:
A. Load Driver → Create Connection → Execute Query → Close Connection
EXPLANATION
The correct JDBC connection sequence is: Class.forName() → DriverManager.getConnection() → Create Statement → Execute Query → Close resources.
Which interface in JDBC is used to execute parameterized queries and prevent SQL injection attacks?
A
Statement
B
PreparedStatement
C
CallableStatement
D
Connection
Correct Answer:
B. PreparedStatement
EXPLANATION
PreparedStatement is used for parameterized queries with placeholders (?), which prevents SQL injection by separating SQL logic from data.
When using JDBC with Spring Framework in 2024-25, which approach is recommended for resource management?
A
Manual try-catch blocks
B
Try-with-resources statement
C
JdbcTemplate with Spring
D
DriverManager only
Correct Answer:
C. JdbcTemplate with Spring
EXPLANATION
JdbcTemplate in Spring Framework automatically manages JDBC resources and is the recommended approach in modern applications.
In JDBC, what is the significance of setting FetchSize on a Statement when dealing with large ResultSets?
A
It determines the maximum number of rows to fetch from database in one round-trip
B
It sets the total memory allocated for ResultSet
C
It automatically closes the ResultSet
D
It enables caching of all rows in memory
Correct Answer:
A. It determines the maximum number of rows to fetch from database in one round-trip
EXPLANATION
FetchSize controls how many rows are fetched from the database at once, optimizing performance for large datasets.
Which JDBC feature should be used for executing multiple SQL statements in a batch for optimal performance?
A
executeUpdate() in a loop
B
executeBatch() with addBatch()
C
execute() method
D
Multiple Statement objects
Correct Answer:
B. executeBatch() with addBatch()
EXPLANATION
executeBatch() with addBatch() reduces network round-trips and improves performance for bulk operations.
In a JDBC application, when using transactions with setAutoCommit(false), what happens to intermediate savepoints if rollback() is called?
A
All changes are saved
B
Only savepoint changes are rolled back
C
All changes since last commit are rolled back
D
The entire connection is closed
Correct Answer:
C. All changes since last commit are rolled back
EXPLANATION
When rollback() is called without savepoints, all changes made since the last commit are undone and rolled back.
Consider a scenario: A developer uses Statement with user input directly in SQL queries. What is the primary risk?
A
Slower query execution
B
SQL Injection attacks
C
Memory leaks
D
Connection timeout
Correct Answer:
B. SQL Injection attacks
EXPLANATION
Direct concatenation of user input in SQL queries exposes the application to SQL injection attacks. PreparedStatement should be used instead.