SlideShare a Scribd company logo
Advanced Java Programming
by
Amol S. Gaikwad
Lecturer,
Government Polytechnic Gadchiroli
Advanced Java Programming
Unit-V
Interacting with
Database
Welcome!
Are you excited for a fun
learning session?
Unit Outcomes
Choose JDBC or ODBC depending on the given application requirements
Explain the function of given tier of JDBC architecture for two tier/ three tier
models
Use the relevant type of JDBC driver for the specified environment
Elaborate steps with example to established connectivity with the specified
database
Introduction to JDBC
Full form of JDBC is Java Database Connectivity
JDBC is a java API, which is used for connecting various types of databases with
java programming language
API defines how different software components can interact with each other
API defines how how and which function calls and requests to make and which
data to be used when interacting with different software componets
API (Application Programming Interface) - represents classes and interfaces
required for communication with other software programs or components
Introduction to JDBC
Software
program-1
Software
program-2
API (Application Programming Interface )
( collection of classes, interfaces, calls, request,
data formats etc. for communications )
Fig : API concept
Introduction to JDBC
With the help of JDBC we can save, delete, edit and fetch data and execute
various SQL queries on the database
JDBC is a java API, which is used for connecting various types of databases with
java programming language
JDBC API is used to get any kind of data stored in relational (tabular) database
JDBC API uses JDBC drivers to connect with the database
JDBC API works with java on various operating systems such as - windows, Mac OS,
UNIX
Introduction to JDBC
Database driver - A database driver is a computer program that implements a
protocol (ODBC or JDBC) for a database connection.
To connect with individual databases, JDBC requires drivers for each specific
database type.
Various types of java programs can use JDBC to use data from the database like :
Java Applications
Java Applets
Java Servlets
Java Server Pages
Introduction to JDBC
Java Application
JDBC Driver
JDBC API
Database
Fig : JDBC Concept
JDBC Driver is a software component that enables various types java programs to
interact with the database
JDBC Drivers
There are four types of JDBC drivers as shown below :
JDBC-ODBC
bridge driver
Native-API driver
(partially java
driver)
Network Protocol
driver (fully java
driver)
Thin driver (fully
java driver)
JDBC-ODBC Bridge Driver
The JDBC-ODBC bridge driver uses ODBC driver to connect to the database.
The JDBC-ODBC bridge driver converts JDBC method (function) calls into the ODBC
function calls.
However, we do not use this driver as pure java driver are available
Database vendors - these are companies which develop, sell, install, or maintains
database products, ex :- Oracle, Microsoft, IBM, SAP, etc.
Library - collection of files, programs, routines, scripts, functions that are used to
develop software
JDBC-ODBC Bridge Driver
Advantages :
Any database for which an ODBC driver is installed can be used
Performance slows down as function calls go through JDBC bridge to
ODBC and then to the native database connectivity interface
Disadvantages :
The ODBC driver needs to be installed on the client machine.
Not suitable for applets because ODBC driver needs to be installed on client
machine
No support from JDK 1.8 (Java 8).
JDBC-ODBC Bridge Driver
JDBC API
Java Application
JDBC Driver
Database
Vendor (
company )
database
library
Client Machine
Native-API Driver
The Native API driver uses the client-side libraries of the database
The driver converts JDBC method calls into native (C/C++) calls of the database API
It is not written completely in java
If we change the database then we have to change the native API also as it is specific
to database
Advantages :
This driver works faster than JDBC-ODBC bridge driver
Native-API Driver
Disadvantages :
The vendor client library needs to be installed on the client machine
Not all databases have a client-side library
This driver is platform dependent
This driver supports all Java applications except applets
Native-API Driver
JDBC API
Java Application
Native API
Driver Database
Vendor (
company )
database
library
Client Machine
Network Protocol Driver
This driver is fully written in java language
The Network Protocol driver uses middleware (application server) that converts
JDBC calls directly or indirectly into the vendor-specific database protocol
The JDBC clients use standard network sockets to communicate with a
midddleware application server
The socket information is then translated by middleware (application server) in
function calls understood by the database server
This driver is platform-independant because the middleware adjusts with
different platforms
Network Protocol Driver
This driver follows three-tier communication method
It works with multiple databases
Client driver to middleware communication is database independent
Advantages :
No need for the database vendor library on the client
No need to make changes in the client for new database
The middleware (application sercer) can provide various services like - caching,
load balancing, auditing, logging, security and firewall access
If middleware supports, then one drive can work with any database
Network Protocol Driver
Disdvantages :
Client machine requires network support
Requires database-specific coding to be done in the middleware (applicatio
server) which also increases maintenance cost of network protocol driver
The middleware (application sercer) can provide various services like - caching,
load balancing, auditing, logging, security and firewall access
Additional layer of middleware (application server) may increase processing
time
Network Protocol Driver
JDBC API
Java Application
Native
Protocol Driver Database
Middleware
(application
server )
Client Machine
Server side
Thin Driver (Database-Protocol Driver )
This protocol is fully written in java language
This driver converts JDBC function calls directly into a vendor-specific database
protocol
It is platform independent
As this driver is vendor specific, JDBC client requires separate drivers for different
types of database
Advantages :
Better performance than all other drivers
Client applications directly connects with server, hence addition layer of
middleware (application server) is not require and also improves performance
Disdvantages :
Drivers depends on database, for different database requires different drivers
Thin Driver (Database-Protocol Driver )
JDBC API
Java Application
Thin Driver
Database
Client Machine
JDBC Architecture
There are two types of JDBC architectures or processing models for database
access as follow :
Two-tier architecture Three-tier architecture
JDBC Two-Tier Architecture
Java Application
JDBC
Database
(DBMS)
Client Machine
DBMS proprietary protocol
Database Server
In two tier architecture the java application directly communicate with the
database or data source
JDBC Two-Tier Architecture
JDBC driver communicates with database or data source
In this architecure user commands are sent to the database and the required data
is sent back to the user
The database or data source are located on a machine somewhere in the network to
which our client machine is connected
This type of architecture can also be called as client-server architecture
User's machine act as a client and database or data source act as a server
JDBC Three-Tier Architecture
Java Applet or HTML
web browser
Database
(DBMS)
Client Machine / Graphical
User Interface (GUI)
Server Machine (Business Logic)
Database Server
Application Server (Java)
JDBC
HTML , RMI or other calls
DBMS proprietary protocol
In three tier architecture one additional middle tier called as middleware or
application server is added
JDBC Three -Tier Architecture
In this architecture java application commands are sent to the middle tier which
forwards the command to the data source or database
The data source processes the commands and sents the required data to middle
tier which forwards back the data to java application or user
The data source processes the commands and sents the required data to middle
tier which forwards back the data to java application or user
In many cases, the three-tier architecture can provide performance advantages.
JDBC Three -Tier Architecture
Earlier C / C++ language was used to write middle tier software but now java is
also used for writing middle tier prgram
Introduction to ODBC
The full form of ODBC is Open Database Connectivity
ODBC is an API (Application Programming Interface) which is used for accessing
different databases
ODBC does not depend on database, platform or operating system
Once we develop application using ODBC, it can run on any platform, both on the
client and server side, with few changes to the data access code
ODBC API uses ODBC driver to communicate between application and DBMS
ODBC was developed by Microsoft and Simba technologies in early 1990s
ODBC works with all types of languages such as C, C++, java etc whereas JDBC
works with only java language
Important Components of ODBC Architecure
Application - An ODBC architecture is any software or program that
communicates with database using ODBC function calls
ODBC Driver Manager - ODBC driver manager takes function calls from application
and forwards it to the ODBC driver, the driver then takes data from the database
and gives it back to the driver manager
ODBC Driver - ODBC driver accepts function calls from driver manager and takes
the required data from connected data source or database and returns it back to
driver manager
Data Source - Data source contains data and environment associated with it such
as software required to access that database, operating system and network
platform
ODBC Architecure
ODBC Application
ODBC API
ODBC Drive Manager
ODBC Drive
Datbase
( Data
Source )
Fig : ODBC Architecture
ODBC vs JDBC
JDBC
ODBC
ODBC works with all types of languages such
as C, C++, java etc.
JDBC works with only java language
ODBC is platform dependant - used with
windows OS only
JDBC is platform independant - used with any
operating system
ODBC is procedural JDBC is object oriented
Performance of ODBC is faster than JDBC Performance of JDBC is slower than ODBC
ODBC drivers are implemented in native
languages like C/C++.
JDBC drivers are implemented in Java.
ODBC was introduced by Microsoft JDBC was introduced by Sun MicroSystems
Which type of JDBC driver should be used in your software ?
If your are using only type of database in your software like Microsoft, Oracle,
Sybase, IBM etc then you should use type-4 (Thin Driver) JDBC driver
If your java application is using many databases at a same time then you should
use type-3 (Network Protocol Driver) JDBC driver
If type-3 (Network Protocol Driver) and type-4 (Thin Driver) are not available for
database you selected, then you should use type-2 (Native Driver) JDBC driver
Type-1 (JDBC-ODBC Bridge Driver) is used only for development and testing
purpose and not for deployment
Packages/Classes/Interface required for Java
Database Application (JDBC Application)
Package required :-
java.sql package
Classes required :-
DriverManager class
Interfaces required :-
Connection interface
Statement interface
PreparedStatement interface
ResultSet interface
DriverManager Class
The DriverManager class in java acts as an interface between user and drivers
It keeps track of how many and which drivers are available for connecting with
the database
It helps in establishing a connection between a database and the appropriate
driver
The DriverManager class maintains a list of Driver classes that have registered
themselves by calling the method(function) DriverManager.registerDriver().
Functions of DriverManager Class
public static void registerDriver(Driver driver) - this function registers the given
driver with DriverManager class
public static void deregisterDriver(Driver driver) - it de-registers the given driver
(drop the driver from the list) with DriverManager
public static Connection getConnection(String url) - it is used to establish the
connection with the given url
public static Connection getConnection(String url,String userName,String password) -
it is used to establish the connection with the given url, and given username and
password
Connection Interface
The Connection interface in java helps to establish connection with the database
A Connection is the session between java application and database
The Connection interface is a factory of Statement, PreparedStatement, and
DatabaseMetaData - it means, object of Connection interface can be used to get the
object of Statement and DatabaseMetaData
The Connection interface has many methods (functions) for transaction
management like - rollback( ), commit( ), close( ) etc.
Connection Interface
The Connection interface in java helps to establish connection with the database
A Connection is the session between java application and database
The Connection interface is a factory of Statement, PreparedStatement, and
DatabaseMetaData - it means, object of Connection interface can be used to get the
object of Statement and DatabaseMetaData
The Connection interface has many methods (functions) for transaction
management like - rollback( ), commit( ), close( ) etc.
Functions of Connection Interface
public Statement createStatement() - this function creates object of Statement
interface which can be use to execute SQL queries
public Statement createStatement(int resultSetType,int resultSetConcurrency) -
creates object of Statement interface that generates object of ResultSet interface
with given type and concurrency
public void setAutoCommit(boolean status) - this function sets the commit status,
by default it's value is true
public void commit() - this function permanently saves the changes done since
previous commit or rollback
Functions of Connection Interface
public void rollback( ) - drops or undo all changes done since the previous commit or
rollback
public void close( ) - closes the connection and releases all JDBC resources
immediately
Statement Interface
The Statement interface provides methods(functions) which are used to execute
SQL queries with the database
Object of Statement interface can work with only static query
The statement interface is a factory of ResultSet interface i.e. it provides factory
method to get the object of ResultSet interface
The statement interface is a factory of ResultSet interface i.e. it provides factory
method to get the object of ResultSet interface
Functions of Statement Interface
public ResultSet executeQuery(String sql) - it is used to execute SELECT SQL query
which retrieves (gives) data from the database. It returns the object of ResultSet
interface
public int executeUpdate(String sql) - this function is used to make modification in
your database like create, drop, insert, update, delete etc
public boolean execute(String sql) - this method is used for all type of SQL statement
(eg.Select, Insert, Update etc.), it is used to execute queries that may return
multiple results
public int[] executeBatch(): this function is used to execute batch (group) of
commands. It returns a array of integer type
ResultSet Interface
ResultSet interface represents the set of results of a database query
The object of ResultSet maintains a cursor pointing to a row of a table, initially,
cursor points to before the first row
The methods of the ResultSet interface can be divided into three categories -
Navigational methods - these methods are used to move the cursor around
Get methods: used to see the data in the columns of the current row being
pointed by the cursor
Update methods: used to update the data in the columns of the current row
By default, ResultSet object can be moved forward only and it is not updatable
ResultSet Interface
Types of Resultset - denoted by three constants as below
ResultSet.TYPE_FORWARD_ONLY - The cursor can only move forward in the result set
ResultSet.TYPE_SCROLL_INSENSITIVE - The cursor can scroll forward and backward and if
changed are made in database after result set was created then result remains
unchanged (i.e not sensitive)
ResultSet.TYPE_SCROLL_SENSITIVE - The cursor can scroll forward and backward and if
changed are made in database after result set was created then result remains also
changes (i.e sensitive)
Concurrency of ResultSet - means updatable or not
ResultSet.CONCUR_READ_ONLY - creates a read-only result set, this is the default value
ResultSet.CONCUR_UPDATABLE - creates an updateable result set
ResultSet Interface
Fig: - concept of ResultSet
Datbase
data data
data data
data data
ResultSet
object
cursor
Functions of ResultSet Interface
public boolean next() - is used to move the cursor to the one row next from the current
position
public boolean previous() - is used to move the cursor to the one row previous from the
current position
public boolean first() - is used to move the cursor to the first row in result set object
public boolean last() - is used to move the cursor to the last row in result set object
public boolean absolute(int row) - is used to move the cursor to the specified row number in
the ResultSet object
public boolean relative(int row) - is used to move the cursor to the relative row number in
the ResultSet object, it may be positive or negative
Functions of ResultSet Interface
public int getInt(int columnIndex) - is used to return the data of specified column index of the
current row as int
public int getInt(String columnName) - is used to return the data of specified column name of
the current row as int
public String getString(int columnIndex) - is used to return the data of specified column index
of the current row as String
public String getString(String columnName) - is used to return the data of specified column
name of the current row as String
PreparedStatement Interface
The PreparedStatement interface is a subinterface (child) of Statement interface and it is
used to execute SQL queries with parameters
The PreparedStatement interface accepts input parameters at runtime
If we use PreparedStament interface then performance of application will be faster because
query is compiled only once
PreparedStament interface is used for the queries which are to be executed multiple times
We can used Preparedstatement interface for reading and writing binary data
Object of PreparedStatement interface is returned by prepareStatement() method of
Connection interface
Performance of PreparedStatement is better than Statement interface
Function of PreparedStatement Interface
public void setInt(int paramIndex, int value) - this function sets the integer value to the given
parameter index
public void setString(int paramIndex, String value) - sets the String value to the given
parameter index
public void setFloat(int paramIndex, float value) - sets the float value to the given parameter
index
public void setDouble(int paramIndex, double value) - sets the double value to the given
parameter index
public int executeUpdate() - executes the query. It is used for create, drop, insert, update,
delete etc
public ResultSet executeQuery() - executes the SELECT query. It returns an instance (object) of
ResultSet interface
JDBC Environment Setup
JDBC environment means what software components are require to develop a JDBC
application
Following components are require to develop a java database applications :
Install Java - JDK 5.0 or greater versions with JDBC packages ( java.sql and javax.sql
packages )
Install Database - open source databases like MySQL DB, postgreSQL or commercial
database like Oracle DB
Install database drivers - JDBC-ODBC bridge drivers, thin driver etc.
Steps for creating JDBC application
Open connection with database - create Connection object using
DriverManager.getConnection( ) method
Extract data from the result set - use functions of ResultSet interface to get data
from result set
Import the packages for JDBC classes - java.sql.* or javax.sql.*
Register the JDBC driver - for opening communication channel with database
Execute Query - create object of Statement interface
Clean up the environment - close all database connections and resources
Ad

