Difference Betwixt Connected Vs Disconnected Rowset Inward Coffee Jdbc | Rowsetprovider, Rowsetfactory Example

RowSet interface is roughly other primal interface from JDBC API, which extends the ResultSet interface to furnish back upward for the JavaBean element model. Along amongst convenient methods to fix connexion parameters e.g. setUrl(), setUsername(), setPassword() in addition to setCommand(), it besides defines getters in addition to setters for unlike information types e.g. getLong() in addition to setLong(). Another characteristic of RowSet is that it supports lawsuit listeners. You tin attach listeners to RowSet object to heed for events, much similar Observer pattern pattern. RowSet acts equally Subject in addition to notifies Observer on events similar alter inwards cursor location, alter inwards row or alter inwards the entire RowSet. There are ii types of RowSet available inwards JDBC 4.1 API, connected in addition to disconnected.

Influenza A virus subtype H5N1 connected RowSet ever keeps connexion amongst database, spell a disconnected RowSet connects to database, larn the information in addition to thence unopen the connection.

This doesn't forbid from working amongst information though, y'all tin even thence alter information inwards disconnected state, but to commit, connexion needs to endure re-established yesteryear RowSet.

Java provides 5 unlike types of RowSet, JdbcRowSet, JoinRowSet, CachedRowSet, FilteredRowSet and WebRowSet. Out of these 5 only JdbcRowSet is connected RowSet, residual of them are disconnected row sets.

It's rattling of import to sympathise these novel concepts shape JDBC API, at that spot are rattling popular JDBC Interview questions based upon these concepts e.g. difference betwixt RowSet in addition to ResultSet in addition to difference betwixt Connected vs Disconnected RowSet.

Once y'all are familiar amongst primal concept of RowSet class, y'all should endure able to respond these enquiry yesteryear your own.



Difference betwixt Connected in addition to Disconnected RowSet

As I mentioned, primary departure betwixt connected in addition to disconnected RowSet is that quondam ever keeps a database connection, spell afterward doesn't. It connects to database, larn the information in addition to thence unopen the connection. Here are couplet of to a greater extent than differences betwixt them :

1) Disconnected database connected to the database entirely when they desire to read or write, all the times they are disconnected from the database, on the other hand, Connected database drib dead on JDBC connexion endure all time.

2) Only JdbcRowSet from JDBC 4.1 API is connected, residual similar CachedRowSet, WebRowSet are disconnected RowSet implementation.

3) Disconnected RowSet are Serializable and that's why suitable for sending over the network


RowSet Example inwards Java

Let's run across a unproblematic instance of RowSet inwards Java. In this example, nosotros volition retrieve employees tape using RowSet instead of ResultSet interface. So far, y'all mightiness bring entirely seen a ResultSet object returning query result, but from JDBC 4.1 API, y'all tin purpose whatsoever implementation of RowSet implementation for querying database in addition to retrieving result. In this JDBC example, nosotros volition purpose JdbcRowSet class. In guild to create object of JdbcRowSet, y'all demand a RowSetFactory, which tin endure created yesteryear using newFactory() method of RowSetProvider. Once y'all bring an object of RowSet, y'all tin top configuration details similar database URL, username in addition to password yesteryear calling their respective setProperties() method e.g. setURL(), setUsername() in addition to setPassword(). After configuration, its fourth dimension to specify our SQL SELECT Query, nosotros volition purpose method called setCommand() for providing query. Now y'all tin execute the query yesteryear calling execute() method. Did y'all notice a much cleaner in addition to unproblematic API? After query execution its fourth dimension to retrieve data, which is rattling similar to what y'all are used yesteryear calling diverse getXXX() method from ResultSet. You simply demand to iterate through RowSet yesteryear checking hasNext() method inwards spell loop in addition to getting information row yesteryear row.

