Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. /* JDBC program with ResultSet objects.  A ResultSet is
  2.    the JDBC analog of a PL/SQL cursor. */
  3. import java.sql.*;
  4. import java.io.*;
  5. import java.util.*;
  6. public class ThreeLevel
  7. { public static void main(String[]args) throws
  8.     SQLException, ClassNotFoundException
  9.   {
  10.     /* load jdbc drivers and
  11.        connect to Oracle with the thin JDBC driver*/
  12.     Class.forName("oracle.jdbc.OracleDriver");
  13.     Connection conn =
  14.       DriverManager.getConnection
  15.         ("jdbc:oracle:thin:@db11.eng.fau.edu:1521:R11g",
  16.          "acuartin", "oracle");
  17.  
  18.     /* create a Statement object for the connection and
  19.        execute the indicated query from it, placing the
  20.        output from the query in a ResultSet object */
  21.     Statement s = conn.createStatement();
  22.     ResultSet r1 = s.executeQuery("SELECT * FROM S ORDER BY S#");
  23.  
  24.     /* we separately prepare the select statement representing
  25.        the set of part records that are supplied by the current
  26.        supplier for a loop iteration so that we can compile that
  27.        select statement once, instead of compiling it for each
  28.        loop iteration */
  29.     ResultSet r2;
  30.     PreparedStatement s2 =
  31.       conn.prepareStatement(
  32.         "SELECT DISTINCT P.* FROM P,SPJ" +
  33.            " WHERE SPJ.S# = ? AND SPJ.P# = P.P#" +
  34.              " ORDER BY P.P#");
  35.  
  36. /*new stuff*/
  37.     ResultSet r3;
  38.     PreparedStatement j3 =
  39.       conn.prepareStatement(
  40.         "SELECT DISTINCT J.* FROM J,SPJ" +
  41.            " WHERE SPJ.P# = ? AND SPJ.J# = J.J#" +
  42.              " ORDER BY J.J#");
  43. /*til here*/
  44.  
  45.  
  46.     /* for each supplier record */
  47.     while(r1.next())
  48.     {  
  49.        /* print the supplier record */
  50.        System.out.println(r1.getString(1)+"  "+ r1.getString(2)+"  "+
  51.                           r1.getInt(3)+"  "+r1.getString(4));
  52.  
  53.        /* substitute the current S# for the parameter in the
  54.           PreparedStatement object, and execute the select
  55.           statement from that PreparedStatement object */
  56.        s2.setString(1,r1.getString(1));
  57.        r2 = s2.executeQuery();
  58.  
  59.        /* for each part record supplied by the current supplier */
  60.        while(r2.next())
  61.        {
  62.         /* print the part record */
  63.         System.out.println("    "+ r2.getString(1)+"  " +
  64.                            r2.getString(2)+"  "+ r2.getString(3)+
  65.                            "  " +r2.getInt(4)+"  "+r2.getString(5));
  66. /*new stuff*/
  67.             j3.setString(1,r2.getString(1));
  68.             r3 = j3.executeQuery();
  69.  
  70.  
  71.             while(r3.next())
  72.             {
  73.              System.out.println("        "+ r3.getString(1)+"  " +
  74.                                 r3.getString(2)+"  "+ r3.getString(3));
  75.             }
  76. /*until here*/
  77.         }
  78.    
  79.     }
  80.    
  81.  
  82.     /* close the Statement object, PreparedStatement object, and
  83.        connection (note that the result sets are automatically closed
  84.        when their statements are closed) */
  85.     s.close();
  86.     s2.close();
  87.    
  88. /*new stuff*/
  89.     j3.close();
  90. /*til here*/
  91.    
  92.     conn.close();
  93.   }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement