Database Design
Now to design a directory database table, that is, a table contains the root directory and all levels of subdirectories, when we can design a table, with parent_id to store subdirectories corresponding to the parent directory of the sequence number, the design table is as follows:
field type of table:
+-----------+----------------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+-----------+----------------------+------+-----+---------+----------------+| type_id | smallint ( 5) unsigned | NO | PRI | NULL | auto_increment | | type_name | varchar | NO | | NULL | | | parent_id | smallint (5) unsigned | YES | | 0 | | +-----------+----------------------+------+-----+---------+----------------+
The contents of the table:
+---------+-----------------------+-----------+| type_id | type_name | parent_id |+---------+-----------------------+-----------+| 1 | Books | 0 | | 2 | Technology | 1 | | 3 | Photography Technology | 2 | | 4 | Electronics and Communications | 2 | | 5 | Architecture | 2 | | 6 | Vacuum Electronic Technology | 4 | | 7 | Wireless Communication | 4 | | 8 | Semiconductor Technology | 4 | | 9 | Photography Theory | 3 | | 10 | Cameras and Equipment | 3 | | 11 | Architectural History | 5 | | 12 | Architectural Design | 5 | | 13 | Building Materials | 5 | | 14 | Economy | 1 | | 15 | History | 1 | | 16 | Modern Economy | 14 | | 17 | Ancient Economy | 14 | | 18 | Chinese History | 15 | | 19 | Western History | |+---------+-----------------------+-----------+
from the table, we can be informed that the book is the root directory, technology is a sub-directory of books, and then photography technology is a sub-directory of technology ... Wait, now we want to get this kind of directory hierarchy through Java JDBC.
Java Programming
Package demo0807;/** * Get the Catalog table for the database */import java.sql.connection;import java.sql.drivermanager;import java.sql.ResultSet ; Import Java.sql.resultsetmetadata;import Java.sql.sqlexception;import Java.sql.statement;import Java.util.arraylist;import java.util.hashmap;import Java.util.linkedlist;import Java.util.Map;public class DB {// Database name private string dbname;//database logon name private string username;//database login Password private string Password;//statement object private Static Statement statement=null;//is used to store the results of the hash table private linkedlist<map<string, arraylist<string>>> Linkedlist=new linkedlist<map<string,arraylist<string>>> ();//With the parameter constructor public DB (String DbName, String userName, String password) {this.dbname = Dbname;this.username = Username;this.password = password;} /** * Gets the root directory of the data table * @return root linear table */public arraylist<string> Getrootorder () {String sql=null; ResultSet Resultset=null; Connection Connection=null; ResultSetMetaData Metadata=null; int ColumnCount= 0; arraylist<string> parentlist = new arraylist<string> (); try {class.forname ("Com.mysql.jdbc.Driver"),} catch (ClassNotFoundException e) {System.err.println (" Unable to start MYSQL.JDBC driver! ");} try {connection = drivermanager.getconnection ("jdbc:mysql://localhost/" +dbname, Username,password);} catch (Sqlexce Ption e) {System.err.println ("drive Manager cannot connect to the database! ");} SQL statement sql= "Select Type_name as root from tb3 where parent_id=0;"; Execute SQL statement try {statement=connection.createstatement (); resultset=statement.executequery (sql);} catch (SQLException E {SYSTEM.ERR.PRINTLN ("Cannot execute Query");} try {//Gets the information metadata for the result metaData = Resultset.getmetadata ();//Gets the total number of columns ColumnCount = Metadata.getcolumncount ();} catch ( SQLException e) {System.out.println ("Cannot get table information");} try {while (Resultset.next ()) {int Index=1;while (index<=columncount) {Parentlist.add (resultset.getstring (index)) ; index++;}}} catch (SQLException e) {System.err.println ("failed to query results correctly");} return parentlist;} /** * Recursively find out all directory structures * Find a subdirectory of all directories until the subdirectory is empty * @param parentorder * @return */public linkedlist<map<string, Arrayl Ist<string>>> Lookupsuborder (arraylist<string> parentorder) {ResultSet resultset=null;for (String Dynamicparent:parentorder) {//map type, for storing key-valuemap<string,arraylist<string>> map=new HashMap< String,arraylist<string>> (); String key=dynamicparent; arraylist<string> value = new arraylist<string> (); String sql= "Select Child_type_name from (select p.type_id as type_id,p.type_name as type_name,s.type_name as Child_type_n Ame from TB3 as P left joins TB3 as S on p.type_id=s.parent_id) as tb_tmp where type_name= "+" ' "+dynamicparent+" ' "; try {ResU Ltset = statement.executequery (sql), if (Resultset.next () ==false) {;} else {Boolean flag=true;while (flag) {String tmp= Null;if ((tmp=resultset.getstring (1))!=null) {value.add (TMP);} Flag=resultset.next ();} Lookupsuborder (value);} Map.put (key, value);} catch (SqlexCeption e) {System.err.println ("database query failed! "); E.printstacktrace ();} Linkedlist.add (map);} return linkedlist;}}
Test Code
Package demo0807;/** * Test class */import Java.util.arraylist;import java.util.iterator;import java.util.linkedlist;import Java.util.map;import Java.util.map.entry;import Java.util.set;public class Test {public static void main (string[] args) {db db = new db ("Demo2", "root", "031422zw"); arraylist<string> Mainorder = Db.getrootorder (); SYSTEM.OUT.PRINTLN ("root directory:" +mainorder.tostring ()); linkedlist<map<string, arraylist<string>>> linkedlistsuborder = Db.lookupSubOrder (MainOrder); while (!linkedlistsuborder.isempty ()) {map<string, arraylist<string>> suborder = Linkedlistsuborder.removelast (); set<entry<string, arraylist<string>>> entryset = Suborder.entryset (); Iterator<Entry<String , arraylist<string>>> iterator = Entryset.iterator (); while (Iterator.hasnext ()) {entry<string, Arraylist<string>> next = Iterator.next (); String Parent=next.getkey (); arraylist<string> children = Next.getvalue (); if (!children.isempty ()) {SyStem.out.println (parent+ "subdirectory has" +children.tostring ());}}}}
Run Results
The root directory is: A subdirectory of [book] books has a subdirectory of [Science, economics, history] history has [the history of China, the history of the West] the subdirectory of the economy has [modern economy, Ancient economy] sub-catalogue of science and technology [photography technology, electronics and communication, architecture] sub-catalogue of buildings [architectural history, architectural design, building Sub-directories for electronics and communications [vacuum electronic technology, wireless communication, semiconductor technology] sub-catalogue of photographic technology [photographic theory, camera equipment and devices]
Summary
The program also has a lot of small flaws, feel familiar with the basic data structure and recursive algorithm for programming help very much!
Using Java recursion to get the directory table structure of the database