Module-5
JDBC
Topics
JDBC Objects: The Concept of JDBC
● JDBC Driver Types
● JDBC Packages, A Brief Overview of the JDBC
process, Database Connection, Associating the
JDBC/ODBC Bridge with the Database, Statement
Objects, Result Set, Transaction Processing,
Metadata, Data types, Exceptions.
The Concept of JDBC
● The JDBC ( Java Database Connectivity) API defines interfaces and
classes for writing database applications in Java by making database
connections.
● Using JDBC you can send SQL, PL/SQL statements to almost any
relational database. JDBC is a Java API for executing SQL statements
and supports basic SQL functionality.
● It provides RDBMS access by allowing you to embed SQL inside Java
code.
What is JDBC?
● JDBC is an acronym for Java Database Connectivity.
● It’s an advancement for ODBC ( Open Database Connectivity ).
JDBC is a standard API specification developed in order to move
data from the front end to the back end.
● This API consists of classes and interfaces written in Java. It
basically acts as an interface (not the one we use in Java) or
channel between your Java program and databases, i.e it
establishes a link between the two so that a programmer can send
data from Java code and store it the database for future use .
Why JDBC Come into Existence?
● As previously told JDBC is an advancement for ODBC, ODBC being
platform-dependent had a lot of drawbacks. ODBC API was
written in C, C++, Python, and Core Java and as we know above
languages (except Java and some part of Python)are platform-
dependent. Therefore to remove dependence, JDBC was
developed by a database vendor which consisted of classes and
interfaces written in Java.
Steps to Connect Java Application
with Database
● Below are the steps that explains how to connect to Database in
Java:
Step 1 – Import the Packages
Step 2 – Load the drivers using the forName() method
Step 3 – Register the drivers using DriverManager
Step 4 – Establish a connection using the Connection class object
Step 5 – Create a statement
Step 6 – Execute the query
Step 7 – Close the connections
Java Database Connectivity
JDBC Driver Types
JDBC Packages
A Brief Overview of the JDBC process
● JDBC stands for Java Database Connectivity. JDBC is a Java API
to connect and execute the query with the database. It is a
specification from Sun Microsystems that provides a standard
abstraction(API or Protocol) for Java applications to
communicate with various databases.
● It provides the language with Java database connectivity
standards. It is used to write programs required to access
databases. JDBC, along with the database driver, can access
databases and spreadsheets. The enterprise data stored in a
relational database(RDB) can be accessed with the help of JDBC
APIs.
Definition of JDBC(Java Database
Connectivity)
● JDBC is an API(Application programming interface) used in Java
programming to interact with databases. The classes and
interfaces of JDBC allow the application to send requests made by
users to the specified database. The current version of JDBC is
JDBC 4.3, released on 21st September 2017.
Purpose of JDBC
Enterprise applications created using the JAVA EE technology
need to interact with databases to store application-specific
information. So, interacting with a database requires efficient
database connectivity, which can be achieved by using the
ODBC(Open database connectivity) driver. This driver is used with
JDBC to interact or communicate with various kinds of databases
such as Oracle, MS Access, Mysql and SQL server database.
Components of JDBC
● There are generally four main components of JDBC through which
it can interact with a database. They are as mentioned below:
● 1. JDBC API: It provides various methods and interfaces for easy
communication with the database. It provides two packages as
follows, which contain the java SE and Java EE platforms to exhibit
WORA(write once run anywhere) capabilities. The [Link]
package contains interfaces and classes of JDBC API.
● It also provides a standard to connect a database to a client
application.
● 2. JDBC Driver manager: It loads a database-specific driver in an
application to establish a connection with a database. It is used to
make a database-specific call to the database to process the user
request.
● 3. JDBC Test suite: It is used to test the operation(such as
insertion, deletion, updation) being performed by JDBC Drivers.
● 4. JDBC-ODBC Bridge Drivers: It connects database drivers to the
database. This bridge translates the JDBC method call to the
ODBC function call. It makes use of the [Link] package
which includes a native library to access ODBC characteristics.
Architecture of
● Description: JDBC
● Application: It is a java applet or a servlet that communicates with a data source.
● The JDBC API: The JDBC API allows Java programs to execute SQL statements and
retrieve results. Some of the important interfaces defined in JDBC API are as
follows: Driver interface , Result Set Interface , RowSet Interface , Prepared
Statement interface, Connection inteface, and Classes defined in JDBC API are as
follows: Driver Manager class, Types class, Blob class, clob class.
● Driver Manager: It plays an important role in the JDBC architecture. It uses some
database-specific drivers to effectively connect enterprise applications to databases.
● JDBC drivers: To communicate with a data source through JDBC, you need a JDBC
driver that intelligently communicates with the respective data source.
Database Connection
Associating the JDBC/ODBC Bridge
with the Database
The JDBC-ODBC bridge
provides JDBC access via most
ODBC drivers.
Types of Statements in JDBC
● The statement interface is used to create SQL basic statements in
Java it provides methods to execute queries with the database.
There are different types of statements that are used in JDBC as
follows:
● Create Statement
● Prepared Statement
● Callable Statement
● 1. Create a Statement:
● From the connection interface, you can create the object for this
interface. It is generally used for general–purpose access to
databases and is useful while using static SQL statements at
runtime.
Syntax:
2. Prepared Statement:
● It represents a recompiled SQL statement, that can be executed
many times. This accepts parameterized SQL queries. In this, “?” is
used instead of the parameter, one can pass the parameter
dynamically by using the methods of PREPARED STATEMENT at
run time.
3. Callable Statement:
Callable Statements are stored procedures which are a group of
statements that we compile in the database for some task, they
are beneficial when we are dealing with multiple tables with
complex scenario & rather than sending multiple queries to the
database, we can send the required data to the stored procedure
& lower the logic executed in the database server itself. The
Callable Statement interface provided by JDBC API helps in
executing stored procedures.
Result Set
Transaction Processing
● Transaction Processing: Transactions in Java, as in general refer
to a series of actions that must all complete successfully.
● Hence, if one or more action fails, all other actions must back out
leaving the state of the application unchanged.
● This is necessary to ensure that the integrity of the application
state is never compromised.
Metadata
● Operations of Transaction: Following are the main operations of transaction:
● Read(X): Read operation is used to read the value of X from the database
and stores it in a buffer in main memory.
● Write(X): Write operation is used to write the value back to the database
from the buffer.
● Let's take an example to debit transaction from an account which consists
of following operations:
● 1. R(X);
● 2. X = X - 500;
● 3. W(X);
● Let's assume the value of X before starting of the transaction is 4000.
● The first operation reads X's value from database and stores it in a buffer.
● The second operation will decrease the value of X by 500. So buffer will
contain 3500.
● The third operation will write the buffer's value to the database. So X's final
value will be 3500.
● But it may be possible that because of the failure of hardware, software or
power, etc. that transaction may fail before finished all the operations in the
set .
Data types:
Java is statically typed and also a
strongly typed language because, in
Java, each type of data (such as integer,
character, hexadecimal, packed decimal,
and so forth) is predefined as part of the
programming language and all
constants or variables defined for a
given program must be described with
one of the Java data types.
Primitive Data Types in Java
●Primitive data are only single values and
have no special capabilities. There are 8
primitive data types. They are depicted
below in tabular format below as follows:
Type Description Default Size Example Literals Range of values
boolean true or false false 1 bit true, false true, false
twos-complement
0 8 bits (none) -128 to 127
integer
byte
‘a’, ‘\u0041’, ‘\101’, ‘\ characters representation of ASCII values
Unicode character \u0000 16 bits
\’, ‘\’, ‘\n’, ‘β’ 0 to 255
char
twos-complement
0 16 bits (none) -32,768 to 32,767
integer
short
-2,147,483,648
twos-complement
0 32 bits -2,-1,0,1,2 to
intger
2,147,483,647
int
-9,223,372,036,854,775,808
twos-complement
0 64 bits -2L,-1L,0L,1L,2L to
integer
9,223,372,036,854,775,807
long
IEEE 754 floating 1.23e100f ,
0.0 32 bits upto 7 decimal digits
point -1.23e-100f , .3f ,3.14F
float
IEEE 754 floating 1.23456e300d ,
0.0 64 bits upto 16 decimal digits
point -123456e-300d , 1e1d
double
Exceptions
● Exceptions: When executing Java code, different errors can occur:
coding errors made by the programmer, errors due to wrong input, or
other unforeseeable things. When an error occurs, Java will normally
stop and generate an error message. The technical term for this is: Java
will throw an exception (throw an error).
● Java try and catch
● The try statement allows you to define a block of code to be tested for
errors while it is being executed. The catch statement allows you to
define a block of code to be executed, if an error occurs in the try block.
● The try and catch keywords come in pairs:
Syntax:
try {
// Block of code to try
}
catch(Exception e) {
// Block of code to handle errors
}
Consider the following example: This will generate an error, because
myNumbers[10] does not exist.
public class Main {
public static void main(String[ ] args) {
int[] myNumbers = {1, 2, 3};
[Link] (myNumbers[10]); // error!