JDBC
JDBC
JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the query with
the database.
Jar: -
→Runnable jar
1. Executable jar: -
There is no class which contains main method.
This is suitable to provide external library classes
2. Runnable jar: -
Step 1: - create a java project, create some packages and classes then write your code
Step 4: - Select a location where you want to store the jar file
1. External way
2. Internal way (recommended)
Adding jars external way: -
Step 1: - Right click on your project and select “build path” then select “configure build path”
Step 2: - Select “libraries” and then select class path later select add external jars
Step 3: - Select the jar file which you wanted to add to a project
Step 2: - copy jar file and paste it into the lib folder
Step 3: - Right click on your project and select “build path” then select “configure build path”
Step 4: - Select “libraries” and then select class path later select add jars
Step 5: - Select the jar file which is present inside your lib folder
(Right click on jar and then select build path and then select add to build path instead of Step 3, 4, 5, 6)
API: -
→ API are used to connect one or more application/programs in a loosely coupled manner.
→ If API is developed by java, then API is given in the form of jar file.
→ I form of API
→ II form of API
I form II form
• In this form implementation logic is given • There is no implementation logic
• It always need updation if there is any • It doesn’t update if there is any changes
changes at service provide side. at service provider side.
Drivers: -
Drivers provides the implementation logic of API. Here the following are the 4 types of JDBC
drivers.
Type-1 driver or JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The
JDBC-ODBC bridge driver converts JDBC method calls into the ODBC function calls. Type-1 driver is
also called Universal driver because it can be used to connect to any of the databases.
Advantages
• This driver software is built-in with JDK so no need to install separately.
• It is a database independent driver.
Disadvantages
• As a common driver is used in order to interact with different databases, the data transferred
through this driver is not so secured.
• The ODBC bridge driver is needed to be installed in individual client machines.
• Type-1 driver isn’t written in java, that’s why it isn’t a portable driver.
Native-API driver
The Native API driver uses the client -side libraries of the database. This driver converts JDBC
method calls into native calls of the database API. In order to interact with different database, this
driver needs their local API, that’s why data transfer is much more secure as compared to type-1
driver. This driver is not fully written in Java that is why it is also called Partially Java driver.
Advantage
• Native-API driver gives better performance than JDBC-ODBC bridge driver.
Disadvantages
• Driver needs to be installed separately in individual client machines
• The Vendor client library needs to be installed on client machine.
• Type-2 driver isn’t written in java, that’s why it isn’t a portable driver
• It is a database dependent driver.
Advantages
• Does not require any native library and Middleware server, so no client-side or server-side
installation.
• It is fully written in Java language; hence they are portable drivers.
Disadvantage
• If the database varies, then the driver will carry because it is database dependent.
JDBC: -
• JDBC stands for java data base connectivity.
• It contains two parts:
• JDBC API
• JDBC Driver
JDBC API
→ JDBC API is a specification given in the form of jar file
→ JDBC API helps to connect java program and dta base in a secure, organized and in a loosely
coupled manner.
→ JDBC API is a II from of API
→ JDBC API is present in java.sql package.
JDBC Interfaces: -
Driver
Connection
Statement
PreparedStatement
CallableStatement
ResultSet
DatabaseMetaData
ResultSetMEtaData
Helper Classses: -
DriverManager
Steps of JDBC: -
• Load And Register the jdbc Driver
• Establish connection between java program and the data base.
• Create a platform to execute SQL queries.
• Execute the queries.
• Fetch the resultant dat(optional)
• Closing of costly resource.
Step 1: -Load and Register driver: -
• In this step we load and register the class which implements java.sql.Driver interface
• We can do this step in 2 ways
• new keyword way
• Class.forName() (Recommended)
registerDriver(): -
→ It is a static method of DriverManager used to register driver class.
Public static void registerDriver(Driver t())
In mysql:
Java.sql.Driver p = new com.mysql.cj.jdbc.Driver();
Note:
New keyword way creates tight coupling between java program and data base, so this way of
loading and registering is not recommended.
Class.forName way:
forName(): -
getConnection(): -
It is a static method.
Protocol:subprotocal://hostname:protnumber?data=value&data2=value2……..
Protocol: -
Sub protocol: -
Host:
• It is a machine which runs the programs and also manages the request.
• There are 2 types of host
▪ Localhost
▪ Remotehost
Local host: -
Whenever client and application(resource) both are present and running in a same
machine(system) then it is referred as localhost.
Ex: running mysql application and access in same system.
Remote Hose: -
Jdbc:mysql://localhost:3306?user=root&password=root
➔ Platform will help to compile the query, execute the query, get result of execution etc.,
➔ There are 3 different platforms
o Statement
o PreparedStatement
o CallableStatement
Statement:
createStatement() :
execute(): -
this is a generic method, used for executing all type of sql queries.
true: - DQL
executeUpdate(): -
int -> it is a number which represent number of rows got affected by the execution of sql query.
executeQuery(): -
Whenever we execute DQL queries we get some data, to fetch the result we make use of
ResultSet interface methods.
ResultSet: -
• it is a interface of jdbc api (java.sql.package)
• ResultSet object holds the tabular data by using resultset methods we can fetch data present in
it.
• In other words result set contains the results of sql query executed.
• ResultSet cursor always points before the first row.
• By using next() method of ResultSet we can move the cursor to next positions.
• next() is used o move the cursor to next record,(it moves the ursor with a step of 1).
• It return true if net row is present, it gives false if next row is not present
• public Boolean next();
• By using get****() method we can fetch data from specific column.
public *** get***(int columnNumber)
pubic *** get***(String columnName)
here *** represents data type.
PreparedStatement: -
prepareStatement(): -
Method Declaration: -
PlaceHolder in PreparedStatement:
→before executing the query data must be set for every placeholder.
→ Every placeholder represent columns, each place holder must be set with proper type of
value.
set***()
→ it is a method of PreparedStatement.
Statement PreparedStatement
• We create object of Statement by using • We create the oject of
createStatement() PreparedStatement by
prepareStatement()
• We pass the query at the time of • We pass the query at the time of
execution Platform createion
• In case of executing same query for • In case of executing same query multiple
multiple time, each time compilation and time, one compilation and multiple time
each time execution will happen execution will happens
• This is suitable for DDL type of queries • This is suitable for DML type of queries