More Related Content

Similar to Java and Database - Interacting with database (20)

jdbc.pptx jdbc.pptxjdbc.pptxjdbc.pptxjdb
jdbc.pptx jdbc.pptxjdbc.pptxjdbc.pptxjdbjdbc.pptx jdbc.pptxjdbc.pptxjdbc.pptxjdb
jdbc.pptx jdbc.pptxjdbc.pptxjdbc.pptxjdb
devamrana27
 
JDBC Architecture and Drivers
JDBC Architecture and DriversJDBC Architecture and Drivers
JDBC Architecture and Drivers
SimoniShah6
 
Unit 5-jdbc2
Unit 5-jdbc2Unit 5-jdbc2
Unit 5-jdbc2
msafad
 
JDBC-Introduction
JDBC-IntroductionJDBC-Introduction
JDBC-Introduction
Mythili Shankar
 
jdbc
jdbcjdbc
jdbc
Gayatri Patel
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
Vikas Jagtap
 
Jdbc drivers
Jdbc driversJdbc drivers
Jdbc drivers
Saurabh Bhartiya
 
Core jdbc basics
Core jdbc basicsCore jdbc basics
Core jdbc basics
Sourabrata Mukherjee
 
Jdbc driver types
Jdbc driver typesJdbc driver types
Jdbc driver types
Soham Kansodaria
 
jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx
 jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx
jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx
ujjwalmatoliya
 
Jdbc 1
Jdbc 1Jdbc 1
Jdbc 1
Mukesh Tekwani
 
Rajesh jdbc
Rajesh   jdbcRajesh   jdbc
Rajesh jdbc
Aditya Sharma
 
Ajp notes-chapter-05
Ajp notes-chapter-05Ajp notes-chapter-05
Ajp notes-chapter-05
JONDHLEPOLY
 
Ajp notes-chapter-05
Ajp notes-chapter-05Ajp notes-chapter-05
Ajp notes-chapter-05
Ankit Dubey
 
UNIT 1 PPT- FINAL.pptx Advance Java JDBC
UNIT 1 PPT- FINAL.pptx Advance Java JDBCUNIT 1 PPT- FINAL.pptx Advance Java JDBC
UNIT 1 PPT- FINAL.pptx Advance Java JDBC
iamspidey0104
 
Jdbc introduction
Jdbc introductionJdbc introduction
Jdbc introduction
Rakesh Kumar Ray
 
jdbc-130913021409-phpapp01000988www.pptx
jdbc-130913021409-phpapp01000988www.pptxjdbc-130913021409-phpapp01000988www.pptx
jdbc-130913021409-phpapp01000988www.pptx
ssuser8878c1
 
