The JoinRowSet is used to hold the data coming from two or more tables. It can be achieved by using the join in sql. The JoinRowSet has equivalent of an SQL JOIN without having to connect to a data source In order to get the joined data one column in both tables should be common. The common column is known as the match column. The match column is set by calling the addRowSet() method for each RowSet object with the column index and the RowSet objects which you want to join.


The following code will illustrate the use of the JoinRowSet.


 


package com.visualbuilder;


import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.JoinRowSet;


import com.sun.rowset.CachedRowSetImpl;
import com.sun.rowset.JoinRowSetImpl;


public class JoinRowSetExample {
       public static void main(String[] args) throws Exception {
              Class.forName("org.postgresql.Driver");
                            CachedRowSet employee = new CachedRowSetImpl();
              employee.setUsername("postgres");
              employee.setPassword("pass");
              employee.setUrl("jdbc:postgresql://localhost:5432/postgres");
              employee.setCommand("select id,ename,deptid from emp");
              employee.execute();
                            CachedRowSet dept = new CachedRowSetImpl();
              dept.setUsername("postgres");
              dept.setPassword("pass");
              dept.setUrl("jdbc:postgresql://localhost:5432/postgres");
              dept.setCommand("select dept_id,dname from dept");
              dept.execute();
                            JoinRowSet joinset=new JoinRowSetImpl();
              joinset.addRowSet(employee,3);
              joinset.addRowSet(dept,1);
              while (joinset.next()){
                                           System.out.println("Employee is "+joinset.getString("ename"));
                                          System.out.println("Department is "+joinset.getString("dname"));
                                   }
              }
}


Ouput:-


Employee is Adam


Department is Sales


Employee is Susan


Department is Account


Employee is Adran


Department is Marketing


Employee is Hardy


Department is Marketing


Employee is Tim


Department is Account


Employee is Michael


Department is Sales


                    

Copyright © 2012 VisualBuilder. All rights reserved