import javax.sql.rowset.*; import java.sql.*;  /**  * Java Program to illustrate how to purpose RowSet, RowSetProvider, in addition to RowSetFactory inwards JDBC  * In this example, RowSet is used to retrieve information from database instead of ResultSet.  * @author Javin Paul  */ public class RowSetDemo {      public static void main(String[] args) {         String url = "jdbc:mysql://localhost:3306/addressBook";         String userName = "root";         String password = "mysql123";                 try {             // first, create a mill object for rowset             RowSetFactory rowSetFactory = RowSetProvider.newFactory();              // create a JDBC rowset from the factory             JdbcRowSet rowSet = rowSetFactory.createJdbcRowSet();              // Set connexion properties             rowSet.setUrl(url);             rowSet.setUsername(userName);             rowSet.setPassword(password);              // Set SQL Query to execute             rowSet.setCommand("SELECT * FROM contact");             rowSet.execute();             System.out.println("id \tName \tDepartment \tEmail \t\Salary");              // Iterating over RowSet             while (rowSet.next()) {                 System.out.println(rowSet.getInt("id") + "\t"                         + rowSet.getString("name") + "\t"                         + rowSet.getString("department") + "\t"                         + rowSet.getString("email") + "\t"                         + rowSet.getString("salary"));             }         } catch (SQLException sqle) {             sqle.printStackTrace();         }     } }   Output Id      Name    Department      Email   Salary 1       Jack    Sales           jack@bluechip.com       9000 2       Jill    Marketing       jill@bluechip.com       8000 iii       Tom     Accounts        tom@bluechip.com        7000

You tin run across how slow is to purpose RowSet inwards DAO  of your Java program, or y'all tin besides purpose them along amongst DAO Design Pattern. Since RowSet inherit from java.sql.ResultSet interface, y'all tin truly top RowSet implementation to whatsoever method, which await ResultSet. This diagram volition give y'all to a greater extent than thought nearly ResultSet in addition to RowSet class hierarchy.  It besides useful to detect out which RowSet implementation are Connected in addition to which i are Disconnected. You tin run across that at that spot are ii major implementation of RowSet interface, JdbcRowSet which is connected in addition to CachedRowSet which is disconnected.

interface is roughly other primal interface from JDBC API Difference betwixt Connected vs Disconnected RowSet inwards Java JDBC |  RowSetProvider, RowSetFactory Example

CachedRowSet further has couplet of to a greater extent than implementation inwards price of WebRowSet and its subclasses. This tabular array provides a dainty comparing of features supported yesteryear unlike RowSet implementation inwards JDBC API.

interface is roughly other primal interface from JDBC API Difference betwixt Connected vs Disconnected RowSet inwards Java JDBC |  RowSetProvider, RowSetFactory Example

You tin run across that JdbcRowSet is Scrollable, Updateable in addition to Connected, spell CachedRowSet is besides Serializable because its disconnected. WebRowSet farther adds characteristic of generating in addition to consuming XML.


Things to think nearly RowSet, ResultSet in addition to RowSetFactory

It's fourth dimension to revise few primal points nearly these classes from JDBC 4.1 API.

1) RowSet extends ResultSet interface to add together back upward of Java Bean model e.g. it adds methods similar setUrl() which makes it slow to connect to database.

2) There are ii types of RowSet, connected in addition to disconnected. connected RowSet object brand a connexion amongst the database in addition to maintain that connexion throughout its life cycle. On the other hand, a disconnected RowSet makes a connexion amongst data source, read information from it in addition to unopen the connection. Influenza A virus subtype H5N1 disconnected RowSet tin even thence brand changes to its information spell it is non connected amongst database, but it must re-establish connexion to commit those changes.

3) There are 5 implementations of RowSet available inwards JDBC 4.1 API, JdbcRowSet, CachedRowSet, JoinRowSet, WebRowSet and FilteredRowSet. Out of these five, entirely JdbcRowSet is a connected RowSet, residual of them are disconnected RowSets.

4) RowSetProvider provides methods to larn a RowSetFactory implementation, which tin endure used to instantiate a relevant RowSet implementation. Both of these are added from Java 1.7 only.

5) Disconnected RowSet classes are besides Serializable inwards Java.

That's all nearly What is RowSet inwards JDBC, How to purpose RowSet to connect to database, in addition to difference betwixt Connected in addition to Disconnected RowSet inwards Java. We bring besides run across instance of using RowSetProvider and RowSetFactory which tin endure used to  create unlike types of RowSet in Java. Remember, entirely JdbcRowSet is the connected RowSet, residual of them are disconnected one.

Further Learning
JSP, Servlets in addition to JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 in addition to 2
Java Platform: Working amongst Databases Using JDBC



Sumber https://javarevisited.blogspot.com/

0 Response to "Difference Betwixt Connected Vs Disconnected Rowset Inward Coffee Jdbc | Rowsetprovider, Rowsetfactory Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel