Unit 5
Unit 5
2/25/2016
models and the Java.Sql package
2/25/2016
Like other devices it has a driver program to relieves you
of having to do low level programming to use the
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,
2/25/2016
server.
Package: java.sql
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
6
Java
Application
2/25/2016
JDBC API
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
2/25/2016
The JDBC-ODBC bridge driver converts JDBC
9
Mr. Nilesh Patil 2/25/2016
10
Advantages:
easy to use.
2/25/2016
can be easily connected to any database.
Disadvantages:
11
The Native API driver uses the client-side
2/25/2016
libraries of the database.
2/25/2016
Disadvantage:
The Native driver needs to be installed on the
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.
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.
2/25/2016
the vendor-specific database protocol. That is
why it is known as thin driver.
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.
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
2/25/2016
Creating statement
22
The forName() method of Class class is
used to register the driver class.
2/25/2016
This method is used to dynamically load
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
2/25/2016
The object of statement is responsible to execute
queries with the database.
2/25/2016
used to get all the records of a table.
Syntax:
2/25/2016
to close the connection.
public void close()throws SQLException
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
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.
2/25/2016
Delete Record:
31
Precompiled sql statement object.
2/25/2016
It can read runtime input parameters.
Ex: PreparedStatement pstmt =
2/25/2016
PreparedStatement pst = con.prepareStatement(sql);
pst.setInt(2, 60);
pst.executeUpdate(); 33
Interfaces Recommended Use
2/25/2016
when you are using static SQL statements at runtime. The
Statement interface cannot accept parameters.
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");
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.
37
beforeFirst()
2/25/2016
afterLast()
first()
2/25/2016
For AutoCommit
connection.setAutoCommit(false);
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();
41
Database Driver Version
2/25/2016
int driverMajorVersion = databaseMetaData.getDriverMajorVersion();
int driverMinorVersion = databaseMetaData.getDriverMinorVersion();
Listing Tables
42
while(result.next()) { String tableName = result.getString(3); }