Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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

                         

» View all related tutorials
Related Tags: java sql mysql c database api ide jdbc data batch application io get vi exec state int bat this id

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.