Contents of API
API contains many interfaces , helper classes and implementation class and these API’s will be
given in the form of jar file .
Eg : Apache POI, Jexcel, JDBC , Servlet etc..
API Contains
• Interfaces
Application 1 • Helper classes Application 2
• Implementation
classes
______ . jar
JDBC API
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 .
Specifications of JDBC
• All the Driver classes must contains one static block in it.
• All the Driver classes must mandatorily implements [Link] interface which is a part
of jdbc Api.
• All the Driver classes must be mandatory registered with DriverManager using a static
method called 'registerDriver()' method.
Architecture of JDBC
Implementation classes 1521
Oracle Database
Customer/ Oracle Driver
Interface
utilization 3306
logic MySQL Database
MySQL Driver
Java application Factory class 1433
MsSQL Driver MsSQL Database
1527
Derby Driver
Derby Database
JDBC API
JDBC Drivers
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.
JDBC API contains few Interfaces they are
• Driver
• Connection
• Statement
• Prepared Statement
• Callable Statement
• ResultSet
• MetaData
JDBC contains only one Helper class or factory class i.e, DriverManager .
JDBC Driver :
It loads a database-specific driver in an application to establish a connection with a database.
These drivers should be provided by the respective database or vendors in the form of jar files and whoever
provides the driver classes they should provide the implementation for those methods present in JDBC API
interface.
Note :
For us to communicate with any database server and to perform any DB operation we need to have
two important things, they are : 1). JDBC API 2). JDBC Drivers
➢ Here, we are going to use MySQL database. So we have to establish the connection with mysql
database
➢ The implementation class given by MySQL database is [Link] present in [Link]
file
Different Driver classes provided by respective data base servers or venders
[Link]
OracleDriver
Helper Class
Driver [Link]
Interfaces SQLServerDriver [Link]
Java EmbeddedDriver
Program [Link]
JDBC API
JDBC Driver Fully Qualified Class name
Class loading:
• Loading a dot class file into the jvm memory is known as class loading.
• A class is generally loaded in 2 different ways namely:
➢ By calling any of the members of a class which can either be a constructor, methods,
variables, blocks etc.
➢ By using a static method called forName() method ,which is present in [Link] package.
• Whenever we use this method, it throws an Exception calledClassNotFoundException
(checked Exception) .Surround with try and catch.
Syntax: [Link]("FQCN");
Steps of JDBC:
• Load and register the Driver
• Establish a connection with the data base server.
• Create statement or platform.
• Execute the sql queries or sql statement.
• Process the resultant data (optional).
• Close all the costly resources.
STEP 1: Load and register the Driver
• In this step, we have to load and register the Driver classes which is a part of JDBC
Driver which are provided by respective data base server or vendors.
• Driver classes can be loaded and registered in 2 diff ways namely:
• Manually: In this case an object of the Driver class is created by user and then registered
with DriverManager using static method called registerDriver() method.
Syntax: Driver d =new Driver();
[Link](d);
Note:This is not a good practice since it causes tight coupling b/w java application and data base
server.
• By using forName(): By using a static method called forName() method we can load and
register the Driver classes provided by respective data base servers or vendors.
Syntax: [Link]("[Link]");
Program:
public class JdbcDemo
{
psvm( )
{
try{
[Link]("[Link]
[Link]"); sop("Driver
class loaded and
registered");
}
catch(ClassNotFoundException e)
{
[Link]();
}
}
STEP 2: Load and register the Driver
• In this step, we have to establish a connection b/w the java application and the data base
Server by using getConnection("URL") method.
• getConnection() method is static factory/helper method which is used to create and return an
implementation object of Connection Interface 'based on URL'.
• Hence, the return type for the getConnection() method is Connection interface.
• There are 3 diff overloaded variants of getConnetion() method present namely refer the
above fig.
• Whenever we use either of the overloaded variants of getConnection() it throw a checked
Exception called SQLException.
Syntax:
[Link] con =[Link]("jdbc:mysql://localhost:3306?user=r
oot&password=admin");
[Link]:
• It is an interface which is a part of JDBC Api and the implementations are provided by respective
database servers or vendors as a part of JDBC Driver.
• It is always a good practice to close Connection Interface since it is considered to be a costly
resources which decreases the performance of the application.
[Link]
• It is a Helper class which is a part of JDBC Api which contains 2 important static method in it’s
namely:
1. registerDriver()
2. getConnection()