SlideShare a Scribd company logo
JDBC
By
Sharmilee
9894303344
Java Trainer
Mazenet Solution
Objective
• Introduction to JDBC
• JDBC Drivers
• Steps to connect database
• ResultSet
• Statement & PreparedStatement
• Transaction Management
• Batch Processing
Introduction
• Java JDBC is a java API to connect and
execute query with the database.
• JDBC API uses jdbc drivers to connect with
the database.
Java- JDBC- Mazenet Solution
Why we use JDBC?
• Before JDBC, ODBC API is used
• ODBC API uses ODBC driver
• ODBC Driver written in C language
– platform dependent and
– unsecured.
• That is why Java has defined its own API
(JDBC API) that uses JDBC drivers (written
in Java language).
API
• API (Application programming interface) is
a document that contains description of all
the features of a product or software.
• It represents classes and interfaces that
software programs can follow to
communicate with each other.
• An API can be created for applications,
libraries, operating systems, etc
JDBC Drivers
JDBC Drivers
• JDBC Driver is a software component
• enables java application to interact with the
database
Types of JDBC Drivers
1. JDBC-ODBC bridge driver
2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (fully java driver)
1. JDBC-ODBC bridge Driver
• This uses ODBC driver to connect to the
database.
• This driver converts JDBC method calls into
the ODBC function calls.
Java- JDBC- Mazenet Solution
Advantages
•easy to use.
•can be easily connected to any database.
Disadvantages
•Performance degraded because JDBC
method call is converted into the ODBC
function calls.
•The ODBC driver needs to be installed on
the client machine.
2. Native – API Driver
• It uses the client-side libraries of the
database.
• It converts JDBC method calls into native
calls of the database API.
• It is not written entirely in java.
Java- JDBC- Mazenet Solution
Advantages
• performance upgraded than
JDBC-ODBC bridge driver.
Dis-advantages
•The Native driver needs to be installed on
the each client machine.
•The Vendor client library needs to be
installed on client machine.
3. Network Protocol driver
• It uses middleware (application server) that
converts JDBC calls directly or indirectly
into the vendor-specific database protocol.
• It is fully written in java.
Java- JDBC- Mazenet Solution
Advantages
No client side library is required
because of application server that can
perform many tasks like auditing,
load balancing, logging etc.
Dis-advantages
• 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.
4. Thin Layer
• The thin driver converts JDBC calls directly
into the vendor-specific database protocol.
• It is fully written in Java language.
Java- JDBC- Mazenet Solution
Advantages
•Better performance than all other
drivers.
•No software is required at client
side or server side.
Dis-advantages
• Drivers depends on the Database.
Steps to connect oracle Database
5 Steps to connect to the database in
java
• Register the driver class
• Creating connection
• Creating statement
• Executing queries
• Closing connection
1. Register the driver class
• The forName() method of Class class is
used to register the driver class.
• This method is used to dynamically load the
driver class.
Syntax of forName() method
public static void forName(String className)throws ClassNotFoundEx
ception
Example to register the OracleDriver class
Class.forName("oracle.jdbc.driver.OracleDriver");
2. Create the connection object
• The getConnection() method of
DriverManager class is used to establish
connection with the database.
Syntax of getConnection() method
1) public static Connection getConnection(String url)throws SQLException
2) public static Connection getConnection(String url,String name,String pas
sword)
throws SQLException
Example to establish connection with the Oracle database
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","password");
3) Create the Statement object
• The createStatement() method of
Connection interface is used to create
statement.
• The object of statement is responsible to
execute queries with the database.
Syntax of createStatement() method
public Statement createStatement()throws SQLException
Example to create the statement object
Statement stmt=con.createStatement();
4. Execute the query
• It is used to execute queries to the database.
• This method returns the object of ResultSet
that can be used to get all the records of a
table.
Syntax of executeQuery() method
public ResultSet executeQuery(String sql)throws SQLException
Example to execute query
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2)); }
5. Close the connection object
• By closing connection object statement and
ResultSet will be closed automatically.
• The close() method of Connection interface
is used to close the connection.
Syntax of close() method
public void close()throws SQLException
Example to close connection
con.close();
DriverManager Class
• It acts as an interface between user and
drivers.
• It keeps track of the drivers that are
available and handles establishing a
connection between a database and the
appropriate driver.
Connection interface
• A Connection is the session between java
application and database.
• The Connection interface provide many
methods for transaction management like
commit(),rollback() etc.
Note: By default, connection commits the
changes after executing queries.
Statement Interface
• The Statement interface provides
methods to execute queries with the
database.
• It provides factory method to get the object
of ResultSet.
ResultSet Interface
• The object of ResultSet maintains a cursor
pointing to a particular row of data.
• Initially, cursor points to before the first
row.
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENS
ITIVE,
ResultSet.CONCUR_UPDATABLE);
PreparedStatement Interface
• The PreparedStatement interface is a
subinterface of Statement.
• It is used to execute parameterized query
String sql="insert into emp values(?,?,?)";
Why we use PreparedStatement?
• Improves performance: The performance
of the application will be faster if you use
PreparedStatement interface because query
is compiled only once.
ResultSetMetaData Interface
• The metadata means data about data i.e. we
can get further information from the data.
• Metadata of a table
– total number of column,
– column name,
– column type etc. ,
• ResultSetMetaData interface is useful
because it provides methods to get
metadata from the ResultSet object.
Transaction Management
Transaction Management
• Transaction represents a single unit of
work.
• The ACID properties describes the
transaction management well.
• ACID stands for
Atomicity,
Consistency,
isolation and
durability.
Transaction Management
• Atomicity means either all successful or none.
• Consistency ensures bringing the database
from one consistent state to another consistent
state.
• Isolation ensures that transaction is isolated
from other transaction.
• Durability means once a transaction has been
committed, it will remain so, even in the event
of errors, power loss etc.
Advantage of Transaction
management
• Fast performance - It makes the
performance fast because database is hit at
the time of commit.
Java- JDBC- Mazenet Solution
Batch Processing
• Instead of executing a single query, we can
execute a batch (group) of queries.
• It makes the performance fast.
• java.sql.Statement &
java.sql.PreparedStatement interfaces
provide methods for batch processing.
Example for Batch Processing
• Load the driver class
• Create Connection
• Create Statement
• Add query in the batch
• Execute Batch
• Close Connection
Thank You!

