SlideShare a Scribd company logo
JDBC APIS




            http://www.java2all.com
Chapter 2



JDBC APIs:



             http://www.java2all.com
JDBC APIs:



             http://www.java2all.com
If any java application or an applet wants to
connect with a database then there are various
classes and interfaces available in java.sql package.

   Depending on the requirements these classes
and interfaces can be used.

   Some of them are list out the below which are
used to perform the various tasks with database as
well as for connection.


                                           http://www.java2all.com
Class or Interface              Description


Java.sql.Connection           Create a connection with specific database


                               The task of DriverManager is to manage the database
Java.sql.DriverManager
                             driver


                               It executes SQL statements for particular connection and
Java.sql.Statement
                             retrieve the results


                               It allows the programmer to create prepared SQL
Java.sql.PreparedStatement
                             statements


Java.sql.CallableStatement    It executes stored procedures


                               This interface provides methods to get result row by row
Java.sql.ResultSet
                             generated by SELECT statements
                                                                       http://www.java2all.com
Now we are going to elobrate each class or
interface in detail with their methods and will give
program for each one in next topic.




                                              http://www.java2all.com
The Connection interface:




                        http://www.java2all.com
The Connection interface used to connect java
application with particular database.

      After crating the connection with database we
can execute SQL statements for that particular
connection using object of Connection and retrieve the
results.

     The interface has few methods that makes
changes to the database temporary or permanently.
The some methods are as given below.


                                            http://www.java2all.com
Method                                      Description
                                          This method frees an object of type Connection from
void close()
                                          database and other JDBC resources.

                                          This method makes all the changes made since the last
void commit()                             commit or rollback permanent. It throws
                                          SQLExeception.
                                          This method creates an object of type Statement for
Statement createStatement()               sending SQL statements to the database. It throws
                                          SQLExeception.

boolean isClosed()                        Return true if the connection is close else return false.

                                          This method creates an object of type
CallableStatement prepareCall(String s)   CallableStatement for calling the stored procedures
                                          from database. It throws SQLExeception.
                                          This method creates an object of type
PreparedStatement                         PrepareStatement for sending dynamic (with or without
prepareStatement(String s)                IN parameter) SQL statements to the database. It
                                          throws SQLExeception.

void rollback()                           This method undoes all changes made to the database.
                                                                                http://www.java2all.com
The example program for Connection interface
and its methods are given in next chapter for different
databases.




                                             http://www.java2all.com
Statement Interface:



                   http://www.java2all.com
The Statement interface is used for to
execute a static query.

    It’s a very simple and easy so it also calls a
“Simple Statement”.

      The statement interface has several methods
for execute the SQL statements and also get the
appropriate result as per the query sent to the
database.

     Some of the most common methods are as
given below
                                              http://www.java2all.com
Method                                    Description

                              This method frees an object of type Statement from
void close()
                              database and other JDBC resources.

                              This method executes the SQL statement specified by s.
boolean execute(String s)
                              The getResultSet() method is used to retrieve the result.

                              This method retrieves the ResultSet that is generated by
ResultSet getResultet()
                              the execute() method.

ResultSet                     This method is used to execute the SQL statement
executeQuery(String s)        specified by s and returns the object of type ResultSet.

                              This method returns the maximum number of rows those
int getMaxRows()
                              are generated by the executeQuery() method.

                              This method executes the SQL statement specified by s.
Int executeUpdate(String s)   The SQL statement may be a SQL insert, update and
                              delete statement.
                                                                        http://www.java2all.com
The example program for Statement interface
and its methods are given in next chapter for different
databases.




                                             http://www.java2all.com
The Prepared Statement Interface:




                            http://www.java2all.com
The Prepared Statement interface is used to
execute a dynamic query (parameterized SQL
statement) with IN parameter.

      IN Parameter:-

            In some situation where we need to pass
different values to an query then such values can be
specified as a “?” in the query and the actual values
can be passed using the setXXX() method at the time
of execution.

