遍历给定表的所有父表

时间:2017-04-30 09:13:16

标签: java sql oracle java-8 jdbc-odbc

我目前正在进行独立应用程序,遍历所有父表并遍历直到子表没有父表。 示例

 Table1 
   |_ Table 2
      |_ Table 2.1
      |_ Table 2.2
      |_ Table 2.3
         |_ Table 2.3.a
         |_ Table 2.3.b
  |_ Table 3
      |_ Table 3.1
      |_ Table 3.2
      |_ Table 3.3
         |_ Table 3.3.a
         |_ Table 3.3.b
   |_ Table 4 

在这个例子中,无论关系有多大,我都需要到达所有父表,任何人都可以帮我解决这个问题

再次达到表2.3.b之后,它将来到表3,表3是表1的第二个父母

目前,我可以通过beow code

获取表2.1,表2.2,表2.3
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SqlCheck {

    @SuppressWarnings("unused")
    public static void main(String[] args) {
        System.out.println("-------- Oracle JDBC Connection Testing ------");

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            System.out.println("Oracle JDBC Driver Registered!");

            Connection connection = null;
            connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:ganesh", "789", "987");
            PreparedStatement st = connection.prepareStatement("SELECT a.table_name, a.column_name, a.constraint_name, c_pk.table_name r_table_name, c_pk.constraint_name r_pk FROM all_cons_columns a JOIN all_constraints c ON a.owner = c.owner AND a.constraint_name = c.constraint_name JOIN all_constraints c_pk ON c.r_owner = c_pk.owner AND c.r_constraint_name = c_pk.constraint_name WHERE c.constraint_type = 'R' AND a.table_name = ?");
            st.setString(1, "ITEM");
            ResultSet rs=st.executeQuery();

            List parentTables=new ArrayList();

            while(rs.next()){
                //Retrieve by column name
                String id  = rs.getString("table_name");
                String age = rs.getString("column_name");
                String first = rs.getString("constraint_name");
                String parentTable = rs.getString("r_table_name");
                String first3 = rs.getString("r_pk");

                System.out.println(Got parent Tables +"   PPPPPPPPP");
                parentTables.add(parentTable);

            }

            Iterator listOfParentTable=parentTables.iterator();
            while (listOfParentTable.hasNext()) {

                System.out.println("=============11========");
                String string = (String) listOfParentTable.next();
                String s1=  "SELECT a.table_name, a.column_name, a.constraint_name, c_pk.table_name r_table_name, c_pk.constraint_name r_pk FROM all_cons_columns a JOIN all_constraints c ON a.owner = c.owner AND a.constraint_name = c.constraint_name JOIN all_constraints c_pk ON c.r_owner = c_pk.owner AND c.r_constraint_name = c_pk.constraint_name WHERE c.constraint_type = 'R' "
                        + "AND a.table_name = ?";

                PreparedStatement st2 = connection.prepareStatement(s1);
                st2.setString(1, string);
                ResultSet rs1=st2.executeQuery();

                while(rs1.next()){
                    //Retrieve by column name
                    String id  = rs1.getString("table_name");
                    String age = rs1.getString("column_name");
                    String first = rs1.getString("constraint_name");
                    String parentTable1 = rs1.getString("r_table_name");
                    String first3 = rs1.getString("r_pk");

                    System.out.println(string+"   "+parentTable1);

                }
            }
            System.out.println("WELDONE     ^^^^^^^^");

        } catch (SQLException e) {

            System.out.println("Connection Failed! Check System.output console");
            e.printStackTrace();
            return;

        } catch (ClassNotFoundException e) {

            System.out.println("Where is your Oracle JDBC Driver?");
            e.printStackTrace();
            return;
        }
    }
}

任何帮助或建议都是适当的

0 个答案:

没有答案