Introduction of java jdbc
Introduction of java jdbcIntroduction of java jdbc
Introduction of java jdbc
firstenquiry
 
Introduction of java jdbc
Introduction of java jdbcIntroduction of java jdbc
Introduction of java jdbc
firstenquiry
 
JDBC Driver Types
JDBC Driver TypesJDBC Driver Types
JDBC Driver Types
Rahul Sharma
 
jdbc.pptx jdbc.pptxjdbc.pptxjdbc.pptxjdb
jdbc.pptx jdbc.pptxjdbc.pptxjdbc.pptxjdbjdbc.pptx jdbc.pptxjdbc.pptxjdbc.pptxjdb
jdbc.pptx jdbc.pptxjdbc.pptxjdbc.pptxjdb
devamrana27
 
JDBC Architecture and Drivers
JDBC Architecture and DriversJDBC Architecture and Drivers
JDBC Architecture and Drivers
SimoniShah6
 
Unit 5-jdbc2
Unit 5-jdbc2Unit 5-jdbc2
Unit 5-jdbc2
msafad
 
jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx
 jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx
jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx
ujjwalmatoliya
 
Ajp notes-chapter-05
Ajp notes-chapter-05Ajp notes-chapter-05
Ajp notes-chapter-05
JONDHLEPOLY
 
Ajp notes-chapter-05
Ajp notes-chapter-05Ajp notes-chapter-05
Ajp notes-chapter-05
Ankit Dubey
 
UNIT 1 PPT- FINAL.pptx Advance Java JDBC
UNIT 1 PPT- FINAL.pptx Advance Java JDBCUNIT 1 PPT- FINAL.pptx Advance Java JDBC
UNIT 1 PPT- FINAL.pptx Advance Java JDBC
iamspidey0104
 
jdbc-130913021409-phpapp01000988www.pptx
jdbc-130913021409-phpapp01000988www.pptxjdbc-130913021409-phpapp01000988www.pptx
jdbc-130913021409-phpapp01000988www.pptx
ssuser8878c1
 
Introduction of java jdbc
Introduction of java jdbcIntroduction of java jdbc
Introduction of java jdbc
firstenquiry
 
Introduction of java jdbc
Introduction of java jdbcIntroduction of java jdbc
Introduction of java jdbc
firstenquiry
 

More from Amol Gaikwad (13)

Computer Graphics - Graphics File Formats.pdf
Computer Graphics - Graphics File Formats.pdfComputer Graphics - Graphics File Formats.pdf
Computer Graphics - Graphics File Formats.pdf
Amol Gaikwad
 
Computer Graphics - Cartesian Coordinate System.pdf
Computer Graphics - Cartesian Coordinate System.pdfComputer Graphics - Cartesian Coordinate System.pdf
Computer Graphics - Cartesian Coordinate System.pdf
Amol Gaikwad
 
