0% found this document useful (0 votes)
24 views62 pages

Module 5

The document discusses different types of JDBC drivers: 1. Type 1 JDBC-ODBC bridge driver which translates between JDBC and ODBC but has poor performance. 2. Type 2 native API driver which uses native database client libraries for better performance but less portability. 3. Type 3 network protocol driver which is a pure Java driver that communicates directly with the database using network protocols.

Uploaded by

eng21am0096
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views62 pages

Module 5

The document discusses different types of JDBC drivers: 1. Type 1 JDBC-ODBC bridge driver which translates between JDBC and ODBC but has poor performance. 2. Type 2 native API driver which uses native database client libraries for better performance but less portability. 3. Type 3 network protocol driver which is a pure Java driver that communicates directly with the database using network protocols.

Uploaded by

eng21am0096
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 62

Module 5- Database

Access
Dept.of CSE DSU
SYLLABUS

MODULE 5: 8
hrs

Database Access: The Concept of JDBC; JDBC Driver Types; JDBC Packages; A Brief Overview of the JDBC
process; Database Connection; Associating the JDBC/ODBC Bridge with the Database; Statement Objects;
Result Set.

Dept of CSE, DSU.


Introduction to JDBC

▪ JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and
execute the query with the database. It is a part of JavaSE (Java Standard Edition).
JDBC API uses JDBC drivers to connect with the database.
▪ J2EE(Java 2 Platform Enterprise Edition) is a platform-independent, Java-centric
environment from Sun/Oracle for developing, building and deploying Web-based
enterprise applications online. The J2EE platform consists of a set of services, APIs,
and protocols that provide the functionality for developing multi-tiered, Web-based
applications.
▪ Open Database Connectivity (ODBC) is an open standard Application Programming
Interface (API) for accessing a database

Dept.of CSE DSU 3


Introduction to JDBC(cont.)
▪ Open a connection between the DBMS and the J2EE component.
▪ Translate low-level equivalents of SQL statements sent by the J2EE
component in to messages that can be processed by the DBMS.
▪ Return data that conforms to the JDBC specification to the JDBC driver.
▪ Return information such as error messages that conforms to the JDBC
specification to the JDBC driver.
▪ Provide transaction management routines that conform to the JDBC
specification.
▪ Close the connection between the DBMS and the J2EE component.

Dept.of CSE DSU


Introduction to JDBC(cont.)
JDBC Driver is a software component that enables java application to interact with
the database. There are 4 types of JDBC drivers:
▪ JDBC-ODBC bridge driver
▪ Native-API driver (partially java driver)
▪ Network Protocol driver (fully java driver/Middleware driver)
▪ Thin driver (fully java driver/pure java driver)

Dept.of CSE DSU


THE CONCEPT OF JDBC
2.JDBC DRIVER
▪ The JDBC API defines the Java interfaces and classes that programmers use
to connect to databases and send queries. A JDBC driver implements these
interfaces and classes for a particular DBMS vendor.
▪ A Java program that uses the JDBC API loads the specified driver for a
particular DBMS before it actually connects to a database. The
JDBC DriverManager class then sends all JDBC API calls to the loaded driver.
There are 4 types of JDBC drivers:
▪ JDBC-ODBC bridge driver
▪ Native-API driver (partially java driver)
▪ Network Protocol driver (fully java driver/Middleware driver)
▪ Thin driver (fully java driver/pure java driver)

Dept.of CSE DSU


JDBC ARCHITECTURE

Dept.of CSE DSU


JDBC DRIVER FUNCTIONS

▪ JDBC drivers created by DBMS manufacturers have to


1.Open a connection between the DBMS and the J2EE component.
2.Translate low-level equivalents of SQL statements sent by the
J2EE component into messages that can be processed by the DBMS.
3.Return data that conforms to the JDBC specification to the JDBC
driver.
4.Return information such as error messages that conforms to the
JDBC specification to the JDBC driver.
5.Provide transaction management routines that confirm to the JDBC
specification.
6.Close the connection between the DBMS and the J2EE
component.

Dept.of CSE DSU


JDBC DRIVER FUNCTIONS

▪ The JDBC driver makes J2EE components database independent, which complements Java's
philosophy of platform independence.
▪ Today there are JDBC drivers for nearly every commercial DBMS, and they are available from the
Sun Microsystems, Inc. website (www.sun.com) or from the DBMS manufacturer's web site.
▪ Java code independence is also extended to implementation of the SQL queries. SQL queries are
passed from the JDBC API through the JDBC driver to the DBMS without validation. This means it
is the responsibility of the DBMS to implement SQL statements contained in the query.

