0% found this document useful (0 votes)
3 views41 pages

Unit 5

The document provides an overview of creating database-driven business applications using Java and the JDBC API. It explains different types of JDBC drivers (Type 1 to Type 4), their advantages and disadvantages, and how to establish connections, execute queries, and manage results. Additionally, it covers the use of Statement, PreparedStatement, and CallableStatement interfaces for interacting with databases.

Uploaded by

skiranwable948
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views41 pages

Unit 5

The document provides an overview of creating database-driven business applications using Java and the JDBC API. It explains different types of JDBC drivers (Type 1 to Type 4), their advantages and disadvantages, and how to establish connections, execute queries, and manage results. Additionally, it covers the use of Statement, PreparedStatement, and CallableStatement interfaces for interacting with databases.

Uploaded by

skiranwable948
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

To create database driven business applications

2/25/2016
models and the Java.Sql package

Mr. Nilesh Patil


2
Think of a database as just another device connected to
your computer

2/25/2016
Like other devices it has a driver program to relieves you
of having to do low level programming to use the

Mr. Nilesh Patil


database
The driver provides you with a high level API to the
database

3
Open Data Base Connectivity
Developed by Microsoft for the Windows

2/25/2016
platform as the way for Windows applications to
access Microsoft databases (SQL Server, FoxPro,

Mr. Nilesh Patil


Access)
Has become an industry standard
Most data base vendors supply native, ODBC,
and JDBC drivers for their data base products .
It was developed in C programming.
4
It is low level, high performance interface that is
designed specially for relational data stores.
JDBC drivers implement the defined interfaces in
the JDBC API for interacting with your database

2/25/2016
server.
Package: java.sql

Mr. Nilesh Patil


JDBC drivers enable you to open database
connections and to interact with it by sending
SQL or database commands then receiving
results with Java.

5
JDBC is a Java API that is used to
connect and execute query for the

2/25/2016
database.
JDBC API uses jdbc drivers to connects

Mr. Nilesh Patil


to the database.

6
Java
Application

2/25/2016
JDBC API

Mr. Nilesh Patil


Data Base Drivers

Access SQL DB2 Inform MySQ Sybase


ix L
Server

7
JDBC driver implementation vary because of wide variety of
OS, hardware, databases in which Java operates. It divides into
4 types:

2/25/2016
Type 1

Mr. Nilesh Patil


JDBC-ODBC Bridge
Type 2
Native API, partially java
Type 3
JDBC Network Driver, partially java
Type 4
8
100% Java
The JDBC-ODBC bridge driver uses ODBC driver
to connect to the database.

2/25/2016
The JDBC-ODBC bridge driver converts JDBC

Mr. Nilesh Patil


method calls into the ODBC function calls.

9
Mr. Nilesh Patil 2/25/2016
10
Advantages:
easy to use.

2/25/2016
can be easily connected to any database.
Disadvantages:

Mr. Nilesh Patil


Performance degraded because JDBC method call
is converted into the ODBC function calls.
The ODBC driver needs to be installed on the
client machine.

11
The Native API driver uses the client-side

2/25/2016
libraries of the database.

Mr. Nilesh Patil


The driver converts JDBC method calls into
native (c/c++ API) calls of the database API.

If we change database, we have to change native


API. 12
It is not written entirely in java.
Mr. Nilesh Patil 2/25/2016
13
Advantage:
performance upgraded than JDBC-ODBC bridge driver.

2/25/2016
Disadvantage:
The Native driver needs to be installed on the

Mr. Nilesh Patil


each client machine.
The Vendor client library needs to be installed on
client machine.

14
The Network Protocol driver uses middleware
(application server) that converts JDBC calls

2/25/2016
directly or indirectly into the vendor-specific
database protocol.

Mr. Nilesh Patil


It is fully written in java.

15
Mr. Nilesh Patil 2/25/2016
16
Advantage:
No client side library is required because of

2/25/2016
application server that can perform many tasks
like auditing, load balancing, logging etc.

Mr. Nilesh Patil


Disadvantages:
Network support is required on client machine.
Requires database-specific coding to be done in
the middle tier.
Maintenance of Network Protocol driver becomes
costly because it requires database-specific coding
to be done in the middle tier. 17
The thin driver converts JDBC calls directly into

2/25/2016
the vendor-specific database protocol. That is
why it is known as thin driver.

Mr. Nilesh Patil


It is fully written in Java language.

18
Mr. Nilesh Patil 2/25/2016
19
Advantage:
Better performance than all other drivers.

2/25/2016
No software is required at client side or server
side.

Mr. Nilesh Patil


Disadvantage:
Drivers depends on the Database.

20
If you are accessing one type of database, such as Oracle,
Sybase, or IBM, the preferred driver type is 4.

2/25/2016
If your Java application is accessing multiple types of
databases at the same time, type 3 is the preferred

Mr. Nilesh Patil


driver.
Type 2 drivers are useful in situations, where a type 3 or
type 4 driver is not available yet for your database.
The type 1 driver is not considered a deployment-level
driver, and is typically used for development and testing
prposes only.
21
Register the driver class
Creating connection

2/25/2016
Creating statement

Mr. Nilesh Patil


Executing queries
Closing connection

22
The forName() method of Class class is
used to register the driver class.

2/25/2016
This method is used to dynamically load

Mr. Nilesh Patil


the driver class.

public static void forName(String className)throws


ClassNotFoundException

Example 23
Class.forName("oracle.jdbc.driver.OracleDriver");
The getConnection() method of DriverManager class
is used to establish connection with the database.

2/25/2016
Syntax:
1) public static Connection getConnection(String url)th

