Get table dependency order in jdbc

后端 未结 1 1960
梦如初夏
梦如初夏 2021-02-09 23:51

I have a set of tables [A,B,C,D] in MySQL Database

The dependency is as follows B->C->A and D->A that is, A has a PrimaryKey , C has a foreign key which points A\'s Pri

相关标签:
1条回答
  • 2021-02-10 00:39

    I actually just figured this out for a recent school project. There are a few ways to do it such as using ResultSetMetaData and setting up a loop with an exception listener; however the quickest way is what I have below. read is a Statement object and results is a ResultSet object. Using this you should be able to write a loop that cycles through all of the names. I think this method is better because it returns a ResultSet of just the table names. If you add the modifer FULL, a second column will appear describing what kind of tables each are such as base table. Hope this helps!

    read.execute("SHOW TABLES IN your_database_name;");
    results = read.getResultSet();
    results.first();
    System.out.println(results.getString(1));
    

    MySQL Show Statement

    0 讨论(0)
提交回复
热议问题