What happens when Connection.setAutoCommit(false) is called in JDBC?
A Auto-commit is enabled immediately B Manual transaction control is enabled; changes require explicit commit C All pending transactions are rolled back D The connection is closed automatically
Setting autoCommit to false enables manual transaction management where commit() must be explicitly called.
In JDBC, what is the purpose of the ResultSetMetaData interface?
A To execute SQL queries B To retrieve information about the structure of a ResultSet C To manage database connections D To handle transaction management
ResultSetMetaData provides information about columns, data types, and column properties of a ResultSet.
Which JDBC constant specifies read-only access for a ResultSet?
A ResultSet.CONCUR_READ_ONLY B ResultSet.TYPE_READ_ONLY C ResultSet.MODE_READ D ResultSet.ACCESS_READ_ONLY
CONCUR_READ_ONLY is the concurrency constant that specifies a read-only ResultSet.
What is Connection pooling in JDBC and why is it used?
A A method to execute multiple SQL statements in parallel B Reusing database connections to improve performance and reduce overhead C A feature to automatically backup database connections D A mechanism to encrypt database connections
Connection pooling maintains a pool of reusable connections, reducing the cost of creating new connections repeatedly.
In JDBC, what is the purpose of CallableStatement?
A To execute simple SELECT queries B To execute stored procedures and functions C To batch multiple SQL statements D To manage transaction isolation levels
CallableStatement is used to call stored procedures and stored functions in the database.
A developer creates a JDBC connection but forgets to close it. What might be the consequence?
A Automatic garbage collection will close it immediately B Resource leak leading to exhaustion of database connections C The program will automatically rollback all transactions D No impact as JDBC handles it automatically
Unclosed connections remain allocated, eventually exhausting the connection pool and causing application failures.
Consider a scenario where a developer retrieves a ResultSet and then closes the Statement object. What happens to the ResultSet?
A The ResultSet remains valid and can still be accessed B The ResultSet becomes invalid in most drivers (implementation-dependent) C An immediate SQLException is thrown D The database automatically commits pending changes
Closing a Statement typically invalidates its associated ResultSet in most JDBC drivers, though behavior may vary.
What is the optimal approach to handle JDBC resources to prevent memory leaks in a production application?
A Use try-catch blocks and manually close resources B Rely on garbage collection to close resources C Use try-with-resources statement to auto-close resources D Create resources globally and close them at application shutdown
Try-with-resources (try-with-resources statement) automatically closes AutoCloseable resources, preventing memory leaks.
In a multi-threaded JDBC application, why should each thread have its own Connection object?
A To improve database performance B Connections are not thread-safe and sharing causes race conditions C To reduce memory usage D It is just a best practice with no technical requirement
JDBC Connections are not thread-safe. Each thread must have its own Connection to prevent concurrent access issues and data corruption.
Which interface in JDBC is used to execute SQL queries and retrieve results?
A Statement B Connection C ResultSet D Driver
Statement interface is used to execute SQL queries. Connection creates statements, ResultSet holds results, and Driver manages connections.
Which JDBC API is used to execute parameterized queries safely?
A Statement B PreparedStatement C ResultSet D MetaData
PreparedStatement allows parameterized queries using placeholders (?) and prevents SQL injection attacks.
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');
Class.forName() is the standard way to load JDBC drivers. It dynamically loads the driver class.
Which method is used to retrieve the next row from a ResultSet?
A getNext() B next() C moveToNext() D fetchNext()
The next() method moves the cursor to the next row in the ResultSet and returns true if a row exists.
What exception is thrown when a database connection fails in JDBC?
A IOException B SQLException C ClassNotFoundException D NullPointerException
SQLException is thrown when database-related errors occur, including connection failures and SQL execution errors.
In JDBC, what does the executeQuery() method return?
A int B boolean C ResultSet D void
executeQuery() returns a ResultSet containing the rows fetched from the database for SELECT queries.
What is the purpose of Connection.commit() in JDBC?
A Close the connection B Save all changes made in the current transaction C Rollback transactions D Create a new statement
commit() saves all changes made during the current transaction permanently to the database.
Which JDBC method executes UPDATE, INSERT, or DELETE statements?
A executeQuery() B execute() C executeUpdate() D executeBatch()
executeUpdate() executes DML statements and returns the number of rows affected.
What is the benefit of using PreparedStatement over Statement for repeated queries?
A Faster execution due to pre-compilation B Automatic SQL injection prevention C Better readability of code D Both A and B
PreparedStatement offers both pre-compilation for performance and SQL injection prevention through parameterized queries.
What does ResultSetMetaData.getColumnCount() return?
A Number of rows in ResultSet B Number of columns in ResultSet C Column data type D Column name
getColumnCount() returns the total number of columns present in the ResultSet.
In JDBC, which class is used to retrieve database metadata information?
A ResultSet B Statement C DatabaseMetaData D Connection
DatabaseMetaData provides information about the database, tables, columns, and other database properties.