Java Programming — JDBC
Java OOP, collections, multithreading
29 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 29 questions in JDBC
Q.1 Easy 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.

Take Test
Q.2 Easy JDBC
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.

Take Test
Q.3 Easy JDBC
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.

Take Test
Q.4 Easy JDBC
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.

Take Test
Q.5 Easy JDBC
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.

Take Test
Advertisement
Q.6 Easy JDBC
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.

Take Test
Q.7 Easy JDBC
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.

Take Test
Q.8 Easy JDBC
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.

Take Test
Q.9 Easy JDBC
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.

Take Test
Q.10 Easy JDBC
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.

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