JDBC Questions with Answers

26 practice questions on JDBC (Advanced Java) with answers and step-by-step solutions. Try 10 samples below, then practise all 26 free in the AptiNest app.

Sample questions

Question 1Easy

Which JDBC type is the best defence against SQL injection when building queries from user input?

  1. Statement with string concatenation
  2. ResultSet
  3. DriverManager with a database URL
  4. PreparedStatement

Answer: D. PreparedStatement

Explanation

PreparedStatement sends the SQL and values separately, so input is treated as data and not as SQL.

Step-by-step solution

  1. Write SQL with? placeholders.
  2. Bind values with setXxx.
  3. Database treats them as data.

Why the other options are wrong

  • A. Statement concatenates input into the SQL text, which is vulnerable.
  • B. ResultSet only reads rows.
  • C. DriverManager only opens connections.

Tip: Never concatenate user input.

Question 2Easy

What is the index of the first column in rs.getString(1) for a ResultSet?

  1. 1
  2. 0
  3. -1
  4. It depends on the driver

Answer: A. 1

Explanation

JDBC columns are numbered starting at 1, unlike Java arrays.

Step-by-step solution

  1. Run SELECT.
  2. First column has index 1.
  3. Index 0 throws SQLException.

Why the other options are wrong

  • B. JDBC column indexes are not zero-based.
  • C. Negative values are invalid.
  • D. The standard fixes the first column at 1.

Question 3Easy

What does executeUpdate() return for an INSERT statement?

  1. A ResultSet of the new rows
  2. The number of rows affected
  3. true if it succeeded
  4. The generated primary key

Answer: B. The number of rows affected

Explanation

executeUpdate returns an int row count for INSERT, UPDATE and DELETE, or 0 for DDL.

Step-by-step solution

  1. Run the DML statement.
  2. Driver returns the affected row count.

Why the other options are wrong

  • A. It returns an int, not a ResultSet.
  • C. A boolean is returned by execute().
  • D. Keys are fetched with getGeneratedKeys, not the return value.

Question 4Medium

How do you use a savepoint?

  1. A savepoint is realised by calling setAutoCommit(true) on the connection
  2. A savepoint is activated by the method setSavePoint(mysavepoint) on the transac-tion
  3. A savepoint is used to mark intermediate points inside a transaction, in order to get a more fine- grained control. Transactions can be rolled back to a previous savepoint with-out affecting preceding work
  4. A savepoint triggers an automatic synchronisation with the database.

Answer: C. A savepoint is used to mark intermediate points inside a transaction, in order to get a more fine- grained control. Transactions can be rolled back to a previous savepoint with-out affecting preceding work

Explanation

The SAVEPOINT statement names and marks the current point in the processing of a transaction. With the ROLLBACK TO statement, savepoints undo parts of a transaction instead of the whole transaction To set the savepoint setSavepoint( ) Method is used with Connection object.

Example:

Savepoint spt1 = con.setSavepoint("svpt1");

Step-by-step solution

  1. A savepoint marks a point inside a JDBC transaction.
  2. Rolling back to a savepoint undoes only the work done after that savepoint.
  3. Work done before the savepoint is kept, which gives finer control than rolling back the whole transaction.
  4. So the answer is A savepoint is used to mark intermediate points inside a transaction, in order to get a more fine-grained control. Transactions can be rolled back to a previous savepoint without affecting preceding work.

Why the other options are wrong

  • A. setAutoCommit(true) turns auto-commit on; it does not create a savepoint.
  • B. There is no such call; savepoints are created with setSavepoint on a Connection.
  • D. A savepoint does not commit or synchronise work with the database; it is only a marker for rollback.

Tip: A savepoint is a rollback marker inside a transaction.

Question 5Medium

Why is a connection pool (via DataSource) preferred over DriverManager.getConnection per request?

  1. Pooled connections need no credentials because they are anonymous
  2. DriverManager cannot connect to MySQL or any real database server
  3. Opening a database connection is expensive, so pooled connections are reused
  4. Pooling removes the need to close connections

Answer: C. Opening a database connection is expensive, so pooled connections are reused

Explanation

Pools keep a set of open connections ready and hand them out, saving connect time and bounding database load.

Step-by-step solution

  1. Pool opens connections once.
  2. Request borrows one.
  3. close() returns it to the pool.

Why the other options are wrong

  • A. Credentials are still configured in the pool.
  • B. DriverManager works with any driver.
  • D. You still call close(), which returns it to the pool.

Question 6Medium

What is correct about DDL statements (create, grant,...)?

  1. DDL statements are treated as normal SQL statements, and are executed by calling the execute() method on a Statement (or a sub interface thereof) object
  2. To execute DDL statements, you have to install additional support files
  3. DDL statements can not be executed by making use of JDBC, you should use the native database tools for this.
  4. Support for DDL statements will be a feature of a future release of JDBC

Answer: A. DDL statements are treated as normal SQL statements, and are executed by calling the execute() method on a Statement (or a sub interface thereof) object

Explanation

Data definition language (DDL) is the set of SQL statements (ALTER, CREATE, DROP, GRANT) that let you create, alter, or destroy (drop) the objects that make up a relational database
DDL statements are treated as normal SQL statements, and are executed by calling the execute() method on a Statement (or a sub interface thereof) object

