Nearby lessons
35 of 37JDBC - Questions and Answers
- 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):
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
| Point | Statement | PreparedStatement | CallableStatement |
|---|---|---|---|
| Use for | Simple / one-time SQL | SQL with values (repeated) | Stored procedures |
| Create by | createStatement() | prepareStatement(sql) | prepareCall("{call ...}") |
| Values | Joined in the string | ? placeholders + setXxx | ? placeholders + registerOut |
| Compiled | Every execution | Once | Once |
| SQL injection safe? | No | Yes | Yes |
executeQuery() vs executeUpdate() vs execute()
| Method | Used for | Returns |
|---|---|---|
| executeQuery() | SELECT | ResultSet |
| executeUpdate() | INSERT / UPDATE / DELETE / DDL | int (row count; 0 for DDL) |
| execute() | Any SQL when you don't know the type | boolean (true = ResultSet) |
How do you get the number of rows affected?
Use the int returned by executeUpdate():
How do you use dynamic values safely?
Always use PreparedStatement with ? placeholders — never join user input into a SQL string (SQL injection risk):
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?
What is a ResultSet? How do you process it?
A ResultSet holds the rows returned by a SELECT. Process it with a loop:
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?
| Point | ODBC | JDBC |
|---|---|---|
| Full form | Open Database Connectivity | Java Database Connectivity |
| Created by | Microsoft | Sun Microsystems |
| Languages | Any | Java only |
| Platform | Windows only | Any platform |
| Driver language | C / C++ | Java |
What is transaction management in JDBC?
Combining related operations into one unit following the rule "all or none". Steps:
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).
- 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.