简体   繁体   English

如何通过Java使用“显示表​​”列出多个数据库及其表?

[英]How do I list multiple databases with their tables by using “Show Tables” via Java?

I am trying to list MySQL databases and their tables with Java. 我试图用Java列出MySQL数据库及其表。 For now, I have two databases as "Database_Services with MySQL_Database_Service , MSSQL_Database_Service , and Directory_Services with Active_Directory , OpenLDAP tables. I get the output for Database_Services and its tables but I do not get the other ones. 现在,我有两个数据库,分别是带有MySQL_Database_Service的 Database_ServicesMSSQL_Database_Service和带有Active_Directory的 Directory_ServicesOpenLDAP表。我获得了Database_Services及其表的输出,但是没有得到其他数据库

public class connectMySQL implements serverConnection{
Connection conn;
Statement stmt;
public void connect(String dbName){
    String url;
    try {
        if(dbName.equals("")){
            url = "jdbc:mysql://x:x/";
        }
        else{
            url = "jdbc:mysql://x:x”+ dbName;
        }
        String username = “x”;
        String password = "x";
        conn =  DriverManager.getConnection(url,username,password);
        stmt = conn.createStatement();
    }
    catch (SQLException ex)
    {
        System.out.println("An error occurred. Maybe user/password is invalid");
        ex.printStackTrace();
    }
}

} }

public class listInf extends connectMySQL implements listInfrastructure {
public void list() {
    String dbName;
    ResultSet rs;
    try{
        connect("");
        String str = "SHOW DATABASES";
        ResultSet resultSet = stmt.executeQuery(str);
        while(resultSet.next()){
            dbName = resultSet.getString("Database");
            if(!dbName.contains("schema") && !dbName.equals("mysql")){
                System.out.println(dbName);
                rs = stmt.executeQuery("SHOW TABLES IN " + dbName);
                while (rs.next()) {
                    System.out.println("\t" + rs.getString("Tables_in_" + dbName));
                }
            }
        }
    }
    catch(SQLException e){
        System.out.println("Error");
    }
}

} }

I want to get an output like: 我想得到类似的输出:

Database_Services: Database_Services:

  1. MySQL_Database_Service. MySQL_Database_Service。
  2. MSSQL_Database_Service. MSSQL_Database_Service。

Directory_Services: Directory_Services:

  1. Active_Directory_Service. Active_Directory_Service。

  2. OpenLDAP_Service. OpenLDAP_Service。

You are using the same Statement for multiple queries. 您将同Statement用于多个查询。 You cannot do that. 你不能这样做。 From the Javadoc of Statement : StatementJavadoc中

By default, only one ResultSet object per Statement object can be open at the same time. 默认情况下,每个Statement对象只能同时打开一个ResultSet对象。 Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. 因此,如果一个ResultSet对象的读取与另一个对象的读取是交错的,则每个都必须由不同的Statement对象生成。 All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists. 如果存在打开的语句,Statement接口中的所有执行方法都会隐式关闭该语句的当前ResultSet对象。

Connection conn1 = DriverManager.getConnection(url, username, password);
Connection conn2 = DriverManager.getConnection(url, username, password);

Statement statement1 = conn1.createStatement();
Statement statement2 = conn2.createStatement();

ResultSet resultSet1 = statement1.executeQuery("SHOW TABLES IN DB1");
ResultSet resultSet2 = statement2.executeQuery("SHOW TABLES IN DB2");

while (resultSet1.next()) {
   System.out.println("");
}

while (resultSet2.next()) {
   System.out.println("");
}

if you have more than 2 database, then simply you can use loop for to get the results. 如果您有两个以上的数据库,则只需使用loop即可获取结果。

You can use the meta information database information_schema . 您可以使用元信息数据库information_schema

SELECT 
    TABLE_SCHEMA,
    TABLE_NAME 

FROM information_schema.TABLES

WHERE TABLE_SCHEMA IN ('Database_Services', 'Directory_Services')

ORDER BY TABLE_SCHEMA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM