Java Get database column name, type and other information-years forgotten who-blog Park http://www.cnblogs.com/henuyuxiang/p/6155935.html
PrivateList<string> Getdbtablename ()throwsException {List<String> tables =NewArraylist<>(); String SQL= "Show Tables"; PreparedStatement pstmt=Newdbutil (). Getconn (). preparestatement (SQL); ResultSet RS=Pstmt.executequery (); while(Rs.next ()) {Tables.add (rs.getstring (1)); } returntables; } PrivateMap<string, map<string, string>> gettablecolumnname (String tableName)throwsException {String SQL= "SELECT * from" +TableName; PreparedStatement pstmt=Newdbutil (). Getconn (). preparestatement (SQL); ResultSet RS=Pstmt.executequery (); ResultSetMetaData RSMD=Pstmt.getmetadata (); Map<string, string> tablecolumnname_classname =NewHashmap<>(); for(inti = 0; I < Rsmd.getcolumncount (); i++) {tablecolumnname_classname.put (Rsmd.getcolumnname (i+1), Rsmd.getcolumnclassname (i+1)); } Map<string, map<string, string>> tablecolumnnames =NewHashmap<>(); Tablecolumnnames.put (TableName, tablecolumnname_classname); returnTablecolumnnames; } Private voidGetdballtablesname_tablecolumnsname ()throwsException {List<Map> Dballtablesname_tablecolumnsname =NewArraylist<>(); List<String> tables = This. Getdbtablename (); for(String table:tables) {Dballtablesname_tablecolumnsname.add ( This. Gettablecolumnname (table)); } System.out.println (Dballtablesname_tablecolumnsname.tostring ()); }
MySQL iterates through the database all tables, table names, table column names