9
Dept.of CSE DSU
JDBC PACKAGES

The JDBC API is contained in two packages.


1.java.sql
2.javax.sql
▪ The first package is called java.sql and contains core Java data objects of the JDBC API. These include Java
data objects that provide the basics for connecting to the DBMS and interacting with data stored in the DBMS.
java.sql is part of the J2SE.
▪ The java.sql package contains various interfaces and classes used by the JDBC API. This collection of
interfaces and classes enable the primary functions of opening and managing connections to a particular
DBMS.
▪ The java.sql package contains the entire JDBC API that sends SQL (Structured Query Language) statements to
relational databases and retrieves the results of executing those SQL statements.

Dept.of CSE DSU


Java.sql PACKAGE

Dept.of CSE DSU


Javax.sql PACKAGE
▪ The other package that contains the JDBC API is javax.sql, which extends java.sql and is in the
J2EE.
▪ Included in the javax.sql package are Java data objects that interact with Java Naming and
Directory Interface (JNDI) and Java data objects that manage connection pooling, among other
advanced JDBC features.

Dept.of CSE DSU


TYPE 1:JDBC DRIVER TYPES
1.TYPE 1:JDBC-ODBC BRIDGE DRIVER
▪ JDBC-to-ODBC(Open Database Connection) driver also called as JDBC/ODBC bridge is used to translate
DBMS calls between JDBC specification and ODBC specification.

▪ The JDBC-ODBC driver receive messages from a J2EE component that confirms to the JDBC specification and
those messages are translated by JDBC-ODBC driver into ODBC message format understood by DBMS.

▪ JDBC-ODBC Bridge is not suitable for mission critical applications as extra translation might negatively impact
the performance.

Dept.of CSE DSU


TYPE 1:JDBC DRIVER TYPES

Advantages:
1.It is very easy to use as once ODBC driver is installed.
2.Almost any database is supported.

Limitations:
1.Performance is not efficient.
2.ODBC drivers needs to be installed on the client machine.
3.Type 1 drivers are not portable as they are not completely written in Java.

14
Dept.of CSE DSU
TYPE 1:JDBC DRIVER TYPES

Advantages:
1.It is very easy to use as once ODBC driver is installed.
2.Almost any database is supported.

Limitations:
1.Performance is not efficient.
2.ODBC drivers needs to be installed on the client machine.
3.Type 1 drivers are not portable as they are not completely written in Java.

15
Dept.of CSE DSU
TYPE 1:JDBC DRIVER TYPES

Advantages:
1.It is very easy to use as once ODBC driver is installed.
2.Almost any database is supported.

Limitations:
1.Performance is not efficient.
2.ODBC drivers needs to be installed on the client machine.
3.Type 1 drivers are not portable as they are not completely written in Java.

16
Dept.of CSE DSU
TYPE 1:JDBC DRIVER TYPES

Advantages:
1.It is very easy to use as once ODBC driver is installed.
2.Almost any database is supported.

Limitations:
1.Performance is not efficient.
2.ODBC drivers needs to be installed on the client machine.
3.Type 1 drivers are not portable as they are not completely written in Java.

17
Dept.of CSE DSU
JDBC-ODBC BRIDGE DRIVER

Dept.of CSE DSU


TYPE 2:NATIVE CODE/API DRIVER

▪ The Java/Native Code driver uses Java classes to generate platform-specific code-that is, code only understood
by a specific DBMS.

▪ The manufacturer of the DBMS provides both the Java/Native Code driver and API classes so the J2EE
component can generate the platform-specific code.

▪ The Native API driver uses the client-side libraries of the database. The driver converts JDBC method calls into
native calls of the database API. It is not written entirely in java.

▪ The obvious disadvantage of using a Java/Native Code driver is the loss of some portability of code. The API
classes for the Java/Native Code driver probably won't work with another manufacturer's DBMS.

Dept.of CSE DSU


TYPE 2:NATIVE CODE/API DRIVER

Advantage:
▪ performance upgraded than JDBC-ODBC bridge driver.

▪ Disadvantage:
▪ The Native driver needs to be installed on the each client machine.
▪ The Vendor client library needs to be installed on client machine.
▪ Client side library is not available for all databases.

20
Dept.of CSE DSU
TYPE 2:NATIVE CODE/API DRIVER

