Home Jdbc JDBC Nested Resultset
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

JDBC Nested Resultset

Advertisement
The JDBC Nested Result Set is the simplest join algorithm. In this case for each tuple in the outer join relation, the entire inner join is scanned and the tuple matches the join condition are finally added to the result set.

JDBC Nested Resultset

     

The JDBC Nested Result Set  is the simplest join algorithm. In this case for each tuple in the outer join relation, the entire inner join is scanned and the tuple matches the join condition are finally added  to the result set.

Understand with Example

In this Tutorial we want to describe you a code that helps in understanding JDBC Nested Result Set. The code include a class JDBC Nested Result Set, Inside the class we have a main method, follow the list of steps -

  Import a package java.sql  provides you a network interface, that enables to communicate between front end and back end.

  Loading a driver by making a call class.forName ( ),that accept driver class as argument.

   DriverManager.getConnection ( ) : This provides you to established a connection between url and database.

  createStatement ( ) : The create Statement is used to obtain sql object. This object is used to send and execute sql queries in backend of the database.

   executeQuery ( ) :This method is used to retrieve the record set from a table and store the record set in a result set. The select statement is used for this method.

  next ( ) : This method is used to return the next element in the series.

   getString ( ) : This method  retrieve the value of the specific column in the current row of result set object as string representation.

Finally the println print the ID,Name,Class in the output. In the same way if the tuple in the result set 1 matches with the tuple result set 2,The output  will be printed   In case there is an exception in try block, the catch block caught and handle the exception.

JdbcNestedResultset.java
import java.sql.*; 

public class JdbcNestedResultset {



  public static void main(String args[]) {

  Connection con = null; 

  Statement st1 = null; 

  Statement st2 = null; 

  ResultSet rs1 = null; 

  ResultSet rs2 = null; 



  String url = "jdbc:mysql://localhost:3306/"; 

  String db = "komal"; 

  String driver = "com.mysql.jdbc.Driver"; 

  String user = "root"; 

  String pass = "root"; 

  try { 



  Class.forName(driver);

  con = DriverManager.getConnection(url + db, user, pass);



  st1 = con.createStatement();

  st2 = con.createStatement();



  String sql = "Select * from stu"; 

  rs1 = st1.executeQuery(sql);



  System.out.println("Id\tName\tClass\tLibno");

  while (rs1.next()) {

  String id = rs1.getString("id");



  System.out.print(id + "\t");

  System.out.print(rs1.getString("name") + "\t");

  System.out.print(rs1.getString("class") + "\t");



  rs2 = st2.executeQuery("select * from lib where id ='" + id + "'");

  while (rs2.next()) {

  System.out.println(rs2.getString("libno") + "\t");

  } 

  } 

  } catch (Exception e) {

  e.printStackTrace();

  } 

  } 

} 

Output

Id	Name	Class	Libno
1	komal	10	101	
2	santosh	11	102	
3	rakesh	9	103	
4	ajay	11	104	
5	bhanu	10	105	

Download code

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 19, 2008

Ask Questions?    Discuss: JDBC Nested Resultset  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
inCitizen
March 3, 2012
help needed !

public static void Rule1() { Connection con = null; Statement st = null; ResultSet rs = null; int updateQuery=0; String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "aiman"; /***************/ try { Class.forName("com.mysql.jdbc.Driver").newInstance(); // Load JBBC driver "com.mysql.jdbc.Driver". con = DriverManager.getConnection(url, "root", "aiman");/*Create a connection*/ st = con.createStatement(); /***count rows**/ int count=0; ResultSet res = st.executeQuery("SELECT COUNT(*) FROM feature"); while (res.next()) { count = res.getInt(1); } System.out.println("Number of rows:"+count); /*****************************Inserting Values**************************/ String w1,w2; String nsubj="nsubj"; String query[] ={"SELECT * FROM Dependency WHERE header like 'nsubj'"}; System.out.println("before printing"); for(String q : query) { res = st.executeQuery(q); //System.out.println("Names for query "+ q +" are"); while (res.next()) { String header = res.getString("header"); String wi = res.getString("wi"); String wj = res.getString("wj"); System.out.println(header+"|"+wi+"|"+wj); ResultSet res1 = null; Statement st1=null; st1=con.createStatement(); String query1[] ={"SELECT * FROM tagtable WHERE word like 'wi' AND tag like '%NN%'"}; for(String q1 : query1) { res1 = st1.executeQuery(q1); while (res1.next()) { System.out.println("in the loop"); System.out.println("*******************"); String w = res1.getString("word"); String tag = res1.getString("tag"); System.out.println(w+"|"+tag); } } res1.close(); }//end while System.out.println("done"); }//end for res.close(); st.close(); con.close(); /************************************************************************/ }//try ends catch(Exception e) { System.out.println("Exception is = "+e); } }//rule 1 ends here }//feature class ends ====================================================== the inner while loop which is : while (res1.next()) { System.out.println("in the loop"); System.out.println("*******************"); String w = res1.getString("word"); String tag = res1.getString("tag"); System.out.println(w+"|"+tag); } DOES NOT GET EXECUTED ! HELP !
DMCA.com