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 |