SlideShare a Scribd company logo
1 of 19
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

What's hot

Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Pooja Talreja
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servletsNuha Noor
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)Talha Ocakçı
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivitybackdoor
 
1. java database connectivity (jdbc)
1. java database connectivity (jdbc)1. java database connectivity (jdbc)
1. java database connectivity (jdbc)Fad Zulkifli
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivityAtul Saurabh
 
Java/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCJava/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCFAKHRUN NISHA
 
Java database connectivity with MYSQL
Java database connectivity with MYSQLJava database connectivity with MYSQL
Java database connectivity with MYSQLAdil Mehmoood
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivityVaishali Modi
 
Database connect
Database connectDatabase connect
Database connectYoga Raja
 
Java Servlet
Java ServletJava Servlet
Java ServletYoga Raja
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentationguest11106b
 

What's hot (20)

JDBC ppt
JDBC pptJDBC ppt
JDBC ppt
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)
 
JDBC in Servlets
JDBC in ServletsJDBC in Servlets
JDBC in Servlets
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
 
1. java database connectivity (jdbc)
1. java database connectivity (jdbc)1. java database connectivity (jdbc)
1. java database connectivity (jdbc)
 
java jdbc connection
java jdbc connectionjava jdbc connection
java jdbc connection
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Java/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCJava/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBC
 
Java database connectivity with MYSQL
Java database connectivity with MYSQLJava database connectivity with MYSQL
Java database connectivity with MYSQL
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Jdbc
JdbcJdbc
Jdbc
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Database connect
Database connectDatabase connect
Database connect
 
Java Servlet
Java ServletJava Servlet
Java Servlet
 
JDBC
JDBCJDBC
JDBC
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 

Viewers also liked (14)

Jdbc Ppt
Jdbc PptJdbc Ppt
Jdbc Ppt
 
프로세스 관리
프로세스 관리프로세스 관리
프로세스 관리
 
Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
Jdbc
JdbcJdbc
Jdbc
 
PULSE CODE MODULATION (PCM)
PULSE CODE MODULATION (PCM)PULSE CODE MODULATION (PCM)
PULSE CODE MODULATION (PCM)
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
 
Cultures of india
Cultures of indiaCultures of india
Cultures of india
 
India
IndiaIndia
India
 
Indian culture
Indian cultureIndian culture
Indian culture
 
INCREDIBLE INDIA
INCREDIBLE INDIAINCREDIBLE INDIA
INCREDIBLE INDIA
 
CULTURES OF INDIA
CULTURES OF INDIACULTURES OF INDIA
CULTURES OF INDIA
 
India Presentation
India PresentationIndia Presentation
India Presentation
 
Indian culture
Indian cultureIndian culture
Indian culture
 

Similar to Jdbc api

Similar to Jdbc api (20)

Jdbc[1]
Jdbc[1]Jdbc[1]
Jdbc[1]
 
JDBC programming
JDBC programmingJDBC programming
JDBC programming
 
Introduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applicationsIntroduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applications
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
 
Advance Java Practical file
Advance Java Practical fileAdvance Java Practical file
Advance Java Practical file
 
Lecture17
Lecture17Lecture17
Lecture17
 
Jdbc tutorial
Jdbc tutorialJdbc tutorial
Jdbc tutorial
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commands
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commands
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)
 
Lecture 1. java database connectivity
Lecture 1. java database connectivityLecture 1. java database connectivity
Lecture 1. java database connectivity
 
JDBC
JDBCJDBC
JDBC
 
Advance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-databaseAdvance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-database
 
Jdbc
JdbcJdbc
Jdbc
 
JDBC
JDBCJDBC
JDBC
 
Java beans
Java beansJava beans
Java beans
 
Sqlapi0.1
Sqlapi0.1Sqlapi0.1
Sqlapi0.1
 
Java 1-contd
Java 1-contdJava 1-contd
Java 1-contd
 
JDBC (2).ppt
JDBC (2).pptJDBC (2).ppt
JDBC (2).ppt
 

More from kamal kotecha

Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Examplekamal kotecha
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPTkamal kotecha
 
Java rmi example program with code
Java rmi example program with codeJava rmi example program with code
Java rmi example program with codekamal kotecha
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types pptkamal kotecha
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
String and string buffer
String and string bufferString and string buffer
String and string bufferkamal kotecha
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of javakamal kotecha
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7kamal kotecha
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operatorkamal kotecha
 

More from kamal kotecha (19)

Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
Java rmi example program with code
Java rmi example program with codeJava rmi example program with code
Java rmi example program with code
 
Java rmi
Java rmiJava rmi
Java rmi
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
JSP Error handling
JSP Error handlingJSP Error handling
JSP Error handling
 
Jsp element
Jsp elementJsp element
Jsp element
 
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
Interface
InterfaceInterface
Interface
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
Class method
Class methodClass method
Class method
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Control statements
Control statementsControl statements
Control statements
 
Jsp myeclipse
Jsp myeclipseJsp myeclipse
Jsp myeclipse
 
basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operator
 

Recently uploaded

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 

Recently uploaded (20)

Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

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