Home Subjects Java Programming JDBC

Java Programming
JDBC

Java OOP, collections, multithreading

29 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 29
Topics in Java Programming
Q.11 Easy JDBC
What is the correct syntax to load a JDBC driver in Java?
A DriverManager.loadDriver('com.mysql.jdbc.Driver');
B Class.forName('com.mysql.jdbc.Driver');
C Driver.load('com.mysql.jdbc.Driver');
D Connection.loadDriver('com.mysql.jdbc.Driver');
Correct Answer:  B. Class.forName('com.mysql.jdbc.Driver');
EXPLANATION

Class.forName() is the standard way to load JDBC drivers. It dynamically loads the driver class.

Test
Q.12 Easy JDBC
Which JDBC API is used to execute parameterized queries safely?
A Statement
B PreparedStatement
C ResultSet
D MetaData
Correct Answer:  B. PreparedStatement
EXPLANATION

PreparedStatement allows parameterized queries using placeholders (?) and prevents SQL injection attacks.

Test
Q.13 Easy JDBC
Which interface in JDBC is used to execute SQL queries and retrieve results?
A Statement
B Connection
C ResultSet
D Driver
Correct Answer:  A. Statement
EXPLANATION

Statement interface is used to execute SQL queries. Connection creates statements, ResultSet holds results, and Driver manages connections.

Test
Q.14 Easy JDBC
What is the purpose of the Connection.commit() method in JDBC?
A To establish a new database connection
B To permanently save all changes made in the current transaction
C To execute the next SQL statement
D To close the database connection
Correct Answer:  B. To permanently save all changes made in the current transaction
EXPLANATION

commit() finalizes the transaction and persists all changes made since the last commit or rollback.

Test
Q.15 Easy JDBC
In JDBC, which exception is thrown when the database driver class is not found?
A SQLException
B ClassNotFoundException
C DriverNotFoundException
D DatabaseException
Correct Answer:  B. ClassNotFoundException
EXPLANATION

ClassNotFoundException is thrown when Class.forName() cannot locate the JDBC driver class.

Test
Q.16 Easy JDBC
What does the DriverManager.getConnection() method return?
A A Statement object
B A Connection object
C A ResultSet object
D A DatabaseMetaData object
Correct Answer:  B. A Connection object
EXPLANATION

DriverManager.getConnection() establishes a connection to the database and returns a Connection object.

Test
Q.17 Easy JDBC
Which JDBC method is used to retrieve the number of rows affected by an INSERT, UPDATE, or DELETE statement?
A getRowCount()
B executeUpdate()
C getUpdateCount()
D fetchSize()
Correct Answer:  B. executeUpdate()
EXPLANATION

executeUpdate() returns an int representing the number of rows affected by the SQL statement execution.

Test
Q.18 Easy JDBC
What is the primary advantage of using PreparedStatement over Statement in JDBC?
A Better performance and protection against SQL injection
B Ability to execute multiple queries simultaneously
C Automatic connection pooling
D Direct transaction management
Correct Answer:  A. Better performance and protection against SQL injection
EXPLANATION

PreparedStatement pre-compiles SQL and uses parameterized queries, preventing SQL injection and improving performance.

Test
Q.19 Easy JDBC
Which interface in JDBC is used to execute a single SQL statement and return a ResultSet?
A Statement
B PreparedStatement
C CallableStatement
D Connection
Correct Answer:  A. Statement
EXPLANATION

Statement interface is used to execute simple SQL queries without parameters and return results as ResultSet.

Test
Q.20 Easy JDBC
What happens when you call rs.next() on a ResultSet after the last row?
A Returns true and loops back to the first row
B Returns false
C Throws a SQLException
D Remains on the last row
Correct Answer:  B. Returns false
EXPLANATION

The next() method returns false when there are no more rows available. It does not throw an exception or loop back; it simply indicates that there are no additional rows to traverse.

Test
IGET
IGET AI
Online · Exam prep assistant
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