Java JDBC Interview Questions
35 questions with answers · Java Interview Guide
Java Database Connectivity, prepared statements, connection pooling, and transaction management.
How to close the connection connection
Call connection.close() to release JDBC connection back to pool; use try-with-resources for automatic closure.
How to cause a stored procedure
Use CallableStatement: CallableStatement cs = connection.prepareCall("{call procName(?, ?)}"); set parameters with setString(); execute with executeQuery(); retrieve results with getters.
How is the database request and processing the results are carried out
JDBC uses DriverManager to establish connections, executes SQL via Statement/PreparedStatement objects, and processes results through ResultSet which provides methods like next(), getInt(), getString() to iterate and extract data.
With which requests for the database are formed
Database requests are formed using SQL strings passed to Statement.executeQuery(), executeUpdate(), or PreparedStatement with parameterized queries to prevent SQL injection.
What transaction insulation levels are supported in JDBC
JDBC supports four isolation levels: READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, and SERIALIZABLE, set via Connection.setTransactionIsolation().
How to register a jdbc driver
Register JDBC driver via Class.forName("driver.class.name") which loads the driver class, or rely on automatic registration via ServiceLoader in Java 6+.
Describe the main stages of working with the database using JDBC
JDBC workflow: load driver → create connection → create statement → execute query → process ResultSet → close ResultSet/Statement/Connection.
What parts is JDBC from
JDBC consists of two layers: JDBC API (java.sql package with Connection, Statement, ResultSet) and JDBC Driver (vendor-specific implementation managing database communication).
List the main types of data used in JDBC, how are they related to the types of Java
JDBC types (VARCHAR, INTEGER, TIMESTAMP, BLOB) map to Java types (String, int/Integer, java.util.Date/LocalDateTime, byte[]/Blob) through JDBC type conversion.
What is JDBC url
JDBC URL is a connection string format like 'jdbc:drivertype://hostname:port/databasename' that specifies the database location and connection parameters.
What are the advantages of using JDBC
JDBC advantages include database independence through drivers, direct SQL execution, connection pooling support, and integration with enterprise applications.
How is the database request and processing the results are carried out
JDBC: Register driver → create Connection → execute queries using Statement/PreparedStatement → process ResultSet → close resources in finally/try-with-resources.
With which requests for the database are formed
Database requests are formed using SQL statements: SELECT, INSERT, UPDATE, DELETE; executed through Statement, PreparedStatement, or CallableStatement objects.
What transaction insulation levels are supported in JDBC
JDBC supports four isolation levels: READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE, mapped from Connection constants.
How to register a jdbc driver
Register JDBC driver via Class.forName("driver.class.name") or through DriverManager.registerDriver(); modern versions auto-load via SPI.
Describe the main stages of working with the database using JDBC
Main JDBC workflow: load driver → establish connection → create statement → execute query → process ResultSet → close resources.
What parts is JDBC from
JDBC comprises four components: DriverManager, Connection, Statement, ResultSet; plus the driver implementations for specific databases.
List the main types of data used in JDBC, how are they related to the types of Java
JDBC types: CHAR/VARCHAR map to String, INTEGER/BIGINT to int/long, DOUBLE/FLOAT to double/float, TIMESTAMP to java.sql.Timestamp, BLOB/CLOB to byte[]/String.
What is JDBC url
JDBC URL is a connection string format: jdbc:subprotocol:subname (e.g., jdbc:mysql://localhost:3306/dbname) specifying protocol, host, and database.
What are the advantages of using JDBC
JDBC advantages: direct SQL control, minimal overhead, broad database support, suitable for performance-critical applications, no ORM complexity.
Tell me about JDBCTEMPLATE
JdbcTemplate is Spring's wrapper around JDBC reducing boilerplate code; handles connection management, exception translation, result mapping, and SQL execution elegantly.
How can you install a connection with a database
Use DriverManager.getConnection(url, username, password) or DataSource to establish JDBC connections with proper driver registration.
What is the difference between Statement and PrepareDstate
Statement is recompiled per execution; PreparedStatement is precompiled and parameterized, providing better performance and SQL injection protection.
What is Resultset
ResultSet is a JDBC interface representing query results; provides cursor-based row access via next(), getters for columns by index/name.
What is the difference between Execute, Executequary, Executeupdate
execute() returns boolean (true if ResultSet), executeQuery() returns ResultSet, executeUpdate() returns affected row count; use based on expected output.
How Resultset works inside
ResultSet maintains a cursor position; next() advances to next row, getters fetch column data from current row; internally uses database connection for lazy data loading.
Tell me about JDBCTEMPLATE
JdbcTemplate is a Spring utility that simplifies JDBC operations by handling resource management, exception translation, and repetitive code, providing callback-based access.
How can you install a connection with a database
Database connection is established using DriverManager.getConnection(url, user, password) or DataSource, after loading the JDBC driver.
What is the difference between Statement and PrepareDstate
Statement executes static SQL queries, while PreparedStatement uses parameterized queries preventing SQL injection and improving performance through precompilation.
What is Resultset
ResultSet is a table-like object returned by database queries, containing rows and columns; accessed using cursor-based navigation and type-specific getters.
What is the difference between Execute, Executequary, Executeupdate
execute() returns boolean for any SQL, executeQuery() returns ResultSet for SELECT, executeUpdate() returns row count for INSERT/UPDATE/DELETE.
How Resultset works inside
ResultSet maintains a cursor pointing to current row, retrieved from database in batches determined by fetch size, and can be scrollable or forward-only.
Why do we need Resultset
ResultSet provides tabular access to query results, allows row iteration, type-safe data retrieval, and supports various cursor types and concurrency modes.
Tell me about Execute Executequery and Executeupdate
execute() runs any SQL statement; executeQuery() returns a ResultSet for SELECT queries; executeUpdate() returns row count for INSERT/UPDATE/DELETE operations.
Tell me about Execute Executequery and Executeupdate
executeQuery() returns a ResultSet for SELECT queries, executeUpdate() returns an int (affected rows) for INSERT/UPDATE/DELETE, and execute() returns a boolean indicating if the first result is a ResultSet.
Knowing the answers is half the battle
The other half is explaining them clearly under pressure.
Try a free mock interviewarrow_forward