Advanced Java 4th
Advanced Java 4th
Advanced Java 4th
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
ODBC
• 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 API
• 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
JDBC Architecture
Java
Application
2/25/2016
JDBC API
7
JDBC Driver Types
• JDBC driver implementation vary because of wide variety of
OS, hardware, databases in which Java operates. It divides into
2/25/2016
4 types:
• Type 1
2/25/2016
• The JDBC-ODBC bridge driver converts JDBC
9
Type 1 Driver (cont.)
2/25/2016
• can be easily connected to any database.
• Disadvantages:
11
Type 2 Drivers
2/25/2016
libraries of the database.
2/25/2016
• Disadvantage:
• The Native driver needs to be installed on the
14
Type 3 Drivers
• 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
Type 3 Drivers (cont.)
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
Type 4 Drivers (cont.)
2/25/2016
• No software is required at client side or server
side.
20
Which Driver should I used?
• 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
Step 1
• 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");
Step 2
• 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.
27
Concrete Classes for Interfaces
• 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
Statement Interface
• 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
• St.executeUpdate(“insert into dept values (‘comp’,0130,’GPAN’)”);
• Delete Record:
31
PreparedStatement and
CallableStatement Interface
• 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
Recommended Use
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
Statement Interface Hierarchy
2/25/2016
Mr. Nilesh Patil
35
Batch Execute Example
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 Interface
• ResultSet is table of data which represents a data
2/25/2016
from database.
• next() method is used to move cursor to next row.
37
ResultSet Interface methods
• beforeFirst()
2/25/2016
• afterLast()
• first()
2/25/2016
• For AutoCommit
• connection.setAutoCommit(false);
2/25/2016
connected to.
40
DatabaseMetaData Interface
• Obtaining a DatabaseMetaData Instance
2/25/2016
• DatabaseMetaData databaseMetaData = connection.getMetaData();
41
DatabaseMetaData Interface
• 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); }