More Related Content

PPTX
JDBC ppt
PPTX
PPT
JDBC – Java Database Connectivity
PPTX
Java Beans
PPT
9. Object Relational Databases in DBMS
PPT
Java Networking
PPTX
enterprise java bean
JDBC ppt
JDBC – Java Database Connectivity
Java Beans
9. Object Relational Databases in DBMS
Java Networking
enterprise java bean

What's hot (20)

PPTX
Direct memory access (dma)
PPTX
Jdbc ppt
PPT
Inter process communication
PPTX
Types of Drivers in JDBC
PPTX
Spring boot Introduction
PPT
C# Exceptions Handling
PPTX
Servlets
PDF
Introduction to java (revised)
PPTX
Java database connectivity with MySql
PPTX
Java Server Pages(jsp)
PDF
Oracle Database performance tuning using oratop
PPTX
Advance Java Topics (J2EE)
PPT
Java database connectivity
PPT
MYSQL.ppt
PPT
JDBC Architecture and Drivers
PPTX
Event Handling in java
PPTX
JAVA ENVIRONMENT
PPS
Jdbc architecture and driver types ppt
Direct memory access (dma)
Jdbc ppt
Inter process communication
Types of Drivers in JDBC
Spring boot Introduction
C# Exceptions Handling
Servlets
Introduction to java (revised)
Java database connectivity with MySql
Java Server Pages(jsp)
Oracle Database performance tuning using oratop
Advance Java Topics (J2EE)
Java database connectivity
MYSQL.ppt
JDBC Architecture and Drivers
Event Handling in java
JAVA ENVIRONMENT
Jdbc architecture and driver types ppt
Ad

