• Home
  • About Us
  • Privacy Policy
  • Contact Us

BenchResources.Net

Java tutorial for beginners

  • Home
  • Java
    • Java 1.8 Features
    • Java 8 Date/Time
    • JSON/YAML/XML/JAXB
    • Java Basics
    • OOPS
    • String Handling
    • Exception Handling
    • Generics
    • Serialization
    • Collection
    • Concurrent Collection
    • JDBC
  • Spring
    • Spring Core
    • Spring AOP
    • Spring MVC
    • Spring JDBC
    • Spring ORM
    • Spring Security
  • Web Services
    • Apache CXF – REST
    • JBoss RestEasy
    • Jersey 2.x
    • Spring Restful
    • Apache Axis2 RI
    • Apache CXF – SOAP
    • Metro JAX-WS – SOAP
  • Tools
    • Apache Maven
    • Eclipse IDE
    • Eclipse + Maven
    • FileZilla FTP
  • Oracle SOA
    • Oracle SOA 12c
    • Interview Question on SOA
  • Cloud
    • AWS
    • Microsoft Azure
  • Android
  • Interview Q
  • Jobs

Java – Interview Questions and Answers on JDBC

June 30, 2016 SJ Interview Questions, JDBC 0

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 stands for Java DataBase Connectivity
  • It is a standard set of API (Application Programming Interface)
  • Which is used to interact with different variety of databases from Java application

Q) What is JDBC driver? And its different types ?

  • JDBC Drivers are categorized mainly into 4 types. These are
 
Type
 
 
Driver
 
 
Translates
 
1JDBC-ODBC bridgeJDBC calls into ODBC calls
2Native APIJDBC calls into native database API calls
3Network ProtocolMiddleware translates JDBC calls into database specific calls
4Thin (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 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 API ?

  • Step 1: Loading or registering driver class
  • Step 2: Opening database connection
    1. Step 2.A: Creating and getting connection
    2. Step 2.B: Creating JDBC Statement (or PreparedStatement or CallableStatment)
    3. 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,
    1. Class.forName(“qualifiedDriverClassName”);
    2. DriverManager.registerDriver(“qualifiedDriverClassName”);

Q) What is JDBC DriverManager class ?

  • DriverManager is a mean through which Java application connects to databases
  • using appropriate driver supplied

Q) What are the different types of statements available in JDBC ?

  • There are 3 types statement available in JDBC, namely
  • 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 ?

  • MySQL connection steps
  • Oracle connection steps
  • MS Access connection steps

Q) Steps to connect MS Access using JDBC ?

  • Refer MS Access connection steps

Q) Steps to connect MS Access using JDBC in Java 1.8 version ?

  • Refer MS Access database interaction through Java JDBC API for Java 1.8 version

Q) Explain main components of JDBC ?

  • JDBC Core components are,
    1. DriverManager
    2. Driver
    3. Connection
    4. Statement
    5. ResultSet
  • 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
  • It saves time and performance improves far better

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
  • See example for Batch execution using Statement interface and prepared statement interface

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 should be issued/sent 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
connection.rollback();

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 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();
rollback();
  • All 3 types of statement can be invoked from connection object

Q) What is JDBC ResultSet ?

  • ResultSet holds data retrieved from 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 ResultSet updatable, as it isn’t updatable by default

Q) What are the types of ResultSet ?

  • There are 3 constants available in ResultSet interface, namely
    1. TYPE_FORWARD_ONLY
    2. TYPE_SCROLL_INSENSITIVE
    3. TYPE_SCROLL_SENSITIVE

Q) What is JDBC DatabaseMetaData interface ?

  • DatabaseMetaData interface is used to extract database information like,
    1. driver name
    2. driver version
    3. product name
    4. product version
    5. number of tables
    6. number of views
  • For more details read, Metadata of database using DatabaseMetaData Interface

Q) What is JDBC ResultSetMetaData interface ?

  • ResultSetMetaData interface is used to extract resultset information like,
    1. total number of columns
    2. column names
    3. column data-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
    1. Driver class
    2. JDBC URL
    3. Server IP or address
    4. Server port
    5. Database name
    6. Username
    7. Password
  • Read JDBC Connection steps to understand

Related Articles:

  • Java – Introduction to JDBC
  • Java – JDBC Driver types
  • Java – Core JDBC components
  • Java – JDBC Driver list for all leading database
  • Java – JDBC connection steps
  • Java – An example to connect MySQL database
  • Java – An example to connect Oracle database
  • Java – An example to connect MS Access database
  • Java 8 – An example to connect MS Access database in Java 8
  • Java – JDBC program to connect IBM DB2 database running on Mainframe z/OS system
  • Java – Creating database using JDBC Statement interface
  • Java – Droping database using JDBC Statement interface
  • Java – Creating a table using JDBC Statement interface
  • Java – Inserting a record using JDBC Statement interface
  • Java – Getting all list of records using JDBC Statement interface
  • Java – Getting single record using JDBC Statement interface
  • Java – Updating a record using JDBC Statement interface
  • Java – Deleting a record using JDBC Statement interface
  • Java – Dropping a table using JDBC Statement interface
  • Java – Batch update using JDBC Statement interface
  • Java – Batch insert using JDBC Statement interface
  • Java – Creating a table using JDBC PreparedStatement interface
  • Java – Inserting a record using JDBC PreparedStatement interface
  • Java – Getting all list of records using JDBC PreparedStatement interface
  • Java – Getting single record using JDBC PreparedStatement interface
  • Java – Updating a record using JDBC PreparedStatement interface
  • Java – Deleting a record using JDBC PreparedStatement interface
  • Java – Batch update using JDBC PreparedStatement interface
  • Java – Batch insert using JDBC PreparedStatement interface
  • Java – Calling Stored Procedure using JDBC CallableStatement interface
  • Java – Calling Stored Function using JDBC CallableStatement interface
  • Java – Calling Stored Procedure using JDBC CallableStatement interface with Batch execution
  • Java – Transaction handling using JDBC Statement interface
  • Java – Transaction handling using JDBC PreparedStatement interface
  • Java – Integration with Spring framework (Spring JDBC)
  • Java – Where clause example using JDBC Statement interface
  • Java – Like clause example using JDBC Statement interface
  • Java – Order by clause example using JDBC Statement interface
  • Java – Metadata of database using JDBC DatabaseMetaData interface
  • Java – Metadata of Resultset using JDBC ResultSetMetaData interface
  • Java – Interview question and answer on JDBC

Happy Coding !!
Happy Learning !!

Java Collection framework overview
Java – Metadata of Resultset using JDBC ResultSetMetaData interface

Related

Related posts:

  1. Java – Metadata of Resultset using JDBC ResultSetMetaData interface
  2. Java – Metadata of database using JDBC DatabaseMetaData interface
  3. Java – Getting all list of records using JDBC Statement interface
  4. Java – Order by clause example using JDBC Statement interface
  • database
  • database table
  • interview question
  • Java
  • JDBC
  • JDBC Connection steps
  • metadata
  • mysql
  • resultset
  • select query

Search tutorials

Subscribe via Email

Join 236 other subscribers




Proudly powered by Tuto WordPress theme from MH Themes