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)
8. 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!
11. But as database of his customers
increased, his MySql database
was not able to handle it properly.
12. So he went back to DUCS
students and asked them to
replace MySQL database
with Oracle Database. Now
he can afford Oracle’s
license.
13. So the DUCS students asked him
to give them 20 lacs rupees.
14. Software cost was 10 lacs, but
changing the database cost him 20
lacs!
What happened there???
15. • 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
16. 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
17. - 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.
18. What Bill Gates did? 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
• 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.
21. TYPE 1: JDBC-ODBC Bridge
Used ODBC Drivers within.
Deprecated in JDK 6, Removed
from JDK 8.
Problem: Dependent on ODBC
drivers.
25. Type 4: Thin Driver (Pure Java)
Implemented By
DB Vendors
26. 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
27. 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.
28. 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.
29. 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???
30. 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
31. 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();
32. 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
33. 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!!!
}