Viewers also liked (11)

PPTX
CGA Millennials in the Workforce
PDF
The 99.999 percent cybersecurity problem
PPTX
Medición del radio terrestre Proyecto Eratóstenes
PPTX
The Live Online Class
PPTX
Nassi Shneiderman Diagrams: Algorithms Made Easier (at least a little bit)
PPT
Basics of Html
DOC
Abdel Hamied Lotfy cv
PDF
Praca.pl sytuacja na rynku pracy w I kw.2016 raport
PDF
Guide to Furnace Sootblowing
PPTX
CGA Millennials in the Workforce
The 99.999 percent cybersecurity problem
Medición del radio terrestre Proyecto Eratóstenes
The Live Online Class
Nassi Shneiderman Diagrams: Algorithms Made Easier (at least a little bit)
Basics of Html
Abdel Hamied Lotfy cv
Praca.pl sytuacja na rynku pracy w I kw.2016 raport
Guide to Furnace Sootblowing
Ad

Similar to Java- JDBC- Mazenet Solution (20)

PPTX
Core jdbc basics
PPT
Basic Java Database Connectivity(JDBC)
PPTX
Jdjdbcbc Jdjdbcbc JdjdbcJdjdbcbc Jdjdbcbc Jdjdbcbcbc JdJdbcbc
PDF
PPTX
Jdbc introduction
PDF
Unit 5.pdf
PDF
JDBC : Java Database Connectivity
PPT
PPT
Chap3 3 12
PPT
JDBC.ppt
PPTX
Java Database Connectivity by shreyash simu dbce.pptx
PPTX
Jdbc presentation
PPTX
PPT
Java database connectivity
PPTX
java database connectivity for java programming
PDF
Presentation for java data base connectivity
PPT
Jdbc connectivity
PPTX
Database connect
PPTX
jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx
Core jdbc basics
Basic Java Database Connectivity(JDBC)
Jdjdbcbc Jdjdbcbc JdjdbcJdjdbcbc Jdjdbcbc Jdjdbcbcbc JdJdbcbc
Jdbc introduction
Unit 5.pdf
JDBC : Java Database Connectivity
Chap3 3 12
JDBC.ppt
Java Database Connectivity by shreyash simu dbce.pptx
Jdbc presentation
Java database connectivity
java database connectivity for java programming
Presentation for java data base connectivity
Jdbc connectivity
Database connect
jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx

More from Mazenetsolution (20)