IT Resources for Students.pdf
IT Resources for Students.pdfIT Resources for Students.pdf
IT Resources for Students.pdf
Amol Gaikwad
 
Unit-IV Windowing and Clipping.pdf
Unit-IV Windowing and Clipping.pdfUnit-IV Windowing and Clipping.pdf
Unit-IV Windowing and Clipping.pdf
Amol Gaikwad
 
How To Prepare Resume.pdf
How To Prepare Resume.pdfHow To Prepare Resume.pdf
How To Prepare Resume.pdf
Amol Gaikwad
 
Unit-3 overview of transformations
Unit-3 overview of transformationsUnit-3 overview of transformations
Unit-3 overview of transformations
Amol Gaikwad
 
Unit 1 संगणक प्रणाली ( computer system ) ची ओळख
Unit 1 संगणक प्रणाली ( computer system ) ची ओळखUnit 1 संगणक प्रणाली ( computer system ) ची ओळख
Unit 1 संगणक प्रणाली ( computer system ) ची ओळख
Amol Gaikwad
 
Unit-4 networking basics in java
Unit-4 networking basics in javaUnit-4 networking basics in java
Unit-4 networking basics in java
Amol Gaikwad
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handling
Amol Gaikwad
 
Unit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithmsUnit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithms
Amol Gaikwad
 
Unit-2 swing and mvc architecture
Unit-2 swing and mvc architectureUnit-2 swing and mvc architecture
Unit-2 swing and mvc architecture
Amol Gaikwad
 
Unit-1 basics of computer graphics
Unit-1 basics of computer graphicsUnit-1 basics of computer graphics
Unit-1 basics of computer graphics
Amol Gaikwad
 
Unit-1 awt advanced java programming
Unit-1 awt advanced java programmingUnit-1 awt advanced java programming
Unit-1 awt advanced java programming
Amol Gaikwad
 
Computer Graphics - Graphics File Formats.pdf
Computer Graphics - Graphics File Formats.pdfComputer Graphics - Graphics File Formats.pdf
Computer Graphics - Graphics File Formats.pdf
Amol Gaikwad
 
Computer Graphics - Cartesian Coordinate System.pdf
Computer Graphics - Cartesian Coordinate System.pdfComputer Graphics - Cartesian Coordinate System.pdf
Computer Graphics - Cartesian Coordinate System.pdf
Amol Gaikwad
 
IT Resources for Students.pdf
IT Resources for Students.pdfIT Resources for Students.pdf
IT Resources for Students.pdf
Amol Gaikwad
 
Unit-IV Windowing and Clipping.pdf
Unit-IV Windowing and Clipping.pdfUnit-IV Windowing and Clipping.pdf
Unit-IV Windowing and Clipping.pdf
Amol Gaikwad
 
How To Prepare Resume.pdf
How To Prepare Resume.pdfHow To Prepare Resume.pdf
How To Prepare Resume.pdf
Amol Gaikwad
 
Unit-3 overview of transformations
Unit-3 overview of transformationsUnit-3 overview of transformations
Unit-3 overview of transformations
Amol Gaikwad
 
Unit 1 संगणक प्रणाली ( computer system ) ची ओळख
Unit 1 संगणक प्रणाली ( computer system ) ची ओळखUnit 1 संगणक प्रणाली ( computer system ) ची ओळख
Unit 1 संगणक प्रणाली ( computer system ) ची ओळख
Amol Gaikwad
 
Unit-4 networking basics in java
Unit-4 networking basics in javaUnit-4 networking basics in java
Unit-4 networking basics in java
Amol Gaikwad
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handling
Amol Gaikwad
 
Unit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithmsUnit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithms
Amol Gaikwad
 
Unit-2 swing and mvc architecture
Unit-2 swing and mvc architectureUnit-2 swing and mvc architecture
Unit-2 swing and mvc architecture
Amol Gaikwad
 
Unit-1 basics of computer graphics
Unit-1 basics of computer graphicsUnit-1 basics of computer graphics
Unit-1 basics of computer graphics
Amol Gaikwad
 
Unit-1 awt advanced java programming
Unit-1 awt advanced java programmingUnit-1 awt advanced java programming
Unit-1 awt advanced java programming
Amol Gaikwad
 
Ad

Recently uploaded (20)

A Survey of Personalized Large Language Models.pptx
A Survey of Personalized Large Language Models.pptxA Survey of Personalized Large Language Models.pptx
A Survey of Personalized Large Language Models.pptx
rutujabhaskarraopati
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .
NABLAS株式会社
 
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning ModelsMode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Journal of Soft Computing in Civil Engineering
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32
CircuitDigest
 
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Journal of Soft Computing in Civil Engineering
 
Redirects Unraveled: From Lost Links to Rickrolls
Redirects Unraveled: From Lost Links to RickrollsRedirects Unraveled: From Lost Links to Rickrolls
Redirects Unraveled: From Lost Links to Rickrolls
Kritika Garg
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
Novel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth ControlNovel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth Control
Chris Harding
 
Surveying through global positioning system
Surveying through global positioning systemSurveying through global positioning system
Surveying through global positioning system
opneptune5
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdfML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
rameshwarchintamani
 
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Journal of Soft Computing in Civil Engineering
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
A Survey of Personalized Large Language Models.pptx
A Survey of Personalized Large Language Models.pptxA Survey of Personalized Large Language Models.pptx
A Survey of Personalized Large Language Models.pptx
rutujabhaskarraopati
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .
NABLAS株式会社
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32
CircuitDigest
 
