What is WebRowSet?

A WebRowSet has all the capabilities of CachedRowSet and in addition it is used to read and write database query results into an XML file. Because through XML different platforms can communicate with eachother , XML has become a standard for webservices communication.
Class-Javax.sql.rowset.WebRowSet
Implementation-com.sun.rowset.CachedRowSetImpl

Example:

import javax.sql.*;
import javax.sql.rowset.*;
import java.io.*;
import com.sun.rowset.*;
public class demo{
public static void main(String[] args)throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
WebRowSet wrs=new WebRowSetImpl();
wrs.setUrl("jdbc:odbc:dsn");
wrs.setUsername("system");
wrs.setPassword("oracle");
wrs.setCommand("select *from emp");
wrs.execute();
wrs.writeXml(new FileOutputStream("emp.xml"));
System.out.println("write successful");
wrs.close();
}}

You can also get the data from ResultSet:

import java.io.*;
import javax.sql.*;
import java.sql.*;
import javax.sql.rowset.*;
import com.sun.rowset.*;
import java.io.*;
public class demo1{
public static void main(String[] args)throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
WebRowSet wrs=new WebRowSetImpl();
Connection con=DriverManager.getConnection("jdbc:odbc:dsn","system","oracle");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("Select *from emp");
wrs.writeXml(rs,new FileOutputStream("emp.xml"));//two arguments
wrs.close();
rs.close();
stmt.close();
con.close();
}}

Getting the data from XML file :

The data is already exist in that emp.xml file. So no need for a connection with database.
import javax.sql.*;
import javax.sql.rowset.*;
import java.io.*;
import com.sun.rowset.*;
public class demo{
public static void main(String[] args)throws Exception {
WebRowSet wrs=new WebRowSetImpl();
wrs.readXml(new FileInputStream("emp.xml"));
while(wrs.next()){
System.out.println(wrs.getString(2));
}
wrs.close();
}}
The update, insert,delete method process is same as ResultSet.

1 comment:

  1. Demo.java:8: warning: com.sun.rowset.WebRowSetImpl is Sun proprietary API and may be removed in a future release...

    how to solve this problem coz i am using jdk7..?

    ReplyDelete