PPTX
Tally Auto E-mail Module | Mazenet Technologies
PPTX
Tally Auto SMS Module| Mazenet Technologies
PPTX
Tally auto synchronization
PPTX
Print barcode using voucher- Mazenettechnologies
PPTX
Copy user list | Tally | Tally Software | Accounting Software | Mazenet
PPTX
Auto synchronization | Tally Software | Mazenet Technologies
PPTX
Auto backup | Tally Coimbatore | Tally Software
PPTX
Mazenet Technologies-Tally
PPTX
Android - Intents - Mazenet Solution
PPT
Java - Servlet - Mazenet Solution
PPT
Software Testing - Tool support for testing (CAST) - Mazenet Solution
PPT
Software Testing - Test management - Mazenet Solution
PPTX
Red Hat - LVM - Mazenet Solution
PPT
PHP - Introduction to PHP - Mazenet Solution
PPT
Static testing techniques
PPTX
Java- GUI- Mazenet solution
PPT
Oracle- Introduction to Sql commands- Mazenet solution
PPTX
Process management in linux
PPT
Software Testing- Principles of testing- Mazenet Solution
PPT
Software Testing-Dynamic testing technique-Mazenet solution
Tally Auto E-mail Module | Mazenet Technologies
Tally Auto SMS Module| Mazenet Technologies
Tally auto synchronization
Print barcode using voucher- Mazenettechnologies
Copy user list | Tally | Tally Software | Accounting Software | Mazenet
Auto synchronization | Tally Software | Mazenet Technologies
Auto backup | Tally Coimbatore | Tally Software
Mazenet Technologies-Tally
Android - Intents - Mazenet Solution
Java - Servlet - Mazenet Solution
Software Testing - Tool support for testing (CAST) - Mazenet Solution
Software Testing - Test management - Mazenet Solution
Red Hat - LVM - Mazenet Solution
PHP - Introduction to PHP - Mazenet Solution
Static testing techniques
Java- GUI- Mazenet solution
Oracle- Introduction to Sql commands- Mazenet solution
Process management in linux
Software Testing- Principles of testing- Mazenet Solution
Software Testing-Dynamic testing technique-Mazenet solution

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Classroom Observation Tools for Teachers
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
RMMM.pdf make it easy to upload and study
Anesthesia in Laparoscopic Surgery in India
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Week 4 Term 3 Study Techniques revisited.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Classroom Observation Tools for Teachers
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
TR - Agricultural Crops Production NC III.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Cell Types and Its function , kingdom of life
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Pre independence Education in Inndia.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pharma ospi slides which help in ospi learning
2.FourierTransform-ShortQuestionswithAnswers.pdf
Renaissance Architecture: A Journey from Faith to Humanism

