Java interacts with database using a common database application programming interface called as JDBC (Java
Database Connectivity). JDBC is a Java API to connect and execute the query with the database. JDBC API uses
JDBC drivers (JDBC-ODBC Bridge Driver, Native Driver etc.) to connect with the database.
JDBC ARCHITECTURE
The JDBC API supports both two-tier and three-tier processing models for database access but in general, JDBC
Architecture consists of two layers:
1. JDBC API: This provides the application-to-JDBC Manager connection.
2. JDBC Driver API: This supports the JDBC Manager-to-Driver Connection.
The JDBC API uses a driver manager and database-specific drivers to provide transparent connectivity to
heterogeneous databases.
Common JDBC Components:
1. DriverManager class manages a list of database drivers. Matches connection requests from the java
application with the proper database driver using communication sub protocol.
2. Driver Interface handles the communications with the database server. We will interact directly with
Driver objects very rarely, instead, we use DriverManager objects, which manages objects of this type.
It also abstracts the details associated with working with Driver objects.
3. Connection Interface with all methods for contacting a database. The connection object represents
communication context, all communication with database is through connection object only.
4. Statement we use objects created from this interface to submit the SQL statements to the database.
Some derived interfaces accept parameters in addition to executing stored procedures.
5. ResultSet Objects hold data retrieved from a database after we execute an SOL query using Statement
objects. It acts as an iterator to allow us to move through its data.
6. SQLException class handles any errors that occur in a database application.
JDBC PROCESS
Steps to create connectivity to the database:
Step 1: Load the driver and establish a database connection to the MS-Access/ PostgreSQL/mysql server.
Step 2: Create an instance of the Statement object.
Step 3: Execute a statement to get a ResultSet object.
Step 4: Process the ResultSet object.
Step 5: Close the database connection.
1. Load & Register Driver
= [Link] (“Driver Name”);
2. Create connection
=connection con = [Link](“url”,”username”,”password”);
3. Create statement
=PreparedStatement ps = [Link](“Sql query”);
4. Execute SQL Statement
=[Link]();
5. Process the result
6. Close the connection
=[Link]();
LIFE CYCLE OF THREAD
A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies.
This is also known as life cycle of a thread. The life cycle of the thread in java is controlled by JVM.
1. New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the
thread. It is also referred to as a born thread. The thread is in new state if you create an instance of Thread class
but before the invocation of start() method.
2. Runnable: The thread is in runnable state after invocation of start() method, but the thread scheduler has not
selected it to be the running thread.
3. Running: The thread is in running state ifthe thread scheduler has selected it.
4. Non-Runnable (Blocked): This is the state when the thread is still alive, but is currently not eligible to run.
5. Terminated: A thread is in terminated or dead state when its run() method exits.
JDBC Drivers:
JDBC Drivers are software components that enable a Java application to communicate with a database.
They act as a bridge between the JDBC API and the database-specific protocol.
Types of JDBC Drivers:
Type 1: JDBC-ODBC Bridge Driver:
Converts JDBC calls into ODBC calls.
Requires ODBC driver and DSN configuration on client machine
Provided by JavaSoft (earlier JDK versions)
Working: Java Application → JDBC → JDBC-ODBC Bridge → ODBC Driver → Database
Features:
Requires ODBC driver installation
Uses DSN (Data Source Name) for configuration
Type 2: Native API Driver:
Converts JDBC calls into database-specific native API (C/C++)
Requires vendor libraries on client side
Working: Java Application → JDBC → Native API → Database
Features:
Uses database-specific libraries
Requires installation on client machine
Type 3: Net Pure Java Driver:
Uses a middleware server (3-tier architecture)
JDBC calls are converted into network protocol, then to DB-specific calls
Working: Java Application → JDBC → Middleware Server → Database
Features:
Uses 3-tier architecture
Middleware converts JDBC calls into DB-specific protocol
Type 4: Native Protocol Pure Java Drivers:
Converts JDBC calls directly into database protocol
Uses socket connection to communicate with DB
Working: Java Application → JDBC → Database (Direct via socket)
Features:
No middleware or native libraries required
Direct communication with DB server
Multithreading:
Multithreading is one of the most important features of Java. Multithreading in Java is a process of
executing multiple threads simultaneously. Java provides built-in support for multi-threaded
programming.
Java is a multi-threaded programming language which means we can develop multithreaded program
using Java.
A multi-threaded program contains two or more parts that can run concurrently and each part can
handle different task at the same time making optimal use of the available resources especially when
your computer has multiple CPU’S.
WHAT ARE THREADS?
A task is completed by a program with the sequence of steps, called as process. Each specific task in a
process is called thread. When there are multiple threads execute simultaneously, it is called
multithreading.
A thread is a program's path of execution. A thread is a line of execution. A thread is a lightweight sub
process, a smallest unit of processing.
LIFE CYCLE OF THREAD:
1. New: A new thread begins its life cycle in the new state. It remains in this state until the program starts
the thread. It is also referred to as a born thread. The thread is in new state if you create an instance of
Thread class but before the invocation of start () method.
2. Runnable: The thread is in runnable state after invocation of start () method, but the thread scheduler
has not selected it to be the running thread.
3. Running: The thread is in running state if the thread scheduler has selected it.
4. Non-Runnable (Blocked): This is the state when the thread is still alive, but is currently not eligible to
run.
5. Terminated: A thread is in terminated or dead state when its run () method exits.
Runnable Interface:
This is another easiest way of creating threads and it contains abstract method. If the class is intended
to be executed as a thread, then we can achieve this by implementing Runnable interface.
Step 1: As a first step you need to implement a run () method provided by Runnable interface.
public void run ()
Step 2: At second step you will instantiate a Thread object using the following constructor:
Thread (Runnable threadobj, String threadName);
Step 3: Once Thread object is created, you can start it by calling start () method, which executes a call to
run () method. void start ();
THREAD PRIORITIES:
Thread priorities are the integers which decide how one thread should be treated with respect to the
others. Thread priority decides when to switch from one running thread to another, process is called
context switching.
Java thread priorities are in the range between MIN_PRIORITY (a constant of 1) and
MAX_PRIORITY (a constant of 10). By default, every thread is given priority NORM_PRIORITY (a
constant of 5).
Thread priority is a value assigned to each thread to indicate how important it is for execution. The
thread scheduler uses this priority to decide which thread should get CPU time.
Priority Range:
In Java, thread priority is an integer value from 1 to 10:
Thread.MIN_PRIORITY = 1 → Lowest priority
Thread.NORM_PRIORITY = 5 → Default priority
Thread.MAX_PRIORITY = 10 → Highest priority
SYNCHRONIZATION:
Multithreading introduces asynchronous behaviour to the programs. If a thread is writing some data
another thread may be reading the same data at that time. This may bring inconsistency.
Synchronization is a mechanism used to control the access of multiple threads to shared resources. It
ensures that only one thread can access a critical section at a time, preventing data inconsistency.
When multiple threads access shared data simultaneously Data may become inconsistent and Output
may be incorrect.
Syntax:
synchronized(objectidentifier) {
// Access shared variables and other shared resources
}
Networking:
Networking in Java refers to the ability of Java programs to communicate and share data over a
network (like LAN, WAN, or the Internet). Networking allows one Java application to connect with
another application running on a different machine.
Java provides networking support through [Link] package. This package contains classes and
interfaces for Working with URLs, Creating socket connections and Handling IP addresses.
Concepts in Java Networking:
1. IP Address: Unique identifier of a device on a network. Example: [Link]
2. Port Number: Identifies a specific application on a system. Example: 80 (HTTP), 3306
(MySQL)
3. Protocol: Rules for communication. Examples: HTTP, FTP, TCP, UDP
Purpose of Networking:
o Share data between systems
o Enable communication between client and server
o Build distributed applications (e.g., chat apps, web apps)