【问题标题】:Getting table names of all tables in a particular database in MS SQL Server using Java使用 Java 在 MS SQL Server 中获取特定数据库中所有表的表名
【发布时间】:2017-05-22 16:22:47
【问题描述】:

我正在尝试使用 Java 获取数据库中所有表的名称。

这是我正在使用的方法。但是,在运行它时,它还会列出其他实际上不属于我的数据库的表,或者可能是一些我不感兴趣的系统表。我怎样才能只获取我创建的表?

/* connecting to database using supplied credentials, and printing out the SQL queries for inserting data to local db */
    public void connectToAzure(String connectionString, String databaseName, String username, String password) throws ClassNotFoundException, SQLException
    {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  

        final Connection m_Connection = DriverManager.getConnection( connectionString+";DatabaseName="+databaseName, username, password );
        final ArrayList<String> tables = getTableNames(m_Connection);
    }

/* helper method to get table names */
    public ArrayList<String> getTableNames(final Connection m_Connection)
    {
        final ArrayList<String> tables = new ArrayList<>();
        try
        {
            DatabaseMetaData dbmd = m_Connection.getMetaData();
            String[] types = {"TABLE"};
            ResultSet rs = dbmd.getTables(null, null, "%", types);
            while (rs.next())
            {
                String tableName = rs.getString("TABLE_NAME");
                tables.add( tableName );
            }
        } 
        catch (SQLException e) 
        {
            e.printStackTrace();
        }

        return tables;
    }

上面运行的输出

table1, table2, table3, table4, table5, table6, trace_xe_action_map, trace_xe_event_map

其中,

trace_xe_action_map, trace_xe_event_map

不是我创建的表。

【问题讨论】:

    标签: java sql-server database jdbc


    【解决方案1】:

    您可能希望像这样在数据库上运行一个简单的选择查询:

    select TABLE_NAME from INFORMATION_SCHEMA.TABLES;
    

    您可以使用以下 where 子句进一步过滤掉 table_names:

    SELECT 
        TABLE_NAME
    FROM
        INFORMATION_SCHEMA.TABLES
    WHERE
        TABLE_CATALOG = ? AND TABLE_SCHEMA = ?;
    

    【讨论】:

    • 这就是DatabaseMetaData.getTables 的用途;在具有 SQL 标准信息模式的数据库系统中,这可能是被查询的同一个表,因为 DatabaseMetaData.getTables 是在信息模式上建模的。
    • @GurV,谢谢我使用了以下内容,并让它工作。 select * from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = 'dbo';
    • 这仅对 SQL Server 有效。一旦您的应用程序需要在另一个数据库上运行,它就无法工作。我建议对 JDBCs DataBaseMetaData 使用与 DB 无关的方式。
    【解决方案2】:

    来自the Java API Doc

    ResultSet getTables(String catalog,
                  String schemaPattern,
                  String tableNamePattern,
                  String[] types) throws SQLException
    

    由于您只需要来自数据库的结果,因此请尝试提供“schemaPattern”,如下所示:

    ResultSet rs = dbmd.getTables(null, 'your-database', "%", types);
    

    你得到你想要的,不多不少。

    【讨论】:

      【解决方案3】:

      我对Java一无所知,但你绝对可以使用SQL。

      USE your_DB
      GO
      SELECT t.name AS table_name,
      SCHEMA_NAME(schema_id) AS schema_name,
      c.name AS column_name
      FROM sys.tables AS t
      INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
      WHERE c.name LIKE '%ticker%'
      ORDER BY schema_name, table_name;
      

      select * from information_schema.columns
      where table_schema = 'your_DB'
      order by table_name,ordinal_position
      

      【讨论】:

      • 当 OP 是关于 JDBC 时,这是特定于 MS SQL Server 的
      【解决方案4】:

      试试这个:

      ResultSetMetaData rsmd = rs.getMetaData();
                  DatabaseMetaData md = connection.getMetaData();
                  String[] types = {"TABLE"};
                  ResultSet RS = md.getTables(null, null, "%", types);
                  while (RS.next()) {
                      String table = RS.getString("TABLE_NAME");
                      if (!table.equals("trace_xe_action_map")
                      && !table.equals("trace_xe_event_map"))
                      System.out.println(table);
                  }
      

      【讨论】:

        猜你喜欢
        • 2017-03-09
        • 2021-12-18
        • 2012-10-08
        • 1970-01-01
        • 2011-04-24
        • 2015-04-05
        • 1970-01-01
        • 1970-01-01
        • 2012-02-14
        相关资源
        最近更新 更多