Mr. Nilesh Patil


rows SQLException
2) public static Connection getConnection(String url,St
ring name,String password) throws SQLException
Example
Connection con=DriverManager.getConnection("jdbc:o
24
racle:thin:@localhost:1521:xe","system","password");
The createStatement() method of Connection
interface is used to create statement.

2/25/2016
The object of statement is responsible to execute
queries with the database.

Mr. Nilesh Patil


Syntax:
public Statement createStatement()throws SQLExc
eption
Ex:
Statement stmt=con.createStatement(); 25
The executeQuery() method of Statement interface is
used to execute queries to the database.
This method returns the object of ResultSet that can be

2/25/2016
used to get all the records of a table.
Syntax:

Mr. Nilesh Patil


public ResultSet executeQuery(String sql)throws SQLEx
ception
Ex:
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next()){
26
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
By closing connection object statement and
ResultSet will be closed automatically.
The close() method of Connection interface is used

2/25/2016
to close the connection.
public void close()throws SQLException

Mr. Nilesh Patil


Ex:
con.close();

27
ResultSet interface: oracle.jdbc.driver.OracleResultSetImpl
Statement: oracle.jdbc.driver.T4CStatement
Connection: oracle.jdbc.driver.T4CConnection

2/25/2016
PreparedStatement: oracle.jdbc.driver.T4CPreparedStatement

Mr. Nilesh Patil


28
Two tire architecture
Three tire architecture

2/25/2016
Mr. Nilesh Patil
29
It is mainly used to execute queries.
Few methods of Statement interface:
public ResultSet executeQuery(String sql)

2/25/2016
Used to execute select query.

Mr. Nilesh Patil


public int executeUpdate(String sql)
Used to execute create, insert, update, delete or drop etc
public boolean execute(String sql)
Executes the given SQL statement, which may return multiple results.
public int [] executeBatch()
Submits a batch of commands to the database for execution and if all
commands execute successfully, returns an array of update counts.
30
Insert Record:
Statement st = con.createStatement();

2/25/2016
Delete Record:

Mr. Nilesh Patil


Statement st = con.createStatement();

31
Precompiled sql statement object.

2/25/2016
It can read runtime input parameters.
Ex: PreparedStatement pstmt =

Mr. Nilesh Patil


CallableStatement interface used when Java
interacting with database using stored procedures.
Ex:
CallableStatement cstmt = null;
32
String SQL = "{call getEmpName (?, ?)}";
cstmt = conn.prepareCall (SQL);
String sql = "update DEPARTMENTS set DEPARTMENT_NAME =
? where DEPARTMENT_ID = ?";

2/25/2016
PreparedStatement pst = con.prepareStatement(sql);

Mr. Nilesh Patil


pst.setString(1, "Information Technology");

pst.setInt(2, 60);

pst.executeUpdate(); 33
Interfaces Recommended Use

Statement Use the for general-purpose access to your database. Useful

2/25/2016
when you are using static SQL statements at runtime. The
Statement interface cannot accept parameters.

Mr. Nilesh Patil


PreparedStatement Use the when you plan to use the SQL statements many times.
The PreparedStatement interface accepts input parameters at
runtime.

CallableStatement Use the when you want to access the database stored
procedures. The CallableStatement interface can also accept
runtime input parameters. 34
Mr. Nilesh Patil 2/25/2016
35
stmt.addBatch("update DEPARTMENTS set DEPARTMENT_NAME =
'Administrations' where DEPARTMENT_ID = 10");

2/25/2016
stmt.addBatch("update DEPARTMENTS set DEPARTMENT_NAME = 'Human
Resource' where DEPARTMENT_ID = 40");

Mr. Nilesh Patil


stmt.executeBatch();

36
ResultSet is table of data which represents a data
from database.

2/25/2016
next() method is used to move cursor to next row.

Mr. Nilesh Patil


Type of ResultSet: Bydefault TYPE_FORWARD_ONLY
ResultSet.TYPE_FORWARD_ONLY
ResultSet.TYPE_SCROLL_INSENSITIVE
ResultSet.TYPE_SCROLL_SENSITIVE

37
beforeFirst()

2/25/2016
afterLast()
first()

Mr. Nilesh Patil


last()
previous()
next()
getRow()
38
All action carried out or none of them.

2/25/2016
For AutoCommit
connection.setAutoCommit(false);

Mr. Nilesh Patil


For Rollback:
connection.rollback();
For Commit:
connection.commit();
39
Provide meta data about the database have
connected to.

2/25/2016
Mr. Nilesh Patil
For instance, you can see what tables are defined in
the database, and what columns each table has,
whether given features are supported etc.

40
Obtaining a DatabaseMetaData Instance

2/25/2016
DatabaseMetaData databaseMetaData = connection.getMetaData();

Database Product Name and Version

Mr. Nilesh Patil


int majorVersion = databaseMetaData.getDatabaseMajorVersion();
int minorVersion = databaseMetaData.getDatabaseMinorVersion();
String productName = databaseMetaData.getDatabaseProductName();
String productVersion =
databaseMetaData.getDatabaseProductVersion();

41
Database Driver Version

2/25/2016
int driverMajorVersion = databaseMetaData.getDriverMajorVersion();
int driverMinorVersion = databaseMetaData.getDriverMinorVersion();
Listing Tables

Mr. Nilesh Patil


String catalog = null;
String schemaPattern = null;
String tableNamePattern = null;
String[] types = null;
ResultSet result = databaseMetaData.getTables( catalog, schemaPattern,
tableNamePattern, types );

42
while(result.next()) { String tableName = result.getString(3); }

You might also like