Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

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.

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

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

Advertisements

Ads
Share on Google+Share on Google+

JDBC Nested Resultset

Posted on: November 19, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: JDBC Nested Resultset  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
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 !

Ads

 

Ads