In this article, we will cover some of the interview questions with their justification on Java JDBC
These are most frequently asked interview question from Java JDBC
Read Java JDBC concepts in detail
JDBC Interview Questions
Q) What is JDBC?
–> JDBC (Java DataBase Connectivity) is a standard set of API (Application Programming Interface) is used to interact with different varieties of databases from Java application
Q) What is JDBC driver? And its different types?
–> JDBC Drivers are categorized mainly into 4 types. These are
|1||JDBC-ODBC bridge||JDBC calls into ODBC calls|
|2||Native API||JDBC calls into native database API calls|
|3||Network Protocol||Middleware translates JDBC calls into database specific calls|
|4||Thin (Network Driver)||JDBC calls directly into database calls|
Note: Out of 4 types of JDBC drivers, thin or network driver are most commonly used in today’s industries
Q) Which is the most commonly used Driver type in JDBC?
–> It depends on the situation where we are going to use the database and also number database involved
–> For single database like Oracle or Teradata, Type-4 driver is considered best
–> In a multiple environment with more than 2 databases Type-3 driver is considered apt
Q) What is the fastest type of JDBC driver?
–> Network driver or pure Java driver (Type-4) is the fastest driver among 4 categorized driver since these driver translates JDBC calls directly into database calls
Q) What are the steps to connect database in Java using JDBC?
–> JDBC connection steps
Step 1: Loading or registering driver class
Step 2: Opening database connection
- Step 2.A: Creating and getting connection
- Step 2.B: Creating JDBC Statement (or PreparedStatement or CallableStatment)
- Step 2.C: Executing SQL and MySql queries
Step 3: Closing database connection
–> For detail refer JDBC connection steps in detail
Q) What are the ways to load or register driver?
–> There are 2 ways to load or register JDBC driver class
Q) What is JDBC DriverManager class?
–> DriverManager is a mean through which Java application connects to the databases using the appropriate driver supplied
Q) What are the different types of statements available in JDBC?
–> There are 3 types available
- Statement –> to execute any SQL statement similar to database
- PreparedStatement –> to execute pre-compiled SQL statement
- CallableStatement –> to call stored procedure or stored function
Q) What is JDBC Statement?
–> Statement interface is the one which is actually used to execute queries like inserting a new record or updating an existing record
–> These statement are compiled for syntax checking before executing
Q) What is JDBC PreparedStatement?
–> JDBC PreparedStatement improves performance as they are pre-compiled comparing with JDBC Statement
–> Prepared statement are compiled and sent to database once and can be executed for different values (against ?) which are bounded with arguments
Q) Why PreparedStatement is used in JDBC?
–> It improves performances as these are pre-compiled
Q) What is JDBC CallableStatement?
–> Callable statement is used to execute stored procedure and stored function
Q) How to invoke database Stored Procedure or Stored Function using JDBC?
–> To call/execute stored procedure and stored function, CallableStatement is used
Q) Difference between Statement and PreparedStatement interface?
–> Performance-wise PreparedStatement is better than Statement interface, as prepared statement is pre-compiled whereas Statement is compiled every time
Q) Write a basic Java program to connect to database?
–> Refer connection steps for,
Q) Steps to connect MS Access using JDBC?
–> Refer MS Access connection steps
Q) Explain main components of JDBC?
–> JDBC Core components
–> Refer JDBC Basic components for detail
Q) What is batch processing or batch update in JDBC?
–> Instead of executing single SQL queries each time, we can group related SQL items together in a batch and then sent to database for execution
–> Using batch processing, we can do insert and update operations
Q) Why batch processing or batch update is used in JDBC?
–> Batch processing executes faster as related SQL items are sent to database in a batch resulting in high performance
–> We can use either Statement or PreparedStatement interface for batch execution
Q) How do you handle transaction in JDBC? (Using Connection Interface)
–> By default, auto-commit is turned ON which commits to database for every SQL statement sent/issued to database
–> Turn off auto commit and commit to database at the end using commit(); method
connection.setAutoCommit(false); // perform database operations like insert, update, delete, etc. connection.commit();
–> If any issue or error in between then rollback maintaining consistency and integrity of database
Q) What happens, by setting setAutoCommit(false) in JDBC ?
–> Nothing will be committed to database automatically until and unless we invoke commit(); method of Connection interface
Q) Why do we need to set setAutoCommit(false) in JDBC ?
–> This helps to handle JDBC transaction in our own way, instead committing data to database every single time
Q) Can we commit multiple records in single transaction?
–> Yes, we can commit multiple records in single transaction
–> For this, we need to turn off auto-commit mode and then start the transaction and finally commit to database
Q) What is JDBC Connection?
–> Connection interface is the one through which we can interact with database
–> To handle JDBC transaction, Connection interface has got number of useful methods like
setAutoCommit(boolean); commit() and rollback();
–> All 3 types of statement can be invoked from connection object
Q) What is JDBC ResultSet?
–> ResultSet holds data retrieved from the database when performing/executing queries
–> You can traverse in ResultSet to read data one-by-one, but in forward direction only
–> But by setting static variable “TYPE_SCROLL_INSENSITIVE” in createStatement(), we can make it scrollable in both directions
–> Similarly, by setting static variable “CONCUR_UPDATABLE” in createStatement(), we can make the ResultSet updatable (as it is not updatable by default)
Q) What are the types of ResultSet?
–> There are 3 constants available in ResultSet interface
Q) What is JDBC ResultSetMetaData interface?
–> DatabaseMetaData interface is used to extract database information like
- driver name
- driver version
- product name
- product version
- number of tables
- number of views
–> For more details read, Metadata of database using DatabaseMetaData Interface
Q) What is JDBC DatabaseMetaData interface?
–> ResultSetMetaData interface is used to extract resultset information like
- total number of columns
- column names
- column types
–> For more details read, Metadata of ResultSet using ResultSetMetaData Interface
Q) What are CLOB and BLOB datatypes in JDBC and why it is used?
–> CLOB and BLOB data types are used to handle/store large size data like movies, images, etc.
Q) Which datatype should we map to insert images into database?
–> BLOB data type
Q) What are the things need to be changed for connecting different database like switching from MySQL to Oracle?
–> Below things needs to be changed while switching from one database to another
- Driver class
- JDBC URL consisting
- Server IP or address
- Server port
- Database name
–> Read JDBC Connection steps to understand
- JDBC Tutorial index
- Introduction to JDBC
- Different types of JDBC drivers
- JDBC Driver list for all leading databases
- Basic components of JDBC
- JDBC connection steps
- MySQL database connection steps
- Oracle database connection steps
Happy Coding !!
Happy Learning !!