Dept.of CSE DSU


TYPE 3:NETWORK PROTOCOL
DRIVER/MIDDLEWARE
▪ The Type 3 JDBC driver, also referred to as the Java Protocol, is the most commonly used JDBC driver. The
Type 3 JDBC driver converts SQL queries into JDBC-formatted statements. The JDBC-formatted statements
are translated into the format required by the DBMS.

▪ The Network Protocol driver uses middleware (application server) that converts JDBC calls directly or
indirectly into the vendor-specific database protocol. It is fully written in java.

Dept.of CSE DSU


TYPE 3:NETWORK PROTOCOL
DRIVER/MIDDLEWARE

Advantage:
▪No client side library is required because of application server that can perform many tasks like
auditing, load balancing, logging etc.

Disadvantages:
▪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.

23
Dept.of CSE DSU
TYPE 3:NETWORK PROTOCOL
DRIVER/MIDDLEWARE

Dept.of CSE DSU


TYPE 4: JDBC DRIVER-THIN DRIVER

▪ Type 4 JDBC driver is also known as the Type 4 database


protocol. This driver is similar to the Type 3 JDBC driver except
SQL queries are translated into the format required by the DBMS.
▪ SQL queries do not need to be converted to JDBC-formatted
systems. This is the fastest way to communicate SQL queries to
the DBMS.
▪ The thin driver converts JDBC calls directly into the vendor-
specific database protocol. That is why it is known as thin driver.
It is fully written in Java language.

Dept.of CSE DSU


TYPE 4: JDBC DRIVER-THIN DRIVER

Advantage:
▪ Better performance than all other drivers.
▪ No software is required at client side or server side.
Disadvantage:
▪ Drivers depend on the Database.

26
Dept.of CSE DSU
TYPE 4: JDBC DRIVER-THIN DRIVER

Dept.of CSE DSU


Loading the JDBC Driver :

❏ The JDBC driver must be loaded before the J2EE component can connect to the DBMS.
❏ The Class.forName() method is used to load the JDBC driver.
❏ Suppose a developer wants to work offline and write a J2EE component that interacts with
Microsoft Access on the developer's PC.
❏ The developer must write a routine that loads the JDBC/ODBC Bridge driver called
sun.jdbc.odbc.jdbcOdbcDriver.
❏ The driver is loaded by calling the Class.forName() method and passing it the name of the driver,
as shown in the following code segment:

class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”)

Dept.of CSE DSU


❏ Connect to the DBMS Once the driver is loaded, the J2EE component must connect to the DBMS using the
DriverManager.getConnection() method.
❏ The java.sql.DriverManager class is the highest class in the java.sql hierarchy and is responsible for managing
driver information. The DriverManager.getConnection() method is passed the URL of the database, and the user
ID and password if required by the DBMS.
❏ The URL is a String object that contains the driver name and the name of the database that is being accessed by
the J2EE component. The DriverManager.getConnection() method returns a Connection interface that is used
throughout the process to reference the database. The java.sql.Connection interface is another member of the
java.sql package that manages communication between the driver and the J2EE component.
❏ It is the java.sql.Connection interface that sends statements to the DBMS for processing.
❏ Listing 6-1 illustrates the use of the DriverManager.getConnection() method to load the JDBC/ODBC Bridge and
connect to the Customer Information database.

Dept.of CSE DSU