Syntax :
setXXX(integer data ,XXX value);            http://www.java2all.com
Where XXX means a data type as per the value
we want to pass in the query.
For example,
String query = "Select * from Data where ID = ? and
Name = ? ";
PreparedStatement ps = con.prepareStatement(query);
         ps.setInt(1, 1);
         ps.setString(2, "Ashutosh Abhangi");
      The Prepared statement interface has several
methods to execute the parameterized SQL statements
and retrieve appropriate result as per the query sent to
the database.
      Some of the most common methods are as given
below                                          http://www.java2all.com
Method                                    Description
                                  This method frees an object of type Prepared Statement from
void close()
                                  database and other JDBC resources.

                                  This method executes the dynamic query in the object of type
boolean execute()                 Prepared Statement.The getResult() method is used to
                                  retrieve the result.
                                  This method is used to execute the dynamic query in the
ResultSet executeQuery()          object of type Prepared Statement and returns the object of
                                  type ResultSet.

                                  This method executes the SQL statement in the object of type
Int executeUpdate()               Prepared Statement. The SQL statement may be a SQL
                                  insert, update and delete statement.

                                The ResultSetMetaData means a deta about the data of
                                ResultSet.This method retrieves an object of type
ResultSetMetaData getMetaData() ResultSetMetaData that contains information about the
                                columns of the ResultSet object that will be return when a
                                query is execute.

                                  This method returns the maximum number of rows those are
int getMaxRows()
                                  generated by the executeQuery() method.
                                                                             http://www.java2all.com
The example program for Prepared Statement
interface and its methods are given in next chapter for
different databases.




                                             http://www.java2all.com

More Related Content

PPT
Jdbc ppt
PPTX
DataBase Connectivity
PPT
JDBC Java Database Connectivity
PPT
JDBC – Java Database Connectivity
PPS
Jdbc example program with access and MySql
PPT
JDBC Tutorial
PPT
Data Access with JDBC
PPTX
Database Access With JDBC
Jdbc ppt
DataBase Connectivity
JDBC Java Database Connectivity
JDBC – Java Database Connectivity
Jdbc example program with access and MySql
JDBC Tutorial
Data Access with JDBC
Database Access With JDBC

What's hot (20)

PPTX
JDBC ppt
PPTX
Java Database Connectivity (JDBC)
PPTX
Jdbc in servlets
PDF
JAVA EE DEVELOPMENT (JSP and Servlets)
PDF
JDBC in Servlets
PPT
Java Database Connectivity
PPTX
1. java database connectivity (jdbc)
PPT
java jdbc connection
PPTX
Java database connectivity
PPTX
Java/Servlet/JSP/JDBC
PPT
Java database connectivity with MYSQL
PPT
Java database connectivity
PPT
Java Server Pages
PPTX
Database connect
PPTX
Java Servlet
PPT
PPT
Jsp ppt
PDF
Hibernate Presentation
JDBC ppt
Java Database Connectivity (JDBC)
Jdbc in servlets
JAVA EE DEVELOPMENT (JSP and Servlets)
JDBC in Servlets
Java Database Connectivity
1. java database connectivity (jdbc)
java jdbc connection
Java database connectivity
Java/Servlet/JSP/JDBC
Java database connectivity with MYSQL
Java database connectivity
Java Server Pages
Database connect
Java Servlet
Jsp ppt
Hibernate Presentation
Ad

Viewers also liked (14)

PPTX
프로세스 관리
PPTX
Java DataBase Connectivity API (JDBC API)
PPTX
3 jdbc api
PPTX
PPT
PULSE CODE MODULATION (PCM)
PPT
Java servlet life cycle - methods ppt
PPTX
Cultures of india
PPT
PPSX
Indian culture
PPT
INCREDIBLE INDIA
PPSX
CULTURES OF INDIA
PPTX
India Presentation
PPTX
Indian culture
프로세스 관리
Java DataBase Connectivity API (JDBC API)
3 jdbc api
PULSE CODE MODULATION (PCM)
Java servlet life cycle - methods ppt
Cultures of india
Indian culture
INCREDIBLE INDIA
CULTURES OF INDIA
India Presentation
Indian culture
Ad

Similar to Jdbc api (20)

