Showing 81–90 of 270 questions
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.
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.
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.
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.
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.
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.
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.
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.
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.