String url = "jdbc: odbc:CustomerInformation";
String userID = "jim";
String password = "keogh";
Statement DataRequest;
private Connection Db;
try { Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver");
Db = DriverManager.getConnection(url, userID, password)

Dept.of CSE DSU


Create and Execute a SQL Statement :

❏ The Connect.createStatement() method is used to create a Statement object.


❏ The Statement object is then used to execute a query and return a ResultSet object that contains the
response from the DBMS, which is usually one or more rows of information requested by the J2EE
component.

Statement DataRequest;
ResultSet Results;
try {
String query = "SELECT * FROM Customers";
DataRequest = Database.createStatement();
DataRequest = Db.createStatement();
Results = DataRequest.executeQuery (query);
DataRequest.close();
Dept.of CSE DSU
}
Process the Data Returned by DBMS:

❏ The java.sql.ResultSet Object is assigned the results received from the DBMS after the query is
processed. The java.sql.ResultSet object consists of methods used to interact with the data that is
returned by the DBMS to the J2EE component

Dept.of CSE DSU


ResultSet Results;
String FirstName;
String LastName;
String printrow;
boolean Records = Results.next();
if (! Records ) {
System.out.println("No data returned");
return;
else
do {
FirstName = Results.getString (FirstName) ;
LastName = Results.getString (LastName);
printrow = FirstName + " " + LastName;
System.out.println(printrow);
}
while ( Results.next()
);

Dept.of CSE DSU


Terminate the connection to the DBMS :

❏ The connection to the DBMS is terminated by using the close() method of the Connection
object once the J2EE component is finished accessing the DBMS.
❏ The close() method throws an exception if a problem is encountered when disengaging the
DBMS.
❏ DB.close()

Dept.of CSE DSU


Database Connection
▪ A J2EE component does not directly connect to a DBMS. Instead, the J2EE component connects
with the JDBC driver that is associated with the DBMS.
▪ However, before this connection is made, the JDBC driver must be loaded and registered with
the DriverManager.
▪ The purpose of loading and registering the JDBC driver is to bring the JDBC driver into the Java
Virtual Machine (JVM). The JDBC driver is automatically registered with the DriverManager once
the JDBC driver is loaded and is therefore available to the JVM and can be used by J2EE
components.
▪ The Class.forName() method is used to load the JDBC driver.
▪ In this example, the JDBC/ODBC Bridge is the driver that is being loaded. You can replace the
JOBC/ODBC Bridge with the appropriate JDBC driver for the DBMS being used in your J2EE
application.
▪ The Class.forName() method throws a ClassNotFoundException if an error occurs when loading
the JDBC driver. Errors are trapped using the catch{} block whenever the JDBC driver is being
loaded.
Dept. of CSE, DSU 35
try {
Class.forName(“sun.jdbc.odbc.JdbcodbcDriver”);
}
catch (ClassNotFoundException error) {
System.err.println(“Unable to load JDBC/ODBC bridge “,+ error.getMesssage());
System.exit(1);
}

Dept. of CSE, DSU 36


The Connection
▪ After the JDBC driver is successfully loaded and registered, the J2EE component
must connect to the database. The database must be associated with the JDBC
driver, which is usually performed by either the database administrator or the
systems administrator.
▪ The data source that the JDBC component will connect to is defined using URL
format. The URL consists of three parts. These are
▪ JDBC which indicates that the JDBC protocol is to be used to read the URL
▪ <subprotocol> which is the JDBC driver name.
▪ < subname> which is the name of the database.

Dept. of CSE, DSU 37


▪ The connection to the database is established by using one of three
getConnection() methods of the DriverManager object. The getConnection()
method requests access to the database from the DBMS.
▪ It is up to the DBMS to grant or reject access. A Connection object is returned by
the getConnection() method if access is granted; otherwise, the getConnection()
method throws a SQLException

Dept. of CSE, DSU 38


Dept. of CSE, DSU 39
Dept. of CSE, DSU 40
Dept. of CSE, DSU 41
Dept. of CSE, DSU 42
STATEMENTS

Dept. of CSE, DSU 43


Creating Statement Object
Statement stmt = null;
try {
stmt = conn.createStatement( );
...
} catch (SQLException e) {
...
}
finally {
stmt.close();
}

Statement object, you can use to execute an SQL statement with one of its three execute methods:
▪ boolean execute (String SQL):
• to execute SQL DDL statements
▪ int executeUpdate (String SQL) :
• INSERT, UPDATE, or DELETE and DDL statement
• Returns the number of rows affected
▪ ResultSet executeQuery (String SQL) :
• Returns a ResultSet object for SELECT statement
Dept. of CSE, DSU 44
Creating Statement Object

Dept. of CSE, DSU 45


Creating Statement Object
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();

String sql = "UPDATE Employees set age=30 WHERE id=103";


// Let us check if it returns a true Result Set or not.

Boolean ret = stmt.execute(sql);


System.out.println("Return value is : " + ret.toString() );

// Let us update age of the record with ID = 103;


int rows = stmt.executeUpdate(sql);
System.out.println("Rows impacted : " + rows );

// Let us select all the records and display them.

sql = "SELECT id, first, last, age FROM Employees";


ResultSet rs = stmt.executeQuery(sql);
Dept. of CSE, DSU 46
Creating Statement Object

Dept. of CSE, DSU 47


Creating Statement Object

Dept. of CSE, DSU 48


Creating Statement Object: The PreparedStatement Objects

This statement gives


you the flexibility of
supplying arguments
dynamically

stmt.setInt(1, 35); // This would set


age
stmt.setInt(2, 102); // This would set
ID
int rows = stmt.executeUpdate();
Dept. of CSE, DSU 49
The PreparedStatement Objects

Dept. of CSE, DSU 50


The PreparedStatement Objects: Advantages
1. PreparedStatement are pre-compiled (once), so faster for repeated execution
of dynamic SQL (where parameters change)
2. They provide stronger separation between the query code and the parameter
values (compared to concatenated SQL strings), boosting readability and helping
code maintainers quickly understand inputs and outputs of the query.
3. PreparedStatement allows us to execute dynamic queries with parameter inputs.
4. PreparedStatement provides different types of setter methods to set the input
parameters for the query.
5. If there are multiple parameters to set, writing Query using String concatenation
looks very ugly and error prone.

Dept. of CSE, DSU 51


Creating CallableStatement Object
CREATE OR REPLACE PROCEDURE getEmpName
(EMP_ID IN NUMBER, EMP_FIRST OUT VARCHAR) AS
BEGIN
SELECT first INTO EMP_FIRST
FROM Employees
WHERE ID = EMP_ID;
END;

The next code snippet shows how to employ the Connection.prepareCall() method
to instantiate a CallableStatement object based

Dept. of CSE, DSU 52


Creating CallableStatement Object
CallableStatement cstmt = null; IN/OUT/INOUT
try { parameter
String SQL = "{call getEmpName (?, ?)}";
cstmt = conn.prepareCall (SQL);
...
}
catch (SQLException e) {
... //Bind IN parameter first, then bind OUT parameter
} int empID = 102;
finally { stmt.setInt(1, empID); // This would set ID as 102
cstmt.close();
} // Because second parameter is OUT so register it
stmt.registerOutParameter(2, java.sql.Types.VARCHAR);

//Use execute method to run stored procedure.


System.out.println("Executing stored procedure..." );
stmt.execute();
Dept. of CSE, DSU 53
RESULTSET
▪ A query is used to update, delete, and retrieve information stored in a database. The executeQuery()
method is used to send the query to the DBMS for processing and returns a ResultSet object that
contains data that was requested by the query.

▪ The ResultSet object contains methods that are used to copy data from the ResultSet into a Java
collection object or variable for further processing.

▪ Data in a ResultSet object is logically organized into a virtual table consisting of rows and columns. In
addition to data, the ResultSet object also contains metadata such as column names, column size, and
column data type.

▪ The ResultSet uses a virtual cursor to point to a row of the virtual table. A J2EE component must move
the virtual cursor to each row and then use other methods of the ResultSet object to interact with the data
stored in columns of that row.

▪ The virtual cursor is positioned above the first row of the data when the ResultSet is returned by the
executeQuery() method. So virtual cursor must be moved to first row using next() method.

Dept. of CSE, DSU


RESULTSET
▪ The next() method returns a Boolean true if row contains the data, otherwise, a Boolean value false is returned
indicating that no more rows exists in the ResultSet.

▪ Once the virtual cursor points to arrow, the getXXX() method is used to copy data from the row to variable.

▪ The getxxx() method is data type specific. getString() method is used to copy String data from a column of the
ResultSet. The data type of the getxxx() method must be the same data type as the column in the ResultSet.

▪ The gebxxx() method requires one parameter, which is an integer that represents the number of the column that
contains the data. For example, getString(1) copies the data from the first column of the ResultSet.

▪ Columns appear in the ResultSet in the order in which column names appeared in the SELECT statement in the
query.

▪ SELECT statement: SELECT Customer FirstName, Customer LastName FROM Customer. This query directs the
DBMS to return two columns. The first column contains customer first names and the second column contains
customer last names. Therefore, getString(1) returns data in the customer first-name column of the current row in
the ResultSet.
READING THE RESULTSET
Example:

String query = "SELECT FirstName, Last Name FROM Customers";

DataRequest=Db.createStatement();

Results DataRequest.executeQuery (query);

boolean Records = Results.next();

do

FirstName =Results.getString (1);

LastName = Results.getString (2);

printrow = FirstName +""+LastName;

System.out.println(printrow);

}while (Results.next());

Dept. of CSE, DSU


SCROLLABLE RESULTSET
▪ Until the release of the JDBC 2.1 API, the virtual cursor could only be moved down the ResultSet object. But now
the virtual cursor can be moved backwards or even positioned at a specific row. The JDBC 2.1 API also enables a
J2EE component to specify the number of rows to return from the DBMS.

▪ Six methods of the ResultSet object are used to position the virtual cursor, in addition to the next() method .These
are first(), last(), previous(), absolute(), relative(), and getRow().

▪ The first() method moves the virtual cursor to the first row in the ResultSet. Likewise, the last() method positions
the virtual cursor at the last row in the ResultSet. The previous() method moves the virtual cursor to the previous
row.

▪ The absolute() method positions the virtual cursor at the row number specified by the integer passed as a parameter
to the absolute() method . The relative() method moves the virtual cursor the specified number of rows contained in
the parameter. The parameter is a positive or negative integer where the sign represents the direction the virtual
cursor is moved.

▪ The Statement object that is created using the createStatement() of the Connection object must be set up to handle a
scrollable ResultSet by passing the createStatement() method one of three constants . These constants are
TYPE_FORWARD_ONLY,TYPE_SCROLL_INSENSITIVE and TYPE SCROLL SENSITIVE.
SCROLLABLE RESULTSET
The TYPE FORWARD ONLY constant restricts the virtual cursor to downward movement, which is
the default setting.

The TYPE_SCROLL_INSENSITIVE, and TYPE SCROLL SENSITIVE constants permit the virtual
cursor to move in both directions.

The TYPE_SCROLL_INSENSITIVE constant makes the ResultSet insensitive to changes made by


another J2EE component to data in the table whose rows are reflected in the ResultSet

The TYPE-SCROLL SENSITIVE constant makes the ResultSet sensitive to those changes.

NOTE: Not all JDBC drivers are not scrollable

Dept. of CSE, DSU


SCROLLABLE RESULTSET
Example: DataRequest=Db.createStatement(TYPE_SCROLL_INSENSITIVE);

Results DataRequest.executeQuery (query);

do{ Results.first();

Results.last();

Results.previous();

Results.absolute(10);

Results.relative(-2);

Results.relative(2);

FirstName Results.getString (1);

LastName Results.getString (2);

printrow= FirstName +""+ LastName;

System.out.println(printrow)

}while (Results.next());

Dept. of CSE, DSU


UPDATABLE RESULTSET
▪ Rows contained in the ResultSet can be updated similar to how rows in a table can be updated . This is made possible by
passing the createStatement() method of the Connection object the CONCUR_UPDATABLE.

▪ Alternatively, the CONCUR_READ ONLY constant can be passed to the createStatement() method to prevent the
ResultSet from being updated.

▪ There are three ways to update a ResultSet. These are updating values in a row, deleting a row, and inserting a new row.

▪ updateXXX() method is used to change the value of a column in the current row of the ResultSet. A value in a column of
the ResultSet can be replaced with a NULL value by using the updateNull() method.

Ex:

DataRequest=Db.createStatement(ResultSet.CONCUR_UPDATABLE);

Results DataRequest.executeQuery (query);

Results.updateString ("LastName", "Smith");

Results.updateRow();

Dept. of CSE, DSU


DELETE A ROW IN THE RESULTSET
▪ The deleteRow() method is used to remove a row from a ResultSet. The deleteRow() method is
used to remove a row from a ResultSet.

▪ Sometimes this is advantageous when processing the ResultSet because this is a way to eliminate
rows from future processing. For example, each row of a ResultSet may have to pass three tests.
Those that fail to pass the first test could be deleted from the ResultSet, thereby reducing the
number of rows in the ResultSet that have to be evaluated for the second test. This also deletes it
from the underlying database.

▪ The deleteRow() method is passed an integer that contains the number of the row to be deleted.

▪ The deleteRow() method is then passed a zero integer indicating that the current row must be
deleted Ex:Results.deleteRow(0);

Dept. of CSE, DSU


INSERT A ROW IN THE RESULTSET
▪ Inserting a row into the ResultSet is accomplished using basically the same technique used to
update the ResultSet.

▪ That is, the updateXXX() method is used to specify the column and value that will be placed into
the column of the ResultSet. The updateXXX() method requires two parameters. The first
parameter is either the name of the column or the number of the column of the ResultSet. The
second parameter is the new value that will be placed in the column of the ResultSet.

▪ The insertRow() method is called after the updateXXX() methods which causes anew row to be
inserted into the ResultSet having values that reflect the parameters in the updateXXX()
methods. This also updates the underlying database.

Dept. of CSE, DSU Dept.of CSE DSU

You might also like