Redirects Unraveled: From Lost Links to Rickrolls
Redirects Unraveled: From Lost Links to RickrollsRedirects Unraveled: From Lost Links to Rickrolls
Redirects Unraveled: From Lost Links to Rickrolls
Kritika Garg
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
Novel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth ControlNovel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth Control
Chris Harding
 
Surveying through global positioning system
Surveying through global positioning systemSurveying through global positioning system
Surveying through global positioning system
opneptune5
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdfML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
rameshwarchintamani
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
Ad

Java and Database - Interacting with database

  • 1. Advanced Java Programming by Amol S. Gaikwad Lecturer, Government Polytechnic Gadchiroli
  • 3. Welcome! Are you excited for a fun learning session?
  • 4. Unit Outcomes Choose JDBC or ODBC depending on the given application requirements Explain the function of given tier of JDBC architecture for two tier/ three tier models Use the relevant type of JDBC driver for the specified environment Elaborate steps with example to established connectivity with the specified database
  • 5. Introduction to JDBC Full form of JDBC is Java Database Connectivity JDBC is a java API, which is used for connecting various types of databases with java programming language API defines how different software components can interact with each other API defines how how and which function calls and requests to make and which data to be used when interacting with different software componets API (Application Programming Interface) - represents classes and interfaces required for communication with other software programs or components
  • 6. Introduction to JDBC Software program-1 Software program-2 API (Application Programming Interface ) ( collection of classes, interfaces, calls, request, data formats etc. for communications ) Fig : API concept
  • 7. Introduction to JDBC With the help of JDBC we can save, delete, edit and fetch data and execute various SQL queries on the database JDBC is a java API, which is used for connecting various types of databases with java programming language JDBC API is used to get any kind of data stored in relational (tabular) database JDBC API uses JDBC drivers to connect with the database JDBC API works with java on various operating systems such as - windows, Mac OS, UNIX
  • 8. Introduction to JDBC Database driver - A database driver is a computer program that implements a protocol (ODBC or JDBC) for a database connection. To connect with individual databases, JDBC requires drivers for each specific database type. Various types of java programs can use JDBC to use data from the database like : Java Applications Java Applets Java Servlets Java Server Pages
  • 9. Introduction to JDBC Java Application JDBC Driver JDBC API Database Fig : JDBC Concept
  • 10. JDBC Driver is a software component that enables various types java programs to interact with the database JDBC Drivers There are four types of JDBC drivers as shown below : JDBC-ODBC bridge driver Native-API driver (partially java driver) Network Protocol driver (fully java driver) Thin driver (fully java driver)
  • 11. JDBC-ODBC Bridge Driver The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC bridge driver converts JDBC method (function) calls into the ODBC function calls. However, we do not use this driver as pure java driver are available Database vendors - these are companies which develop, sell, install, or maintains database products, ex :- Oracle, Microsoft, IBM, SAP, etc. Library - collection of files, programs, routines, scripts, functions that are used to develop software
  • 12. JDBC-ODBC Bridge Driver Advantages : Any database for which an ODBC driver is installed can be used Performance slows down as function calls go through JDBC bridge to ODBC and then to the native database connectivity interface Disadvantages : The ODBC driver needs to be installed on the client machine. Not suitable for applets because ODBC driver needs to be installed on client machine No support from JDK 1.8 (Java 8).
  • 13. JDBC-ODBC Bridge Driver JDBC API Java Application JDBC Driver Database Vendor ( company ) database library Client Machine
  • 14. Native-API Driver The Native API driver uses the client-side libraries of the database The driver converts JDBC method calls into native (C/C++) calls of the database API It is not written completely in java If we change the database then we have to change the native API also as it is specific to database Advantages : This driver works faster than JDBC-ODBC bridge driver
  • 15. Native-API Driver Disadvantages : The vendor client library needs to be installed on the client machine Not all databases have a client-side library This driver is platform dependent This driver supports all Java applications except applets
  • 16. Native-API Driver JDBC API Java Application Native API Driver Database Vendor ( company ) database library Client Machine
  • 17. Network Protocol Driver This driver is fully written in java language The Network Protocol driver uses middleware (application server) that converts JDBC calls directly or indirectly into the vendor-specific database protocol The JDBC clients use standard network sockets to communicate with a midddleware application server The socket information is then translated by middleware (application server) in function calls understood by the database server This driver is platform-independant because the middleware adjusts with different platforms
  • 18. Network Protocol Driver This driver follows three-tier communication method It works with multiple databases Client driver to middleware communication is database independent Advantages : No need for the database vendor library on the client No need to make changes in the client for new database The middleware (application sercer) can provide various services like - caching, load balancing, auditing, logging, security and firewall access If middleware supports, then one drive can work with any database
  • 19. Network Protocol Driver Disdvantages : Client machine requires network support Requires database-specific coding to be done in the middleware (applicatio server) which also increases maintenance cost of network protocol driver The middleware (application sercer) can provide various services like - caching, load balancing, auditing, logging, security and firewall access Additional layer of middleware (application server) may increase processing time
  • 20. Network Protocol Driver JDBC API Java Application Native Protocol Driver Database Middleware (application server ) Client Machine Server side
  • 21. Thin Driver (Database-Protocol Driver ) This protocol is fully written in java language This driver converts JDBC function calls directly into a vendor-specific database protocol It is platform independent As this driver is vendor specific, JDBC client requires separate drivers for different types of database Advantages : Better performance than all other drivers Client applications directly connects with server, hence addition layer of middleware (application server) is not require and also improves performance Disdvantages : Drivers depends on database, for different database requires different drivers
  • 22. Thin Driver (Database-Protocol Driver ) JDBC API Java Application Thin Driver Database Client Machine
  • 23. JDBC Architecture There are two types of JDBC architectures or processing models for database access as follow : Two-tier architecture Three-tier architecture
  • 24. JDBC Two-Tier Architecture Java Application JDBC Database (DBMS) Client Machine DBMS proprietary protocol Database Server
  • 25. In two tier architecture the java application directly communicate with the database or data source JDBC Two-Tier Architecture JDBC driver communicates with database or data source In this architecure user commands are sent to the database and the required data is sent back to the user The database or data source are located on a machine somewhere in the network to which our client machine is connected This type of architecture can also be called as client-server architecture User's machine act as a client and database or data source act as a server
  • 26. JDBC Three-Tier Architecture Java Applet or HTML web browser Database (DBMS) Client Machine / Graphical User Interface (GUI) Server Machine (Business Logic) Database Server Application Server (Java) JDBC HTML , RMI or other calls DBMS proprietary protocol
  • 27. In three tier architecture one additional middle tier called as middleware or application server is added JDBC Three -Tier Architecture In this architecture java application commands are sent to the middle tier which forwards the command to the data source or database The data source processes the commands and sents the required data to middle tier which forwards back the data to java application or user The data source processes the commands and sents the required data to middle tier which forwards back the data to java application or user
  • 28. In many cases, the three-tier architecture can provide performance advantages. JDBC Three -Tier Architecture Earlier C / C++ language was used to write middle tier software but now java is also used for writing middle tier prgram
  • 29. Introduction to ODBC The full form of ODBC is Open Database Connectivity ODBC is an API (Application Programming Interface) which is used for accessing different databases ODBC does not depend on database, platform or operating system Once we develop application using ODBC, it can run on any platform, both on the client and server side, with few changes to the data access code ODBC API uses ODBC driver to communicate between application and DBMS ODBC was developed by Microsoft and Simba technologies in early 1990s ODBC works with all types of languages such as C, C++, java etc whereas JDBC works with only java language
  • 30. Important Components of ODBC Architecure Application - An ODBC architecture is any software or program that communicates with database using ODBC function calls ODBC Driver Manager - ODBC driver manager takes function calls from application and forwards it to the ODBC driver, the driver then takes data from the database and gives it back to the driver manager ODBC Driver - ODBC driver accepts function calls from driver manager and takes the required data from connected data source or database and returns it back to driver manager Data Source - Data source contains data and environment associated with it such as software required to access that database, operating system and network platform
  • 31. ODBC Architecure ODBC Application ODBC API ODBC Drive Manager ODBC Drive Datbase ( Data Source ) Fig : ODBC Architecture
  • 32. ODBC vs JDBC JDBC ODBC ODBC works with all types of languages such as C, C++, java etc. JDBC works with only java language ODBC is platform dependant - used with windows OS only JDBC is platform independant - used with any operating system ODBC is procedural JDBC is object oriented Performance of ODBC is faster than JDBC Performance of JDBC is slower than ODBC ODBC drivers are implemented in native languages like C/C++. JDBC drivers are implemented in Java. ODBC was introduced by Microsoft JDBC was introduced by Sun MicroSystems
  • 33. Which type of JDBC driver should be used in your software ? If your are using only type of database in your software like Microsoft, Oracle, Sybase, IBM etc then you should use type-4 (Thin Driver) JDBC driver If your java application is using many databases at a same time then you should use type-3 (Network Protocol Driver) JDBC driver If type-3 (Network Protocol Driver) and type-4 (Thin Driver) are not available for database you selected, then you should use type-2 (Native Driver) JDBC driver Type-1 (JDBC-ODBC Bridge Driver) is used only for development and testing purpose and not for deployment
  • 34. Packages/Classes/Interface required for Java Database Application (JDBC Application) Package required :- java.sql package Classes required :- DriverManager class Interfaces required :- Connection interface Statement interface PreparedStatement interface ResultSet interface
  • 35. DriverManager Class The DriverManager class in java acts as an interface between user and drivers It keeps track of how many and which drivers are available for connecting with the database It helps in establishing a connection between a database and the appropriate driver The DriverManager class maintains a list of Driver classes that have registered themselves by calling the method(function) DriverManager.registerDriver().
  • 36. Functions of DriverManager Class public static void registerDriver(Driver driver) - this function registers the given driver with DriverManager class public static void deregisterDriver(Driver driver) - it de-registers the given driver (drop the driver from the list) with DriverManager public static Connection getConnection(String url) - it is used to establish the connection with the given url public static Connection getConnection(String url,String userName,String password) - it is used to establish the connection with the given url, and given username and password
  • 37. Connection Interface The Connection interface in java helps to establish connection with the database A Connection is the session between java application and database The Connection interface is a factory of Statement, PreparedStatement, and DatabaseMetaData - it means, object of Connection interface can be used to get the object of Statement and DatabaseMetaData The Connection interface has many methods (functions) for transaction management like - rollback( ), commit( ), close( ) etc.
  • 38. Connection Interface The Connection interface in java helps to establish connection with the database A Connection is the session between java application and database The Connection interface is a factory of Statement, PreparedStatement, and DatabaseMetaData - it means, object of Connection interface can be used to get the object of Statement and DatabaseMetaData The Connection interface has many methods (functions) for transaction management like - rollback( ), commit( ), close( ) etc.
  • 39. Functions of Connection Interface public Statement createStatement() - this function creates object of Statement interface which can be use to execute SQL queries public Statement createStatement(int resultSetType,int resultSetConcurrency) - creates object of Statement interface that generates object of ResultSet interface with given type and concurrency public void setAutoCommit(boolean status) - this function sets the commit status, by default it's value is true public void commit() - this function permanently saves the changes done since previous commit or rollback
  • 40. Functions of Connection Interface public void rollback( ) - drops or undo all changes done since the previous commit or rollback public void close( ) - closes the connection and releases all JDBC resources immediately
  • 41. Statement Interface The Statement interface provides methods(functions) which are used to execute SQL queries with the database Object of Statement interface can work with only static query The statement interface is a factory of ResultSet interface i.e. it provides factory method to get the object of ResultSet interface The statement interface is a factory of ResultSet interface i.e. it provides factory method to get the object of ResultSet interface
  • 42. Functions of Statement Interface public ResultSet executeQuery(String sql) - it is used to execute SELECT SQL query which retrieves (gives) data from the database. It returns the object of ResultSet interface public int executeUpdate(String sql) - this function is used to make modification in your database like create, drop, insert, update, delete etc public boolean execute(String sql) - this method is used for all type of SQL statement (eg.Select, Insert, Update etc.), it is used to execute queries that may return multiple results public int[] executeBatch(): this function is used to execute batch (group) of commands. It returns a array of integer type
  • 43. ResultSet Interface ResultSet interface represents the set of results of a database query The object of ResultSet maintains a cursor pointing to a row of a table, initially, cursor points to before the first row The methods of the ResultSet interface can be divided into three categories - Navigational methods - these methods are used to move the cursor around Get methods: used to see the data in the columns of the current row being pointed by the cursor Update methods: used to update the data in the columns of the current row By default, ResultSet object can be moved forward only and it is not updatable
  • 44. ResultSet Interface Types of Resultset - denoted by three constants as below ResultSet.TYPE_FORWARD_ONLY - The cursor can only move forward in the result set ResultSet.TYPE_SCROLL_INSENSITIVE - The cursor can scroll forward and backward and if changed are made in database after result set was created then result remains unchanged (i.e not sensitive) ResultSet.TYPE_SCROLL_SENSITIVE - The cursor can scroll forward and backward and if changed are made in database after result set was created then result remains also changes (i.e sensitive) Concurrency of ResultSet - means updatable or not ResultSet.CONCUR_READ_ONLY - creates a read-only result set, this is the default value ResultSet.CONCUR_UPDATABLE - creates an updateable result set
  • 45. ResultSet Interface Fig: - concept of ResultSet Datbase data data data data data data ResultSet object cursor
  • 46. Functions of ResultSet Interface public boolean next() - is used to move the cursor to the one row next from the current position public boolean previous() - is used to move the cursor to the one row previous from the current position public boolean first() - is used to move the cursor to the first row in result set object public boolean last() - is used to move the cursor to the last row in result set object public boolean absolute(int row) - is used to move the cursor to the specified row number in the ResultSet object public boolean relative(int row) - is used to move the cursor to the relative row number in the ResultSet object, it may be positive or negative
  • 47. Functions of ResultSet Interface public int getInt(int columnIndex) - is used to return the data of specified column index of the current row as int public int getInt(String columnName) - is used to return the data of specified column name of the current row as int public String getString(int columnIndex) - is used to return the data of specified column index of the current row as String public String getString(String columnName) - is used to return the data of specified column name of the current row as String
  • 48. PreparedStatement Interface The PreparedStatement interface is a subinterface (child) of Statement interface and it is used to execute SQL queries with parameters The PreparedStatement interface accepts input parameters at runtime If we use PreparedStament interface then performance of application will be faster because query is compiled only once PreparedStament interface is used for the queries which are to be executed multiple times We can used Preparedstatement interface for reading and writing binary data Object of PreparedStatement interface is returned by prepareStatement() method of Connection interface Performance of PreparedStatement is better than Statement interface
  • 49. Function of PreparedStatement Interface public void setInt(int paramIndex, int value) - this function sets the integer value to the given parameter index public void setString(int paramIndex, String value) - sets the String value to the given parameter index public void setFloat(int paramIndex, float value) - sets the float value to the given parameter index public void setDouble(int paramIndex, double value) - sets the double value to the given parameter index public int executeUpdate() - executes the query. It is used for create, drop, insert, update, delete etc public ResultSet executeQuery() - executes the SELECT query. It returns an instance (object) of ResultSet interface
  • 50. JDBC Environment Setup JDBC environment means what software components are require to develop a JDBC application Following components are require to develop a java database applications : Install Java - JDK 5.0 or greater versions with JDBC packages ( java.sql and javax.sql packages ) Install Database - open source databases like MySQL DB, postgreSQL or commercial database like Oracle DB Install database drivers - JDBC-ODBC bridge drivers, thin driver etc.
  • 51. Steps for creating JDBC application Open connection with database - create Connection object using DriverManager.getConnection( ) method Extract data from the result set - use functions of ResultSet interface to get data from result set Import the packages for JDBC classes - java.sql.* or javax.sql.* Register the JDBC driver - for opening communication channel with database Execute Query - create object of Statement interface Clean up the environment - close all database connections and resources
  翻译: