JDBC
JAVA DATABASE
CONNECTIVITY
PRESENTED BY:
JDBC is a feature of JAVA
Programming Language that
provides us APIs(Classes and
Interfaces) to connect to different
databases
What is JDBC?
How it works?
Classes
and
Interfaces
(java.sql
and
javax.sql)
CURD Operations
CURD OPERATIONS
Create
Update
Retrive
Delete
Why use Databases?
• Done in last semester.
• But basically it provides capabilities that
Programming Languages cannot provide,
especially SECURITY.
• It is almost impossible to break into a
database and access data from outside.
• To access database from a programming
language, Database vendors provide their own
APIs (built in C/C++) for security reasons
which can be used by programming languages
(EMBEDDED SQL)
IF ALL DATABASES
PROVIDE THEIR OWN
APIs WHY THEN WHY USE
JDBC APIs?
BabuRao wants to make his
bussiness go online
He goes to DUCS students to
develop his software and ask them
to use MySql database as it is free
and supported by most servers.
Yes! BabuRao knows all this. He looks poor coz he
was an ENGINEER!
DUCS students agreed to
his terms and developed his
software in 10 lacs rupees
Soon BabuRao’s Bussiness went
big and he became RICH
But as database of his customers
increased, his MySql database
was not able to handle it properly.
So he went back to DUCS
students and asked them to
replace MySQL database
with Oracle Database. Now
he can afford Oracle’s
license.
So the DUCS students asked him
to give them 20 lacs rupees.
Software cost was 10 lacs, but
changing the database cost him 20
lacs!
What happened there???
• DUCS students developed
software using MySql specific
APIs.
• Changing MySql APIs with
Oracle APIs is so tedious that
they have to start building
again from scratch. Hence 20
lacs! :P
That means Using database
specific APIs make your software
database vendor dependent.
And this is a big problem as there
are many different database
vendors that provides different
benefites
- This is Billu
- Billu is Clever and Smart
- He cameup with ODBC
- Using ODBC, now any
programming language can
connect to any database.
-Billu is smart, Be like Billu.
What Bill Gates did? Did he
breached into all databases
security?
No. He just defined function
prototypes and asked database
vendors to implement them.
Now same connect() function call
can connect to different databases.
MySQL
Oracle
MS Access
connect
JDBC Drivers
• Using same approach, Interfaces were
defined in Java which can connect to
any database.
• A JDBC Driver is a middleware layer
that translates the JDBC calls to the
vendor specific APIs.
• There are 4 types of JDBC drivers
developed till now.
• However Only Type 4 driver is used
currently.
TYPE 1: JDBC-ODBC Bridge
Used ODBC Drivers within.
Deprecated in JDK 6, Removed
from JDK 8.
Problem: Dependent on ODBC
drivers.
How it worked?
TYPE-2 Native Drivers
Different for different Vendors
TYPE-3 Native Drivers
Used Type 2
drivers internally
SLOW
Type 4: Thin Driver (Pure Java)
Implemented By
DB Vendors
There are many interfaces in
JDBC Api. Out of these, there are
4 interfaces that all DB vendors
must implement:
1.Driver
2.Connection
3.Statement
4.ResultSet
5 Steps for Connecting Java to
Database
1.Loading the JDBC Driver.
2.Connecting to DBMS.
3.Creating and Executing a Statement.
4.Processing Data returned by DBMS.
5.Terminate the connection with DBMS.
1. Loading the JDBC Driver
Class.forName(“Oracle.Jdbc.driver.OracleDriver”)
What is Class.forName?
• Part of very cool topic of Java : Reflection
• Whole J2EE technology is built on Reflection
• With Reflection, you can change final variable
values and access private methods too!!!
• It loads the class in memory and returns a
Class class object.
What’s happening here?
We are loading a Driver class for Oracle
Database which is implemented by Oracle
Sql.
This class is found in
Oracle->Jdbc->driver->OracleDriver
Let us see if this class actually exists or not.
So we loaded this class but we did not make
use of the Class class object it returned.
Why???
2. Connecting to DBMS
Connection c =
DriverManager.getConnection(“Jdbc:Oracl
e:thin:@localhost:1521:XE”,”system”,”pas
sword”);
Attempts to establish a connection to the given database
URL. The DriverManager attempts to select an appropriate
driver from the set of registered JDBC drivers.
url - a database url of the form jdbc:subprotocol:subname
Returns:a connection to the URL
Throws:SQLException - if a database access error occurs
3. Creating and Executing a
Statement
Once connection is established, we can interact with
database. The Statement interface defines the methods
that we require to send/receive data from database
Creating a Statement
Statement S = c.createStatement();
Statement
Action Query
• Modifies Database
• Uses executeUpdate()
method of Statement
interface.
• Returns number of
rows modified.
Select Query
• No modification
• Uses executeQuery()
method of Statement
interface.
• Returns object
implementing
ResultSet interface
4. Processing Data returned by DBMS
ResultSet rs = s.executeQuery(“SELECT * FROM
emp”);
• ResultSet contains the data returned by database.
• It has various navigation, get and update methods to
view and update the data returned by database
while( rs.next() )
{
System.out.println( rs.getString(“Name”) );
System.out.println( rs.getString(2) );
//Overloaded!!!
}
5. Close the connection
c.close();
Let’s Run basic CURD Operations
now!!!
Create
Delete
Retrive
Update
Want to learn more?
Contact Us:
RSN