Step-by-step solution

  1. DDL stands for Data Definition Language.
  2. It includes commands like CREATE, GRANT, DROP.
  3. These commands modify the structure of the database.
  4. They can be executed using JDBC's Statement or PreparedStatement objects.
  5. The execute() method is used to run these commands.
  6. Option A correctly states that DDL statements are treated as normal SQL statements and executed via the execute() method.

Why the other options are wrong

  • B. No additional support files are needed.
  • C. JDBC supports executing DDL statements.
  • D. Support for DDL is already available in JDBC.

Tip: Use execute() method to run DDL statements with JDBC.

Question 7Medium

What information may be obtained from a ResultSetMetaData object?

  1. Number of columns in the result set
  2. Number of rows in the result set
  3. Database URL and product name
  4. JDBC driver name and version

Answer: A. Number of columns in the result set

Explanation

ResultSetMetaData describes the result set itself, such as the number of columns, their names and their types. The database URL and driver details come from other objects, not from the result set.

Step-by-step solution

  1. ResultSetMetaData describes the columns of a ResultSet.
  2. Its getColumnCount method returns the number of columns in the result set.
  3. Row count is not given by this metadata; rows must be counted by stepping through the result.
  4. Database URL and product name come from DatabaseMetaData, and driver name and version also come from DatabaseMetaData.
  5. The answer is Number of columns in the result set.

Why the other options are wrong

  • B. Row count is not provided by ResultSetMetaData; rows must be counted by iterating.
  • C. Database URL and product name come from DatabaseMetaData, not ResultSetMetaData.
  • D. Driver name and version come from DatabaseMetaData, not ResultSetMetaData.

Tip: ResultSetMetaData is about columns; DatabaseMetaData is about the database.

Question 8Medium

Which Driver is pure Java Driver

  1. Type 1 Driver
  2. Type 2 Driver
  3. Type 3 Driver
  4. Type 4 Driver

Answer: D. Type 4 Driver

Explanation

In a Type 4 driver, a pure Java-based driver that communicates directly with vendor's database through socket connection. This is the highest performance driver available for the database and is usually provided by the vendor itself.
This kind of driver is extremely flexible, you don't need to install special software on the client or server. Further, these drivers can be downloaded dynamically.

Step-by-step solution

  1. JDBC drivers come in four types.
  2. Type 1 uses the JDBC-ODBC bridge, which relies on native code.
  3. Type 2 uses native database client libraries, and Type 3 uses a middleware server.
  4. Type 4 is written entirely in Java and talks directly to the database, so it is the pure Java driver: Type 4 Driver.

Why the other options are wrong

  • A. Type 1 uses the JDBC-ODBC bridge, which relies on native code.
  • B. Type 2 relies on native database client libraries.
  • C. Type 3 uses a middleware server, so it is not pure Java.

Tip: Type 4 is the pure Java driver.

Question 9Medium

How do you know in your Java program that a SQL warning is generated as a result of execut-ing a SQL statement in the database?

  1. You must catch the checked SQLException which is thrown by the method which exe-cutes the statement
  2. You must catch the unchecked SQLWarningException which is thrown by the method which executes the statement
  3. You must invoke the getWarnings() method on the Statement object (or a sub interface thereof)
  4. You must query the ResultSet object about possible warnings generated by the database

Answer: C. You must invoke the getWarnings() method on the Statement object (or a sub interface thereof)

Explanation

In JDBC the Connection, Statement, and ResultSet types each have a getWarnings() method that is specified to produce the first warning associated with objects of that type. The second and subsequent warnings, if they exist, are chained onto the first warning (if it even exists, null is produced if there are no warnings).On the other hand, you must explicitly check for any SQL warnings, because they aren't propagated via the standard exception-handling mechanisms. To do so, call the appropriate getWarnings method on the relevant JDBC object.

Step-by-step solution

  1. A SQL warning is not thrown as an exception, so it cannot be caught as a SQLException.
  2. Warnings are stored on the Statement object and can be read with getWarnings().
  3. So you must invoke getWarnings() on the Statement, or on a sub interface of it.
  4. So the answer is You must invoke the getWarnings() method on the Statement object (or a sub interface thereof).

Why the other options are wrong

  • A. SQLWarning is not thrown as a checked SQLException, so there is nothing to catch.
  • B. SQLWarningException is not a class in JDBC.
  • D. Warnings are stored on the statement, not on the ResultSet.

Tip: For JDBC warnings, think getWarnings() on the Statement or Connection.

Question 10Medium

Which packages contain the JDBC classes?

  1. java.jdbc and javax.jdbc
  2. java.jdbc and java.jdbc.sql
  3. java.sql and javax.sql
  4. java.rdb and javax.rdb

Answer: C. java.sql and javax.sql

Explanation

The JDBC core API is in the java.sql package, and the JDBC standard extensions such as DataSource are in javax.sql.

Step-by-step solution

  1. The JDBC classes are in the standard Java packages java.sql and javax.sql.
  2. java.sql holds the core JDBC API, such as Connection and Statement.
  3. javax.sql holds the extension API, such as DataSource.
  4. So the correct pair is java.sql and javax.sql.
  5. The answer is java.sql and javax.sql.

Why the other options are wrong

  • A. java.jdbc and javax.jdbc are not the JDBC packages.
  • B. java.jdbc and java.jdbc.sql are not the JDBC packages.
  • D. java.rdb and javax.rdb are not the JDBC packages.

Tip: JDBC lives in java.sql, and javax.sql adds the extras.

Practise all 26 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Advanced Java topics · All topics