Adv - Java GTU Study Material Presentations Unit-2 JDBC Programming
Adv - Java GTU Study Material Presentations Unit-2 JDBC Programming
Advanced Java
Unit-2
JDBC Programming
Reference Book:
Complete Reference J2EE by James Keogh mcgraw publication
Chapter : 6 and 7
Unit-2: JDBC Programming
1. Introduction
2. JDBC API
3. The JDBC Connectivity Model
4. JDBC Architecture
5. JDBC Driver
6. JDBC Components
7. JDBC Package
8. JDBC Process
9. JDBC Program
10. Types of Statement
11. ResultSet Interface
12. ResultSetMetaData Interface
13. Executing SQL updates
14. Transaction Management
15. Batch Processing in JDBC
1. Introduction
2. JDBC API
3. The JDBC Connectivity Model
4. JDBC Architecture
5. JDBC Driver
6. JDBC Components
7. JDBC Package
8. JDBC Process
9. JDBC Program
10. Types of Statement
11. ResultSet Interface
12. ResultSetMetaData Interface
13. Executing SQL updates
14. Transaction Management
15. Batch Processing in JDBC
Example
Oracle
MS Access
My SQL
SQL Server
..
.
1. Introduction
2. JDBC API
3. The JDBC Connectivity Model
4. JDBC Architecture
5. JDBC Driver
6. JDBC Components
7. JDBC Package
8. JDBC Process
9. JDBC Program
10. Types of Statement
11. ResultSet Interface
12. ResultSetMetaData Interface
13. Executing SQL updates
14. Transaction Management
15. Batch Processing in JDBC
1. Introduction
2. JDBC API
3. The JDBC Connectivity Model
4. JDBC Architecture
5. JDBC Driver
6. JDBC Components
7. JDBC Package
8. JDBC Process
9. JDBC Program
10. Types of Statement
11. ResultSet Interface
12. ResultSetMetaData Interface
13. Executing SQL updates
14. Transaction Management
15. Batch Processing in JDBC
JDBC API
JAVA
JDBC Driver
Application Database
1. Introduction
2. JDBC API
3. The JDBC Connectivity Model
4. JDBC Architecture
5. JDBC Driver
6. JDBC Components
7. JDBC Package
8. JDBC Process
9. JDBC Program
10. Types of Statement
11. ResultSet Interface
12. ResultSetMetaData Interface
13. Executing SQL updates
14. Transaction Management
15. Batch Processing in JDBC
1. Introduction
2. JDBC API
3. The JDBC Connectivity Model
4. JDBC Architecture
5. JDBC Driver
6. JDBC Components
7. JDBC Package
8. JDBC Process
9. JDBC Program
10. Types of Statement
11. ResultSet Interface
12. ResultSetMetaData Interface
13. Executing SQL updates
14. Transaction Management
15. Batch Processing in JDBC
Java Application
DB Vendor
Application Code Driver
Type 1
ODBC Local
JDBC ODBC Bridge
Driver
DBMS
Database Server
Unit-2 JDBC Programming 18 Darshan Institute of Engineering & Technology
JDBC Driver: Type 1 (JDBC-ODBC Driver)
Advantages :
Allow to communicate with all database supported by ODBC driver
It is vendor independent driver
Disadvantages:
Due to large number of translations, execution speed is decreased
Dependent on the ODBC driver
ODBC binary code or ODBC client library to be installed in every client
machine
Uses java native interface to make ODBC call
Because of listed disadvantage, type1 driver is not used in production
environment. It can only be used, when database doesn’t have any other
JDBC driver implementation.
Java Application
DB Vendor Driver
Application Code
Type 2
Native API Local
DBMS
Database Server
Unit-2 JDBC Programming 21 Darshan Institute of Engineering & Technology
JDBC Driver: Type 2 (Native Code Driver)
Advantages
As there is no implementation of JDBC-ODBC bridge, it may be
considerably faster than a Type 1 driver.
Disadvantages
The vendor client library needs to be installed on the client
machine.
This driver is platform dependent.
This driver supports all java applications except applets.
It may increase cost of application, if it needs to run on different
platform (since we may require buying the native libraries for all of
the platform).
Type 3
JDBC-Net pure Java JDBC Type 4 Driver
Database Server
Unit-2 JDBC Programming 24 Darshan Institute of Engineering & Technology
JDBC Driver: Type 3 (Java Protocol)
Advantages
Since the communication between client and the middleware
server is database independent, there is no need for the database
vendor library on the client.
A single driver can handle any database, provided the middleware
supports it.
We can switch from one database to other without changing the
client-side driver class, by just changing configurations of
middleware server.
E.g.: IDS Driver, Weblogic RMI Driver
Java Application
Application Code
Type 4
100% Pure Java Local
DBMS
Database Server
Unit-2 JDBC Programming 28 Darshan Institute of Engineering & Technology
JDBC Driver: Type 4 (Database Protocol)
Advantages
Completely implemented in Java to achieve platform independence.
No native libraries are required to be installed in client machine.
These drivers don't translate the requests into an intermediary format (such
as ODBC).
Secure to use since, it uses database server specific protocol.
The client application connects directly to the database server.
No translation or middleware layers are used, improving performance.
The JVM manages all the aspects of the application-to-database
connection.
Disadvantage
This Driver uses database specific protocol and it is DBMS vendor
dependent.
Working JDBC-> ODBC JDBC call -> native JDBC call -> JDBC call ->DB
call specific call middleware specific call
ODBC -> native specific.
call Middleware ->
native call
1. Introduction
2. JDBC API
3. The JDBC Connectivity Model
4. JDBC Architecture
5. JDBC Driver
6. JDBC Components
7. JDBC Package
8. JDBC Process
9. JDBC Program
10. Types of Statement
11. ResultSet Interface
12. ResultSetMetaData Interface
13. Executing SQL updates
14. Transaction Management
15. Batch Processing in JDBC
Connection
classes provided
application and by the JDBCItAPI.
database. contains all
methods for contacting a database.
Statement This interface is used to submit the SQL
statements to the database.
These objects hold data retrieved from a
ResultSet database after you execute an SQL query
using Statement objects. It acts as an
Exception
1. Introduction
2. JDBC API
3. The JDBC Connectivity Model
4. JDBC Architecture
5. JDBC Driver
6. JDBC Components
7. JDBC Package
8. JDBC Process
9. JDBC Program
10. Types of Statement
11. ResultSet Interface
12. ResultSetMetaData Interface
13. Executing SQL updates
14. Transaction Management
15. Batch Processing in JDBC
1. Introduction
2. JDBC API
3. The JDBC Connectivity Model
4. JDBC Architecture
5. JDBC Driver
6. JDBC Components
7. JDBC Package
8. JDBC Process
9. JDBC Program
10. Types of Statement
11. ResultSet Interface
12. ResultSetMetaData Interface
13. Executing SQL updates
14. Transaction Management
15. Batch Processing in JDBC
Class.forName("com.mysql.jdbc.Driver”);
Connection conn=
DriverManager.getConnection(URL,USER_NM,PASS)
;Class of java.sql package
Example:
Connection conn = DriverManager.getConnection
("jdbc:mysql://localhost:3306/gtu","root", "pwd");
Database Name
Statement st=con.createStatement();
Example
ResultSet rs = stmt.executeQuery
("SELECT * from diet");
1. Introduction
2. JDBC API
3. The JDBC Connectivity Model
4. JDBC Architecture
5. JDBC Driver
6. JDBC Components
7. JDBC Package
8. JDBC Process
9. JDBC Program
10. Types of Statement
11. ResultSet Interface
12. ResultSetMetaData Interface
13. Executing SQL updates
14. Transaction Management
15. Batch Processing in JDBC
Connection conn=
DriverManager.getConnection
("jdbc:mysql://localhost:3306/gtu",
“root”, “pwd”);
ResultSet rs = stmt.executeQuery("SELECT
* from diet");
while(rs.next())
System.out.print(rs.getString(1));
stmt.close();
conn.close();
Unit-2 JDBC Programming 56 Darshan Institute of Engineering & Technology
First JDBC Program
1. import java.sql.*;
2. public class ConnDemo {
3. public static void main(String[] args) {
4. try {
5. Class.forName("com.mysql.jdbc.Driver");
Database name
6. Connection conn= DriverManager.getConnection
7. ("jdbc:mysql://localhost:3306/gtu","root",”pwd");
8. Statement stmt = conn.createStatement();
9. ResultSet rs = stmt.executeQuery("SELECT * from diet");
10. while(rs.next()){
11. System.out.print(rs.getInt(1)+"\t");
Table name
12. System.out.print(rs.getString(“Name”)+"\t");
13. System.out.println(rs.getString(3));
14. }//while
15. stmt.close();
16. conn.close();
17. }catch(Exception e){System.out.println(e.toString());
18. }//PSVM }//class
1. Introduction
2. JDBC API
3. The JDBC Connectivity Model
4. JDBC Architecture
5. JDBC Driver
6. JDBC Components
7. JDBC Package
8. JDBC Process
9. JDBC Program
10. Types of Statement
11. ResultSet Interface
12. ResultSetMetaData Interface
13. Executing SQL updates
14. Transaction Management
15. Batch Processing in JDBC
Table Name
Parameter 2
public int executeUpdate() Executes the query. It is used for create, drop,
insert, update, delete etc.
INOUT A parameter that provides both input and output values. You bind
variables with the setXXX() methods and retrieve values with the
getXXX() methods.
Table: book
DB Column Name
DB Column Name
13. cs.close();
14. conn.close();
15. }catch(Exception e){System.out.println(e.toString());}
16. }//PSVM
17. }//class
1. Introduction
2. JDBC API
3. The JDBC Connectivity Model
4. JDBC Architecture
5. JDBC Driver
6. JDBC Components
7. JDBC Package
8. JDBC Process
9. JDBC Program
10. Types of Statement
11. ResultSet Interface
12. ResultSetMetaData Interface
13. Executing SQL updates
14. Transaction Management
15. Batch Processing in JDBC
2. Get methods Used to view the data in the columns of the current row
3. Update methods Used to update the data in the columns of the current
database as well.
boolean previous() Moves the cursor to the previous row. This method
throws SQLException returns false if the previous row is off the result set.
boolean absolute(int row) throws Moves the cursor to the specified row.
SQLException
boolean relative(int row) throws Moves the cursor the given number of rows forward
SQLException or backward, from where it is currently pointing.
int getRow() Returns the row number that the cursor is pointing to.
throws SQLException
int getInt(int columnIndex) throws Returns the int in the current row in the specified
SQLException column index. The column index starts at 1,
meaning the first column of a row is 1, the second
column of a row is 2, and so on.
1. Introduction
2. JDBC API
3. The JDBC Connectivity Model
4. JDBC Architecture
5. JDBC Driver
6. JDBC Components
7. JDBC Package
8. JDBC Process
9. JDBC Program
10. Types of Statement
11. ResultSet Interface
12. ResultSetMetaData Interface
13. Executing SQL updates
14. Transaction Management
15. Batch Processing in JDBC
String getColumnName(int index) it returns the column name of the specified column
throws SQLException index.
String getColumnTypeName(int it returns the column type name for the specified
index) index.
throws SQLException
1. Introduction
2. JDBC API
3. The JDBC Connectivity Model
4. JDBC Architecture
5. JDBC Driver
6. JDBC Components
7. JDBC Package
8. JDBC Process
9. JDBC Program
10. Types of Statement
11. ResultSet Interface
12. ResultSetMetaData Interface
13. Executing SQL updates
14. Transaction Management
15. Batch Processing in JDBC
1. Introduction
2. JDBC API
3. The JDBC Connectivity Model
4. JDBC Architecture
5. JDBC Driver
6. JDBC Components
7. JDBC Package
8. JDBC Process
9. JDBC Program
10. Types of Statement
11. ResultSet Interface
12. ResultSetMetaData Interface
13. Executing SQL updates
14. Transaction Management
15. Batch Processing in JDBC
mit
m
Co
Transaction
Initial State
Ro
llb
a ck
Transaction Failed
1. Introduction
2. JDBC API
3. The JDBC Connectivity Model
4. JDBC Architecture
5. JDBC Driver
6. JDBC Components
7. JDBC Package
8. JDBC Process
9. JDBC Program
10. Types of Statement
11. ResultSet Interface
12. ResultSetMetaData Interface
13. Executing SQL updates
14. Transaction Management
15. Batch Processing in JDBC
Variable
Undefined
Same query had
retrieved two
different value
One can get/set the current isolation level by using methods of Connection
interface:
1. getTransactionIsolation()
2. setTransactionIsolation(int isolationlevelconstant)
Unit-2 JDBC Programming 103 Darshan Institute of Engineering & Technology
SQL Exception
4. What is phantom read in JDBC? Which isolation level prevents it? [Sum -16]
5. Discuss CallableStatement with example. [Win -17]
[Win -18]
6. What is ResultSet interface. Write various method for ResultSet interface. [Win -19]
Write a code to update record using this interface.