在MySQL数据库中显示表名及其记录计数

时间:2014-05-26 16:51:02

标签: java mysql netbeans

我正在进行类分配的下一步,我想做一个查询,我从数据库中提取元数据以列出其中的表,然后包括表中的记录数。我有它的地方有点工作,但在上半部分显示它只放一个数字1记录的数量应该去。在下半部分(我现在正在为测试目的这样做,如果我可以让它工作,它将只有前半部分)它打印出的记录数量很好,但我的表名是硬编码的。我想要它能够从元数据结果中读取表名,然后执行查询记录计数。我在带有MySQL数据库的netbeans中运行它并使用JDBC驱动程序。

connect();

             try
                {     
                    String db = conn.getCatalog();
                    data.append("The current database " + db + " contains the following tables:\r\n");
                }
             catch(Exception err)
                {
                    System.out.print(err + "\r\n");
                }         

             try
                {   
                    ResultSet rs = null;
                    DatabaseMetaData md = conn.getMetaData();
                    rs = md.getTables(null, null, "%", null);
                        while (rs.next()) 
                        { 
                            String tbl = rs.getString(3);
                            System.out.print(tbl + "\r\n");
                            ResultSet ct = null;
                            ct = conn.createStatement().executeQuery("SELECT COUNT(*) FROM" + tbl);

                            while (ct.next())
                             {
                                 System.out.print(ct.getString(1) + "\r\n");
                                 data.append("Table \"" + rs.getString(3) + "\" contains " + ct.getString(1) + " records.\r\n");
                             }

                        }

                    data.append("\r\n");

                }
             catch(Exception err)
                 {
                     System.out.print(err + "\r\n");
                 }

             try
                 {
                     ResultSet ct = null;
                     ct = conn.createStatement().executeQuery("SELECT COUNT(*) FROM customer");
                     while (ct.next())
                     {
                         data.append(ct.getString(1) + "\r\n");
                     }

                     ResultSet pt = null;
                     pt = conn.createStatement().executeQuery("SELECT COUNT(*) FROM product");
                     while (pt.next())
                     {
                         data.append(pt.getString(1) + "\r\n");
                     }

                 }

             catch(Exception err)
                 {
                     System.out.print(err + "\r\n");
                 }

2 个答案:

答案 0 :(得分:1)

使用以下查询,您可以查询特定数据库中包含行数,日期大小,索引大小等的所有表。

SELECT concat(table_schema,'.',table_name) tables,
concat(round(table_rows),'') rows,
concat(round(data_length/(1024),2),'') data_size,
concat(round(index_length/(1024),2),'') index_size,
concat(round((data_length+index_length)/(1024),2),'') total_size,
round(index_length/data_length,2) index_data_ratio
FROM information_schema.TABLES
WHERE table_schema like "%YOUR-DATABASE-NAME-HERE%"
ORDER BY rows DESC LIMIT 20;

答案 1 :(得分:-1)

这就是我最终做的事情,它完全符合我的要求。我最终在while循环中使用了一个准备好的语句来使它工作。

connect();

             try
                {     
                    String db = conn.getCatalog();
                    data.append("The current database " + db + " contains the following tables:\r\n");
                }
             catch(Exception err)
                {
                    System.out.print(err + "\r\n");
                }         

             try
                {
                    ResultSet rs = null;
                    DatabaseMetaData md = conn.getMetaData();
                    rs = md.getTables(null, null, "%", null);

                    while (rs.next()) 
                        { 
                            String tbl = rs.getString(3);
                            String query = ("SELECT COUNT(*) FROM " + tbl);   
                            PreparedStatement cnt = conn.prepareStatement(query);                         
                            ResultSet ct = cnt.executeQuery();
                            while (ct.next())
                            {
                                data.append("Table " + rs.getString(3) + " has " + ct.getString(1) + " records\r\n");
                            }  
                        }

                    data.append("\r\n");
                }

             catch(Exception err)
                {
                    System.out.print(err + "\r\n");
                }