JDBC Practice Questions with Answers

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

Sample questions

Question 1Easy

In PreparedStatement ps = con.prepareStatement("UPDATE t SET name = ? WHERE id = ?");, which call binds the id value 7?

  1. ps.setInt(1, 7)
  2. ps.setInt(2, 7)
  3. ps.setInt(0, 7)
  4. ps.setInt("id", 7)

Answer: B. ps.setInt(2, 7)

Explanation

Placeholders are numbered from 1 in the order they appear. The second ? is the id, so index 2.

Step-by-step solution

  1. First ? is index 1 (name).
  2. Second ? is index 2 (id).
  3. Call setInt(2, 7).

Why the other options are wrong

  • A. Index 1 is the name placeholder.
  • C. JDBC parameter indexes start at 1, not 0.
  • D. Named binding is not supported by PreparedStatement.

Question 2Easy

Which call checks whether the last value read by rs.getInt("salary") was SQL NULL?

  1. rs.isNull()
  2. rs.getInt() == null
  3. rs.isEmpty()
  4. rs.wasNull()

Answer: D. rs.wasNull()

Explanation

getInt returns 0 for NULL, so call wasNull() immediately after to distinguish it from a real 0.

Step-by-step solution

  1. Read the column.
  2. Call wasNull right after.
  3. True means the database value was NULL.

Why the other options are wrong

  • A. ResultSet has no isNull method.
  • B. getInt returns an int, which cannot be null; it gives 0.
  • C. isEmpty does not exist on ResultSet.

Question 3Medium

After ResultSet rs = stmt.executeQuery(sql);, what must you do before reading the first row's columns?

  1. Call rs.next()
  2. Call rs.previous()
  3. Nothing, the cursor is on row 1
  4. Call rs.getRow(0)

Answer: A. Call rs.next()

Explanation

A new ResultSet's cursor sits before the first row. next() moves it forward and returns false when no rows remain.

Step-by-step solution

  1. Cursor starts before row 1.
  2. rs.next() advances to the first row.
  3. Then call rs.getString(...).

Why the other options are wrong

  • B. previous() moves backwards, away from the first row.
  • C. Reading before calling next() throws an SQLException.
  • D. getRow() returns the current row number; it does not move.

Question 4Hard

Which approach makes two SQL updates succeed or fail together in JDBC?

  1. Run two Statements one after another with the default settings
  2. Call executeBatch() on a Connection object after both updates
  3. Close the connection between the updates
  4. setAutoCommit(false), run both, then commit(), and rollback() on error

Answer: D. setAutoCommit(false), run both, then commit(), and rollback() on error

Explanation

Autocommit is on by default, so each statement commits immediately. Turn it off to group statements into one transaction.

Step-by-step solution

  1. conn.setAutoCommit(false).
  2. Execute both updates.
  3. commit() on success, rollback() in the catch block.

Why the other options are wrong

  • A. With autocommit each update commits alone.
  • B. executeBatch is a method of Statement, not Connection, and is about batching.
  • C. Closing a connection does not group the updates.

Question 5Hard

Calling executeQuery() with an INSERT statement on a JDBC driver generally leads to what?

  1. SQLException
  2. The row is inserted and an empty ResultSet is returned
  3. A NullPointerException
  4. The insert runs twice

Answer: A. SQLException

Explanation

executeQuery is meant for statements that return a ResultSet. Using it for DML throws a SQLException.

Step-by-step solution

  1. Choose method by statement type.
  2. SELECT: executeQuery.
  3. DML: executeUpdate.

Why the other options are wrong

  • B. Drivers reject this because no result set is produced.
  • C. No empty result set is created for a DML statement.
  • D. It does not return null, it throws.

Practise all 5 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