PPT
Executing Sql Commands
PPT
Executing Sql Commands
PDF
java4th.pdf bilgisayar mühendisliği bölümü
PDF
Java 1-contd
PDF
Lecture17
PPS
Jdbc session02
PDF
Presentation for java data base connectivity
PPTX
PDF
Jdbc 1
PDF
Core Java Programming Language (JSE) : Chapter XIII - JDBC
PPTX
Jdbc presentation
PPTX
preparecallablepptx__2023_09_11_14_40_58pptx__2024_09_23_11_14_59.pptx
PPT
statement interface
PPTX
Jdbc Java Programming
PDF
PPT
JDBC for CSQL Database
PPTX
Jdbc ppt
PPT
Jdbc
PPT
Jdbc oracle
Executing Sql Commands
Executing Sql Commands
java4th.pdf bilgisayar mühendisliği bölümü
Java 1-contd
Lecture17
Jdbc session02
Presentation for java data base connectivity
Jdbc 1
Core Java Programming Language (JSE) : Chapter XIII - JDBC
Jdbc presentation
preparecallablepptx__2023_09_11_14_40_58pptx__2024_09_23_11_14_59.pptx
statement interface
Jdbc Java Programming
JDBC for CSQL Database
Jdbc ppt
Jdbc
Jdbc oracle

More from kamal kotecha (19)

PPS
Java Hibernate Programming with Architecture Diagram and Example
PPTX
Network programming in java - PPT
PPS
Java rmi example program with code
PPS
Java rmi
PPS
Jdbc architecture and driver types ppt
PPS
Java Exception handling
PPS
JSP Error handling
PPS
Jsp element
PPS
Jsp chapter 1
PPS
String and string buffer
PPS
Wrapper class
PPS
Packages and inbuilt classes of java
PPS
Interface
PPS
Inheritance chepter 7
PPS
Class method
PPS
Introduction to class in java
PPS
Control statements
PPTX
Jsp myeclipse
PPTX
basic core java up to operator
Java Hibernate Programming with Architecture Diagram and Example
Network programming in java - PPT
Java rmi example program with code
Java rmi
Jdbc architecture and driver types ppt
Java Exception handling
JSP Error handling
Jsp element
Jsp chapter 1
String and string buffer
Wrapper class
Packages and inbuilt classes of java
Interface
Inheritance chepter 7
Class method
Introduction to class in java
Control statements
Jsp myeclipse
basic core java up to operator

Recently uploaded (20)

PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Insiders guide to clinical Medicine.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PDF
From loneliness to social connection charting
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Open folder Downloads.pdf yes yes ges yes
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Introduction and Scope of Bichemistry.pptx
PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Pre independence Education in Inndia.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Cell Structure & Organelles in detailed.
Insiders guide to clinical Medicine.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
From loneliness to social connection charting
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Open folder Downloads.pdf yes yes ges yes
Abdominal Access Techniques with Prof. Dr. R K Mishra
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Introduction and Scope of Bichemistry.pptx
Revamp in MTO Odoo 18 Inventory - Odoo Slides
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Renaissance Architecture: A Journey from Faith to Humanism
Pre independence Education in Inndia.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
The Final Stretch: How to Release a Game and Not Die in the Process.
human mycosis Human fungal infections are called human mycosis..pptx

