Nearby lessons

35 of 37

JDBC - Questions and Answers

📌 What You Will Learn
  • The most important JDBC questions asked in exams and interviews
  • Clear, short answers with memory tricks
  • The differences tables you must remember
  • A final revision checklist

Test yourself with the complete Questions and Answers quiz — exam-style multiple-choice questions covering What is JDBC?, What is the latest version of JDBC?, What are the 6 standard steps of a JDBC application?.

What is JDBC?

JDBC (Java Database Connectivity) is a Java technology (part of Java SE) used to communicate with a database from a Java application. It is a specification defined by Sun (Oracle) and implemented by database vendors as driver software.

What is the latest version of JDBC?

The latest standard version is JDBC 4.3, part of Java SE 9 and above. The version that shipped with Java 8 was JDBC 4.2.

What are the 6 standard steps of a JDBC application?

The six steps (memorise the acronym):

Trainer's Note: Modern note: from JDBC 4.0 the driver auto-loads, so step 1 is optional today.
Example03
JCode Cell
11. Load & register the driver
22. Establish the connection
33. Create a Statement
44. Send & execute the SQL
55. Process the ResultSet
66. Close the connection

How many types of drivers are there? Which is best?

There are four types: Type-1 (JDBC-ODBC bridge), Type-2 (native), Type-3 (network/middleware), Type-4 (thin). The best is Type-4 (pure Java, thin, recommended). Type-1 is removed from JDK 8.

Statement vs PreparedStatement vs CallableStatement

PointStatementPreparedStatementCallableStatement
Use forSimple / one-time SQLSQL with values (repeated)Stored procedures
Create bycreateStatement()prepareStatement(sql)prepareCall("{call ...}")
ValuesJoined in the string? placeholders + setXxx? placeholders + registerOut
CompiledEvery executionOnceOnce
SQL injection safe?NoYesYes
In simple words: Pick the statement by the job: `Statement` for one-time SQL, `PreparedStatement` for SQL with values, `CallableStatement` for stored procedures. PreparedStatement compiles once, runs many times, and is safe from SQL injection.

executeQuery() vs executeUpdate() vs execute()

MethodUsed forReturns
executeQuery()SELECTResultSet
executeUpdate()INSERT / UPDATE / DELETE / DDLint (row count; 0 for DDL)
execute()Any SQL when you don't know the typeboolean (true = ResultSet)
In simple words: Each execute method returns a different thing: `executeQuery()` gives a `ResultSet`, `executeUpdate()` gives a row count, and `execute()` gives a boolean. Use the one that matches the query you are running.

How do you get the number of rows affected?

Use the int returned by executeUpdate():

Example07
JCode Cell
1int rows = st.executeUpdate("DELETE FROM employee WHERE salary < 10000");
2System.out.println("Rows affected: " + rows);

How do you use dynamic values safely?

Always use PreparedStatement with ? placeholders — never join user input into a SQL string (SQL injection risk):

In simple words: `?` placeholders keep user input out of the SQL string — that is what stops SQL injection. The setXxx() methods send values as data, so nothing a user types can become part of the query.
Example08
JCode Cell
1String sql = "INSERT INTO employee VALUES (?, ?, ?)";
2PreparedStatement pst = con.prepareStatement(sql);
3pst.setInt(1, 101);
4pst.setString(2, "Rahul");
5pst.setDouble(3, 50000);
6pst.executeUpdate();

How many queries can one Statement object submit?

A single Statement object can be used to submit any number of queries, but only one query at a time (the previous ResultSet must be closed or processed first). A PreparedStatement can execute the same compiled query with many different values.

What is the difference between Class.forName() and registerDriver()?

Class.forName("driver.class") loads the class, which automatically runs its static block and registers the driver. DriverManager.registerDriver(driver) registers it manually. Since JDBC 4.0, neither is needed — drivers auto-register from the classpath.

What is the use of DriverManager if the driver already works?

The DriverManager is the middle-man: it keeps the list of all registered drivers and, when you call getConnection(url, ...), it chooses the right driver for that URL and asks it to connect. So the driver does the real work, and the DriverManager coordinates it.

What is the JDBC URL format?

Example12
JCode Cell
1jdbc : <subprotocol> : <subname>
2 | | |
3 main driver database detail
4protocol name
5 
6MySQL : jdbc:mysql://localhost:3306/company
7Oracle : jdbc:oracle:thin:@localhost:1521:xe

What is a ResultSet? How do you process it?

A ResultSet holds the rows returned by a SELECT. Process it with a loop:

Example13
JCode Cell
1ResultSet rs = st.executeQuery("SELECT * FROM employee");
2while (rs.next()) {
3 int id = rs.getInt("id");
4 String name = rs.getString("name");
5 System.out.println(id + " " + name);
6}

What are the JDBC metadata types?

Three types: DatabaseMetaData (about the database, from con.getMetaData()), ResultSetMetaData (about result columns, from rs.getMetaData()), and ParameterMetaData (about PreparedStatement parameters).

What is the difference between JDBC and ODBC?

PointODBCJDBC
Full formOpen Database ConnectivityJava Database Connectivity
Created byMicrosoftSun Microsystems
LanguagesAnyJava only
PlatformWindows onlyAny platform
Driver languageC / C++Java

What is transaction management in JDBC?

Combining related operations into one unit following the rule "all or none". Steps:

Example16
JCode Cell
1con.setAutoCommit(false);
2try {
3 // do all operations
4 con.commit();
5} catch (Exception e) {
6 con.rollback();
7}

What is connection pooling and why is it used?

Keeping a pool of ready connections so the application borrows and returns them instead of creating and destroying connections each time. It is faster and prevents database connection exhaustion. The modern way is via DataSource (e.g., HikariCP).

In simple words: Connection pooling reuses a set of ready-made connections instead of opening a new one for every request — the application borrows one, uses it, and returns it, which is faster and never exhausts the database.
📝 Key Takeaways
  • JDBC connects Java to any database; it is database independent and platform independent.
  • Type-4 thin driver is the modern choice; the ODBC bridge is dead.
  • PreparedStatement is the professional default — safe and fast.
  • Transactions, pooling, metadata, ResultSet types and RowSets round out the full picture.
  • Revise the comparison tables (Statement family, execute family, driver types) before any interview.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4