KEMBAR78
Jdbc api | PPS
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

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 javaapplication 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 aregoing 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 interfaceused 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 programfor 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 interfaceis 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 programfor Statement interface and its methods are given in next chapter for different databases. http://www.java2all.com
  • 15.
    The Prepared StatementInterface: http://www.java2all.com
  • 16.
    The Prepared Statementinterface 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 meansa 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 programfor Prepared Statement interface and its methods are given in next chapter for different databases. http://www.java2all.com