Jdbc api

  • 1. JDBC APIS http://www.java2all.com
  • 2. Chapter 2 JDBC APIs: http://www.java2all.com
  • 3. JDBC APIs: http://www.java2all.com
  • 4. If any java application or an applet wants to connect with a database then there are various classes and interfaces available in java.sql package. Depending on the requirements these classes and interfaces can be used. Some of them are list out the below which are used to perform the various tasks with database as well as for connection. http://www.java2all.com
  • 5. Class or Interface Description Java.sql.Connection Create a connection with specific database The task of DriverManager is to manage the database Java.sql.DriverManager driver It executes SQL statements for particular connection and Java.sql.Statement retrieve the results It allows the programmer to create prepared SQL Java.sql.PreparedStatement statements Java.sql.CallableStatement It executes stored procedures This interface provides methods to get result row by row Java.sql.ResultSet generated by SELECT statements http://www.java2all.com
  • 6. Now we are going to elobrate each class or interface in detail with their methods and will give program for each one in next topic. http://www.java2all.com
  • 7. The Connection interface: http://www.java2all.com
  • 8. The Connection interface used to connect java application with particular database. After crating the connection with database we can execute SQL statements for that particular connection using object of Connection and retrieve the results. The interface has few methods that makes changes to the database temporary or permanently. The some methods are as given below. http://www.java2all.com
  • 9. Method Description This method frees an object of type Connection from void close() database and other JDBC resources. This method makes all the changes made since the last void commit() commit or rollback permanent. It throws SQLExeception. This method creates an object of type Statement for Statement createStatement() sending SQL statements to the database. It throws SQLExeception. boolean isClosed() Return true if the connection is close else return false. This method creates an object of type CallableStatement prepareCall(String s) CallableStatement for calling the stored procedures from database. It throws SQLExeception. This method creates an object of type PreparedStatement PrepareStatement for sending dynamic (with or without prepareStatement(String s) IN parameter) SQL statements to the database. It throws SQLExeception. void rollback() This method undoes all changes made to the database. http://www.java2all.com
  • 10. The example program for Connection interface and its methods are given in next chapter for different databases. http://www.java2all.com
  • 11. Statement Interface: http://www.java2all.com
  • 12. The Statement interface is used for to execute a static query. It’s a very simple and easy so it also calls a “Simple Statement”. The statement interface has several methods for execute the SQL statements and also get the appropriate result as per the query sent to the database. Some of the most common methods are as given below http://www.java2all.com
  • 13. Method Description This method frees an object of type Statement from void close() database and other JDBC resources. This method executes the SQL statement specified by s. boolean execute(String s) The getResultSet() method is used to retrieve the result. This method retrieves the ResultSet that is generated by ResultSet getResultet() the execute() method. ResultSet This method is used to execute the SQL statement executeQuery(String s) specified by s and returns the object of type ResultSet. This method returns the maximum number of rows those int getMaxRows() are generated by the executeQuery() method. This method executes the SQL statement specified by s. Int executeUpdate(String s) The SQL statement may be a SQL insert, update and delete statement. http://www.java2all.com
  • 14. The example program for Statement interface and its methods are given in next chapter for different databases. http://www.java2all.com
  • 15. The Prepared Statement Interface: http://www.java2all.com
  • 16. The Prepared Statement interface is used to execute a dynamic query (parameterized SQL statement) with IN parameter. IN Parameter:- In some situation where we need to pass different values to an query then such values can be specified as a “?” in the query and the actual values can be passed using the setXXX() method at the time of execution. Syntax : setXXX(integer data ,XXX value); http://www.java2all.com
  • 17. Where XXX means a data type as per the value we want to pass in the query. For example, String query = "Select * from Data where ID = ? and Name = ? "; PreparedStatement ps = con.prepareStatement(query); ps.setInt(1, 1); ps.setString(2, "Ashutosh Abhangi"); The Prepared statement interface has several methods to execute the parameterized SQL statements and retrieve appropriate result as per the query sent to the database. Some of the most common methods are as given below http://www.java2all.com
  • 18. Method Description This method frees an object of type Prepared Statement from void close() database and other JDBC resources. This method executes the dynamic query in the object of type boolean execute() Prepared Statement.The getResult() method is used to retrieve the result. This method is used to execute the dynamic query in the ResultSet executeQuery() object of type Prepared Statement and returns the object of type ResultSet. This method executes the SQL statement in the object of type Int executeUpdate() Prepared Statement. The SQL statement may be a SQL insert, update and delete statement. The ResultSetMetaData means a deta about the data of ResultSet.This method retrieves an object of type ResultSetMetaData getMetaData() ResultSetMetaData that contains information about the columns of the ResultSet object that will be return when a query is execute. This method returns the maximum number of rows those are int getMaxRows() generated by the executeQuery() method. http://www.java2all.com
  • 19. The example program for Prepared Statement interface and its methods are given in next chapter for different databases. http://www.java2all.com