Showing 1–10 of 29 questions
in JDBC
In a JDBC application, which interface is responsible for executing SQL queries and returning ResultSet objects?
A
Statement
B
Connection
C
Driver
D
DataSource
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.
What is the difference between commit() and rollback() in JDBC transactions?
A
commit() saves changes, rollback() undoes them
B
Both perform the same function
C
rollback() is faster than commit()
D
commit() is used for SELECT queries, rollback() for UPDATE
Correct Answer:
A. commit() saves changes, rollback() undoes them
EXPLANATION
commit() permanently saves all changes made during the transaction, while rollback() discards all changes and reverts to the previous state.
Which exception is thrown when a JDBC driver is not found in the classpath?
A
SQLException
B
ClassNotFoundException
C
DriverNotFoundException
D
JDBCException
Correct Answer:
B. ClassNotFoundException
EXPLANATION
When Class.forName() cannot locate the driver class, it throws ClassNotFoundException, indicating the driver JAR is not in the classpath.
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.
In JDBC, what does the executeQuery() method return?
A
int
B
boolean
C
ResultSet
D
void
Correct Answer:
C. ResultSet
EXPLANATION
executeQuery() returns a ResultSet containing the rows fetched from the database for SELECT queries.
What exception is thrown when a database connection fails in JDBC?
A
IOException
B
SQLException
C
ClassNotFoundException
D
NullPointerException
Correct Answer:
B. SQLException
EXPLANATION
SQLException is thrown when database-related errors occur, including connection failures and SQL execution errors.
Which method is used to retrieve the next row from a ResultSet?
A
getNext()
B
next()
C
moveToNext()
D
fetchNext()
Correct Answer:
B. next()
EXPLANATION
The next() method moves the cursor to the next row in the ResultSet and returns true if a row exists.