Database Connectivity in Java MCQs and Short Answers: 11 Solved JDBC Questions

Solve 11 current JDBC question-bank records with a clear dependency map. The set covers four driver types, API roles, connection setup, compile-time and runtime failures, and the right method for SELECT.

KnowledgeGate Team

Exam prep & CS education

Updated 29 Nov 20267 min read
Database Connectivity in Java MCQs and Short Answers: 11 Solved JDBC Questions

JDBC questions often mix four driver architectures with the dependency chain from Connection to Statement to ResultSet. Separate the transport path from the object lifecycle before choosing an answer. For the broader programming route, start with Coding & DSA.

1. Database connectivity in Java: build one dependency map before answering

JDBC dependency flow runs from DriverManager to Connection, Statement, and ResultSet, with a code sequence below.

Use three passes. Classify the prompt as driver architecture, JDBC component, or execution flow. Then write this dependency chain:

Code
DriverManager -> Connection -> Statement or PreparedStatement -> ResultSet

Finally decide whether the code fails at compile time, fails at runtime, or reaches the database. Use this warm-up:

java
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "1234");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM student");

st depends on con, and rs depends on a query through st. Questions 1 to 3 and 5 to 7 use multiple-choice format; Questions 4 and 8 to 11 require written reasoning. Q2 is attributed to ISRO Computer Science, December 2017; no other question in this set carries an exam attribution.

2. JDBC driver-type questions: Q1 to Q4

Q1. Direct database communication without middleware

Code
Which JDBC driver communicates directly with the database without middleware?
कौन-सा JDBC ड्राइवर बिना मिडलवेयर के सीधे डेटाबेस से संचार (communicate) करता है?
  • (a) Type 1

टाइप 1

  • (b) Type 2

टाइप 2

  • (c) Type 3

टाइप 3

  • (d) Type 4

टाइप 4

Answer: (d) Type 4. Type 1 adds an ODBC bridge, Type 2 needs a vendor native library, and Type 3 uses middleware. Type 4 is the direct pure-Java-to-database-protocol path.

Q2. Identify the stored description of a Type IV driver

Code
Type IV JDBC driver is a driver 
  • (a) which is written in C++

  • (b) which requires an intermediate layer

  • (c) which communicates through Java sockets

  • (d) which translates JDBC function calls into API not native to DBMS

Answer: (c) which communicates through Java sockets. This describes Java network communication without ODBC, a native client library, or middleware. Q2 is attributed to ISRO Computer Science, December 2017; that source does not indicate a current syllabus or pattern.

Q3. Translate JDBC calls into a database-specific protocol

Code
Which JDBC driver type translates JDBC calls directly into database-specific protocol?
कौन-सा JDBC ड्राइवर प्रकार JDBC कॉल्स को सीधे डेटाबेस-विशिष्ट प्रोटोकॉल में ट्रांसलेट करता है?
  • (a) Type 1 driver

टाइप 1 ड्राइवर

  • (b) Type 2 driver

टाइप 2 ड्राइवर

  • (c) Type 3 driver

टाइप 3 ड्राइवर

  • (d) Type 4 driver

टाइप 4 ड्राइवर

Answer: (d) Type 4 driver. “Direct database-specific protocol” removes the bridge, native-client, and middleware distractors. Q1 and Q3 are separate questions testing the same distinction.

Q4. Compare all four driver architectures

Code
A transport management system is being developed to connect a Java application with a database.
In the first approach, a structured system uses different connection methods like ODBC bridge, native libraries, and network-based middleware, where developers manually configure each connection mechanism and manage compatibility issues.
In the second approach, a Java-based system uses JDBC drivers of different types (Type 1, Type 2, Type 3, Type 4) to establish database connectivity through a standardized API.
During implementation, developers assume that all JDBC driver types perform the same function and can be used interchangeably without impact.
Evaluate this assumption and compare all JDBC driver types in terms of working mechanism, dependency, and performance.

The interchangeability claim is false. The drivers expose JDBC, but their paths and dependencies differ:

Type

Working path

Main dependency or overhead

1

JDBC -> ODBC -> database

ODBC and native components

2

JDBC -> vendor native API -> database

Vendor client libraries

3

JDBC -> middleware server -> database

An extra network tier

4

JDBC -> database-specific wire protocol

No native client library

The first three add translation or network stages. Type 4 has less architectural overhead, though performance still depends on the system and workload.

Four-row flow diagram compares JDBC driver Types 1 through 4, showing their connection paths and dependencies, with Type 4 highlighted as the answer.

3. JDBC API, DriverManager and Statement: Q5 to Q7

Q5. The database-independent connectivity layer

Code
In JDBC architecture, which component provides database-independent connectivity API?
JDBC आर्किटेक्चर में कौन-सा कंपोनेंट डेटाबेस-स्वतंत्र (database-independent) कनेक्टिविटी API प्रदान करता है?
  • (a) DriverManager

ड्राइवर मैनेजर

  • (b) JDBC API

  • (c) ResultSet

रिज़ल्टसेट

  • (d) Statement

स्टेटमेंट

Answer: (b) JDBC API. It is the database-independent abstraction. DriverManager obtains a connection, Statement sends SQL, and ResultSet exposes rows.

Q6. The component that establishes the connection

Code
Which JDBC component establishes a connection with the database?
JDBC का कौन-सा कंपोनेंट डेटाबेस के साथ कनेक्शन स्थापित करता है?
  • (a) DriverManager

  • (b) Statement

  • (c) ResultSet

  • (d) PreparedStatement