Java- JDBC- Mazenet Solution

  • 2. Objective • Introduction to JDBC • JDBC Drivers • Steps to connect database • ResultSet • Statement & PreparedStatement • Transaction Management • Batch Processing
  • 3. Introduction • Java JDBC is a java API to connect and execute query with the database. • JDBC API uses jdbc drivers to connect with the database.
  • 5. Why we use JDBC? • Before JDBC, ODBC API is used • ODBC API uses ODBC driver • ODBC Driver written in C language – platform dependent and – unsecured. • That is why Java has defined its own API (JDBC API) that uses JDBC drivers (written in Java language).
  • 6. API • API (Application programming interface) is a document that contains description of all the features of a product or software. • It represents classes and interfaces that software programs can follow to communicate with each other. • An API can be created for applications, libraries, operating systems, etc
  • 8. JDBC Drivers • JDBC Driver is a software component • enables java application to interact with the database
  • 9. Types of JDBC Drivers 1. JDBC-ODBC bridge driver 2. Native-API driver (partially java driver) 3. Network Protocol driver (fully java driver) 4. Thin driver (fully java driver)
  • 10. 1. JDBC-ODBC bridge Driver • This uses ODBC driver to connect to the database. • This driver converts JDBC method calls into the ODBC function calls.
  • 12. Advantages •easy to use. •can be easily connected to any database. Disadvantages •Performance degraded because JDBC method call is converted into the ODBC function calls. •The ODBC driver needs to be installed on the client machine.
  • 13. 2. Native – API Driver • It uses the client-side libraries of the database. • It converts JDBC method calls into native calls of the database API. • It is not written entirely in java.
  • 15. Advantages • performance upgraded than JDBC-ODBC bridge driver. Dis-advantages •The Native driver needs to be installed on the each client machine. •The Vendor client library needs to be installed on client machine.
  • 16. 3. Network Protocol driver • It uses middleware (application server) that converts JDBC calls directly or indirectly into the vendor-specific database protocol. • It is fully written in java.
  • 18. Advantages No client side library is required because of application server that can perform many tasks like auditing, load balancing, logging etc. Dis-advantages • 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.
  • 19. 4. Thin Layer • The thin driver converts JDBC calls directly into the vendor-specific database protocol. • It is fully written in Java language.
  • 21. Advantages •Better performance than all other drivers. •No software is required at client side or server side. Dis-advantages • Drivers depends on the Database.
  • 22. Steps to connect oracle Database
  • 23. 5 Steps to connect to the database in java • Register the driver class • Creating connection • Creating statement • Executing queries • Closing connection
  • 24. 1. Register the driver class • The forName() method of Class class is used to register the driver class. • This method is used to dynamically load the driver class. Syntax of forName() method public static void forName(String className)throws ClassNotFoundEx ception Example to register the OracleDriver class Class.forName("oracle.jdbc.driver.OracleDriver");
  • 25. 2. Create the connection object • The getConnection() method of DriverManager class is used to establish connection with the database. Syntax of getConnection() method 1) public static Connection getConnection(String url)throws SQLException 2) public static Connection getConnection(String url,String name,String pas sword) throws SQLException Example to establish connection with the Oracle database Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","system","password");
  • 26. 3) Create the Statement object • The createStatement() method of Connection interface is used to create statement. • The object of statement is responsible to execute queries with the database. Syntax of createStatement() method public Statement createStatement()throws SQLException Example to create the statement object Statement stmt=con.createStatement();
  • 27. 4. Execute the query • It is used to execute queries to the database. • This method returns the object of ResultSet that can be used to get all the records of a table. Syntax of executeQuery() method public ResultSet executeQuery(String sql)throws SQLException Example to execute query ResultSet rs=stmt.executeQuery("select * from emp"); while(rs.next()){ System.out.println(rs.getInt(1)+" "+rs.getString(2)); }
  • 28. 5. Close the connection object • By closing connection object statement and ResultSet will be closed automatically. • The close() method of Connection interface is used to close the connection. Syntax of close() method public void close()throws SQLException Example to close connection con.close();
  • 29. DriverManager Class • It acts as an interface between user and drivers. • It keeps track of the drivers that are available and handles establishing a connection between a database and the appropriate driver.
  • 30. Connection interface • A Connection is the session between java application and database. • The Connection interface provide many methods for transaction management like commit(),rollback() etc. Note: By default, connection commits the changes after executing queries.
  • 31. Statement Interface • The Statement interface provides methods to execute queries with the database. • It provides factory method to get the object of ResultSet.
  • 32. ResultSet Interface • The object of ResultSet maintains a cursor pointing to a particular row of data. • Initially, cursor points to before the first row. Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENS ITIVE, ResultSet.CONCUR_UPDATABLE);
  • 33. PreparedStatement Interface • The PreparedStatement interface is a subinterface of Statement. • It is used to execute parameterized query String sql="insert into emp values(?,?,?)";
  • 34. Why we use PreparedStatement? • Improves performance: The performance of the application will be faster if you use PreparedStatement interface because query is compiled only once.
  • 35. ResultSetMetaData Interface • The metadata means data about data i.e. we can get further information from the data. • Metadata of a table – total number of column, – column name, – column type etc. , • ResultSetMetaData interface is useful because it provides methods to get metadata from the ResultSet object.
  • 37. Transaction Management • Transaction represents a single unit of work. • The ACID properties describes the transaction management well. • ACID stands for Atomicity, Consistency, isolation and durability.
  • 38. Transaction Management • Atomicity means either all successful or none. • Consistency ensures bringing the database from one consistent state to another consistent state. • Isolation ensures that transaction is isolated from other transaction. • Durability means once a transaction has been committed, it will remain so, even in the event of errors, power loss etc.
  • 39. Advantage of Transaction management • Fast performance - It makes the performance fast because database is hit at the time of commit.
  • 42. • Instead of executing a single query, we can execute a batch (group) of queries. • It makes the performance fast. • java.sql.Statement & java.sql.PreparedStatement interfaces provide methods for batch processing.
  • 43. Example for Batch Processing • Load the driver class • Create Connection • Create Statement • Add query in the batch • Execute Batch • Close Connection