Java - JDBC - Interview Questions

What is JDBC?

 

JDBC, which stands for Java Database Connectivity, is an API provided by the Java programming languages that provides a standard way to connect to relational databases. Following are the key database-independent tasks that are standardized by the JDBC API.

  • Connecting to a database.
  • Creating SQL statements.
  • Executing SQL statements against the database.
  • Navigating the results.
    Getting meta-data of the database.

What are the key steps to connect to a database using JDBC API?

 FAQ

There are two key steps to connecting to a database using Java JDBC API

1. Load JDBC Driver - Every database that can be connected using JDBC API must have a corresponding JDBC Driver class that implements java.sql.Driver interface. Before you can make a connection to the database you must load the driver. From JDBC 4.0 onwards any JDBC driver found in the classpath is automatically loaded. Prior to JDBC 4.0 you have to load the driver specifically using Class.forName(JDBC Driver).

2. Open database connection -After loading the driver, you can open a connection to the database by calling getConnection() method on DriverManager class and passing the database connection URL, user id and password as arguments.

What is the difference between Statement, PreparedStatement and Callable Statement?

 FAQ

A Statement is an interface provided by the Java programming language that represents a SQL statement. You execute a statement object which returns a ResultSet object, which contains the database data returned by that query.

There are three kinds of statements. Statement, PreparedStatement and CallableStatement.

Statement is used to represent simple SQL queries that have no parameters.

PreparedStatement which extends Statement represents pre-compiled SQL statements that may contain input parameters.

CallableStatement which extends from PreparedStatement is used to execute stored procedures that may contain both input and output parameters.

How do you execute queries using JDBC API?

 

You execute SQL queries by calling the execute() method on the statement object. Based on the type of query you can call either execute(), executeQuery() or executeUpdate().

execute() returns true if the query returns a result set. Use execute() if the query returns more than one result set. You can then get each result set by calling Statement.getResultSet().

executeQuery() returns one ResultSet object.

executeUpdate() is used to execute insert, delete or update SQL statements which update the rows of the database. execute() returns an integer representing the number of rows that were updated.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

How do you call stored procedures using JDBC API?

 FAQ

You can call a stored procedure by calling the execute() method on the CallableStatement object.

*** See complete answer in the Java Interview Guide.

What are result sets?

 

ResultSet object contains the database data that is returned by the query. You can iterate and access the data from the result set via cursor which is a pointer pointing to one row in the ResultSet object.

*** See complete answer in the Java Interview Guide.

What are the ResultSet types based on cursor movement and data sensitivity defined JDBC API?

 FAQ

The ResultSet can be of three different types based on the flexibility of cursor movement and sensitivity of data in the database.

1. TYPE_FORWARD_ONLY - The result set cannot be scrolled. Its cursor can move forward only from before the first row to after the last row. You can move from one row to the next by calling next() on the result set.

*** See complete answer in the Java Interview Guide.

What are the ResultSet types based on concurrency defined in JDBC API?

 FAQ

The concurrency of a ResultSet object defines if a ResultSet object can be updated or not. There are two types of ResultSet objects based on concurrence.

1. CONCUR_READ_ONLY - The ResultSet object cannot be updated using the ResultSet interface.

*** See complete answer in the Java Interview Guide.

What are the most commonly used methods to scroll a ResultSet?

 FAQ

Following are some commonly used methods to scroll a ResultSet.

next() - Moves the cursor forward one row from its current position.

previous() - Moves the cursor to the previous row in this ResultSet object.

first() - Moves the cursor to the first row in this ResultSet object.

*** See complete answer in the Java Interview Guide.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

How do you get the metadata of a result set?

 FAQ

ResultSetMetaData object contains information regarding the types and properties of columns in ResultSet object. ResultSetMetaData object is retrieved by calling ...

*** See complete answer in the Java Interview Guide.

What are RowSets?

 

RowSet objects are derived from ResultSet and adds following capabilities...

*** See complete answer in the Java Interview Guide.

What are connected and disconnected RowSets?

 

A RowSet object is considered either as connected or disconnected.

1. A connected RowSet is connected to a database, via a JDBC driver, throughout its life span.

*** See complete answer in the Java Interview Guide.

What are the different implementations of RowSets?

 

There are five different RowSet implementations.

1. JdbcRowSet – JdbcRowSet is an implementation of connected RowSet object. JdbcRowSet is similar to ResultSet. JdbcRowSet is commonly used to wrap a non-scrollable and read-only ResultSet object to add the scrolling and update capabilities to the ResultSet.

*** See complete answer in the Java Interview Guide.

What is a JDBC transaction?

 

A transaction is a set of statements that executes as a unit. So either all of the statements in a transaction are executed successfully or none of them are.

*** See complete answer in the Java Interview Guide.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 
 
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
 

Java - Interview Questions

Java - Object Oriented ProgrammingJava - Objects & ClassesJava - Data TypesJava - VariablesJava - StringsJava - ArraysJava - CollectionsJava - ReflectionJava - Lambda ExpressionsJava - StreamsJava - GenericsJava - ExceptionsJava - IOJava - ThreadsJava - ConcurrencyJava - JDBCJava - NetworkingJava - SecurityJava - JVM InternalsJava - PerformanceJava - New in Java 8Java - New in Java 9Java - New in Java 10Java - New in Java 11
 
MASTER Java  

Top ranked courses to help you master Java skills.
Java Programming Masterclass

iconicon

Offered By - Tim Buchalka
Platform - Udemy
Rating - * * * * *
Students Enrolled - 575,000 +

RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example