Answer: (a) DriverManager. DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "1234") returns the required Connection. Statements need that connection, while ResultSet appears only after retrieval.

Q7. The interface that executes SQL statements

Code
Which JDBC interface executes SQL statements?
JDBC का कौन-सा इंटरफ़ेस SQL स्टेटमेंट्स को निष्पादित (execute) करता है?
  • (a) Connection

  • (b) Statement

  • (c) ResultSet

  • (d) DriverManager

Answer: (b) Statement. Create it with Statement st = con.createStatement(), then call st.executeQuery("SELECT * FROM student"). Connection owns the session, Statement executes SQL, and ResultSet is the returned cursor.

4. The standard JDBC sequence: Q8

Q8. Define JDBC and order the connection steps

Code
What is JDBC? List and explain the standard sequential steps required to connect a Java application with a database using JDBC architecture.

JDBC is the Java database-connectivity API. Make the driver available, obtain Connection con, create a statement, execute SQL and consume ResultSet rs, then close rs, st, and con.

java
try (Connection con = DriverManager.getConnection(
         "jdbc:mysql://localhost/test", "root", "1234");
     Statement st = con.createStatement();
     ResultSet rs = st.executeQuery("SELECT * FROM student")) {
    while (rs.next()) {
        System.out.println(rs.getInt(1));
    }
}

Try-with-resources closes objects automatically. The dependency order remains con, then st, then rs.

5. Uninitialized and null connections: Q9 and Q10

Q9. Components are present but used in the wrong order

A developer writes the following JDBC program and argues that since all required JDBC components, Connection, Statement, and ResultSet, are present in the code, the program should execute successfully regardless of the order in which they are used.

Code
java
import java.sql.*;

class Demo {
    public static void main(String[] args) throws Exception {
        ResultSet rs;
        Statement st;
        Connection con;

        st = con.createStatement();
        rs = st.executeQuery("SELECT * FROM student");
        con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "1234");

        while (rs.next()) {
            System.out.println(rs.getInt(1));
        }
    }
}

Analyze the above JDBC program and evaluate the developer's claim. Does the presence of all JDBC components guarantee successful execution? Identify the conceptual flaw in the program, explain what will actually happen at runtime, and describe the correct order in which JDBC components must be initialized and used. Justify your answer with reasoning based on object dependency and the JDBC workflow.

This program never reaches runtime. Reading con before definite assignment causes a compile-time error such as variable con might not have been initialized. Assign con = DriverManager.getConnection(...), then create st, then create rs; declaration is not initialization.

Q10. A connection explicitly set to null

A developer writes the following Java code to fetch data from a database:

Code
import java.sql.*;

class Test {
    public static void main(String[] args) throws Exception {
        Connection con = null;
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM student");

        while(rs.next()) {
            System.out.println(rs.getInt(1));
        }
    }
}

The developer claims this code demonstrates JDBC connectivity and data retrieval.
However, the program fails at runtime.
Identify the issue in the code and explain why it does not correctly demonstrate JDBC usage. Justify your answer.

Unlike Q9, this compiles because con is assigned null. It throws NullPointerException at con.createStatement(), before SQL executes. Replace the null assignment with DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "1234") before creating stmt.

6. Choose the right execution method: Q11

Q11. executeUpdate cannot retrieve a SELECT result set

A developer writes the following program to test JDBC execution flow:

Code
import java.sql.*;

class Demo {
    public static void main(String[] args) throws Exception {
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test","root","1234");
        Statement st = con.createStatement();
        
        int result = st.executeUpdate("SELECT * FROM student");
        System.out.println(result);
    }
}

The developer expects the program to print the number of records present in the student table.
However, the program behaves unexpectedly.
Explain the behavior of the code and identify the conceptual mistake related to JDBC architecture.

executeUpdate is for SQL that produces an update count. Because SELECT * FROM student produces a ResultSet, JDBC requires the driver to throw SQLException; this program does not print a row count. Use executeQuery and count the returned rows. For an illustrative table with first-column ids 101, 102, and 103, use:

java
ResultSet rs = st.executeQuery("SELECT * FROM student");
int count = 0;
while (rs.next()) count++;
System.out.println(count);

The trace is 0 -> 1 at id 101, 1 -> 2 at id 102, and 2 -> 3 at id 103, so it prints 3. These are illustrative rows, not live data. Practise the SQL itself with SQL Queries for Placement Interviews, Step by Step; this question tests which JDBC method carries it.

7. Answer map, recurring traps and the next practice step

The six MCQ answers are: Q1 d, Q2 c, Q3 d, Q5 b, Q6 a, Q7 b.

Short-answer checkpoints: Q4 compares four paths; Q8 orders five actions; Q9 fails at compile time; Q10 throws runtime NullPointerException; Q11 uses executeQuery and traverses ResultSet.

Three traps recur. JDBC API is the abstraction; DriverManager creates connections. Declaration is not initialization, so build dependencies in order. executeUpdate does not return a SELECT row count; use executeQuery and traverse rows.

Next, work through Java Programming. For runnable connections, prepared queries and transaction basics, use JDBC in Java Tutorial: Connections, Queries and Transactions with Examples. For a deeper commit-and-rollback case, continue to Database Connectivity in Java: JDBC Transactions with a Worked Transfer. Those explainers build applications; this 11-question set focuses on diagnosing driver, object-order and execution-method traps.