Jdbc_ravi_2016

  • 1.
  • 2.
    JDBC is afeature of JAVA Programming Language that provides us APIs(Classes and Interfaces) to connect to different databases What is JDBC?
  • 3.
  • 4.
  • 5.
    Why use Databases? •Done in last semester. • But basically it provides capabilities that Programming Languages cannot provide, especially SECURITY. • It is almost impossible to break into a database and access data from outside. • To access database from a programming language, Database vendors provide their own APIs (built in C/C++) for security reasons which can be used by programming languages (EMBEDDED SQL)
  • 6.
    IF ALL DATABASES PROVIDETHEIR OWN APIs WHY THEN WHY USE JDBC APIs?
  • 7.
    BabuRao wants tomake his bussiness go online
  • 8.
    He goes toDUCS students to develop his software and ask them to use MySql database as it is free and supported by most servers. Yes! BabuRao knows all this. He looks poor coz he was an ENGINEER!
  • 9.
    DUCS students agreedto his terms and developed his software in 10 lacs rupees
  • 10.
    Soon BabuRao’s Bussinesswent big and he became RICH
  • 11.
    But as databaseof his customers increased, his MySql database was not able to handle it properly.
  • 12.
    So he wentback to DUCS students and asked them to replace MySQL database with Oracle Database. Now he can afford Oracle’s license.
  • 13.
    So the DUCSstudents asked him to give them 20 lacs rupees.
  • 14.
    Software cost was10 lacs, but changing the database cost him 20 lacs! What happened there???
  • 15.
    • DUCS studentsdeveloped software using MySql specific APIs. • Changing MySql APIs with Oracle APIs is so tedious that they have to start building again from scratch. Hence 20 lacs! :P
  • 16.
    That means Usingdatabase specific APIs make your software database vendor dependent. And this is a big problem as there are many different database vendors that provides different benefites
  • 17.
    - This isBillu - Billu is Clever and Smart - He cameup with ODBC - Using ODBC, now any programming language can connect to any database. -Billu is smart, Be like Billu.
  • 18.
    What Bill Gatesdid? Did he breached into all databases security? No. He just defined function prototypes and asked database vendors to implement them.
  • 19.
    Now same connect()function call can connect to different databases. MySQL Oracle MS Access connect
  • 20.
    JDBC Drivers • Usingsame approach, Interfaces were defined in Java which can connect to any database. • A JDBC Driver is a middleware layer that translates the JDBC calls to the vendor specific APIs. • There are 4 types of JDBC drivers developed till now. • However Only Type 4 driver is used currently.
  • 21.
    TYPE 1: JDBC-ODBCBridge Used ODBC Drivers within. Deprecated in JDK 6, Removed from JDK 8. Problem: Dependent on ODBC drivers.
  • 22.
  • 23.
    TYPE-2 Native Drivers Differentfor different Vendors
  • 24.
    TYPE-3 Native Drivers UsedType 2 drivers internally SLOW
  • 25.
    Type 4: ThinDriver (Pure Java) Implemented By DB Vendors
  • 26.
    There are manyinterfaces in JDBC Api. Out of these, there are 4 interfaces that all DB vendors must implement: 1.Driver 2.Connection 3.Statement 4.ResultSet
  • 27.
    5 Steps forConnecting Java to Database 1.Loading the JDBC Driver. 2.Connecting to DBMS. 3.Creating and Executing a Statement. 4.Processing Data returned by DBMS. 5.Terminate the connection with DBMS.
  • 28.
    1. Loading theJDBC Driver Class.forName(“Oracle.Jdbc.driver.OracleDriver”) What is Class.forName? • Part of very cool topic of Java : Reflection • Whole J2EE technology is built on Reflection • With Reflection, you can change final variable values and access private methods too!!! • It loads the class in memory and returns a Class class object.
  • 29.
    What’s happening here? Weare loading a Driver class for Oracle Database which is implemented by Oracle Sql. This class is found in Oracle->Jdbc->driver->OracleDriver Let us see if this class actually exists or not. So we loaded this class but we did not make use of the Class class object it returned. Why???
  • 30.
    2. Connecting toDBMS Connection c = DriverManager.getConnection(“Jdbc:Oracl e:thin:@localhost:1521:XE”,”system”,”pas sword”); Attempts to establish a connection to the given database URL. The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers. url - a database url of the form jdbc:subprotocol:subname Returns:a connection to the URL Throws:SQLException - if a database access error occurs
  • 31.
    3. Creating andExecuting a Statement Once connection is established, we can interact with database. The Statement interface defines the methods that we require to send/receive data from database Creating a Statement Statement S = c.createStatement();
  • 32.
    Statement Action Query • ModifiesDatabase • Uses executeUpdate() method of Statement interface. • Returns number of rows modified. Select Query • No modification • Uses executeQuery() method of Statement interface. • Returns object implementing ResultSet interface
  • 33.
    4. Processing Datareturned by DBMS ResultSet rs = s.executeQuery(“SELECT * FROM emp”); • ResultSet contains the data returned by database. • It has various navigation, get and update methods to view and update the data returned by database while( rs.next() ) { System.out.println( rs.getString(“Name”) ); System.out.println( rs.getString(2) ); //Overloaded!!! }
  • 34.
    5. Close theconnection c.close();
  • 35.
    Let’s Run basicCURD Operations now!!!
  • 36.
  • 38.
    Want to learnmore? Contact Us: RSN