美文网首页
获取MySQL数据库所有表的列名

获取MySQL数据库所有表的列名

作者: lopman87 | 来源:发表于2017-09-28 15:16 被阅读0次

    importjava.sql.Connection;

    importjava.sql.DatabaseMetaData;

    importjava.sql.DriverManager;

    importjava.sql.ResultSet;

    importjava.util.ArrayList;

    importjava.util.HashMap;

    importjava.util.Iterator;

    importjava.util.Map;

    importorg.apache.log4j.Logger;

    importcom.alibaba.fastjson.JSON;

    publicclassSchemaDetailsTest {

    privatestaticfinalLoggerlog= Logger.getLogger(SchemaDetailsTest.class);

    publicstaticvoidmain(String[]args)throwsException {

    StringdatabaseName="members";

    StringuserName="haowa";

    Stringpassword="haowa";

    StringmySQLPort="6307";

    StringhostUrl="172.16.177.139";

    SchemaDetailsTestddd=newSchemaDetailsTest();

    Map>PAY_TYPE=ddd.getAllColumn(databaseName,userName,password,mySQLPort,

    hostUrl);

    System.out.println(JSON.toJSONString(PAY_TYPE,true));

    }

    publicMap> getAllColumn(StringdatabaseName, StringuserName, Stringpassword,

    StringmySQLPort, StringhostUrl)throwsException {

    Map>PAY_TYPE=newHashMap>();

    // Setup the connection with the DB

    Class.forName("com.mysql.jdbc.Driver");

    Connectionconn= DriverManager.getConnection("jdbc:mysql://"+hostUrl+":"+mySQLPort,userName,password);

    // --- LISTING DATABASE SCHEMA NAMES ---

    ResultSetresultSet=conn.getMetaData().getCatalogs();

    // --- LISTING DATABASE TABLE NAMES ---

    String[]types= {"TABLE"};

    resultSet=conn.getMetaData().getTables(databaseName,null,"%",types);

    StringtableName="";

    while(resultSet.next()) {

    tableName=resultSet.getString(3);

    PAY_TYPE.put(tableName,newArrayList());

    }

    resultSet.close();

    // --- LISTING DATABASE COLUMN NAMES ---

    DatabaseMetaDatameta=conn.getMetaData();

    Iteratoriiiiii=PAY_TYPE.keySet().iterator();

    while(iiiiii.hasNext()) {

    StringtempTable=iiiiii.next();

    resultSet=meta.getColumns(databaseName,null,tempTable,"%");

    while(resultSet.next()) {

    StringColumn_Name=resultSet.getString(4);

    PAY_TYPE.get(tempTable).add(Column_Name);

    }

    }

    resultSet.close();

    conn.close();

    returnPAY_TYPE;

    }

    }

    相关文章

      网友评论

          本文标题:获取MySQL数据库所有表的列名

          本文链接:https://www.haomeiwen.com/subject/dctvextx.html