首先查詢所有表的owner,table_name,comments(說明,注解),其中owner 就是自己要查詢的分類
select tcom.OWNER, tcom.TABLE_NAME,tcom.COMMENTS from all_tab_comments tcom where owner='SADMIN' and table_type='TABLE';
要統計每個表的記錄數,可以進行嵌套查詢,首先選出所有的表的名字,然后在count 記錄的條數。簡單說下里面會遇到的問題:
1.內層的循環在進行的時候,里面的ResultSet 也要進行next(),不然會報錯。
2.如果數據量太大,內層循環時,會報 數據庫游標已使用最大 錯誤。這時候,應該把內層循環抽為一個方法,在對應的方法里面進行查詢,並查詢完畢后,對ResultSet 和PreparedStatement 進行關閉。
String sql ="*********"; pdst = conn.prepareStatement(sql); // 所有表的名字和說明的結果 result = pdst.executeQuery(); String total; //每張表的記錄條數 while(result.next()){ //要查詢的表名 String tableName=result.getString("OWNER")+"."+result.getString("TABLE_NAME"); String sql_num = "select count(*) from "+tableName; // 調用方法,避免出現游標不夠用的情況 total = executeSql(conn, sql_num,"count(*)"); } } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(conn, pdst, result); } ////////////////////////////////////////////////////////////// /** * 執行內層循環里面的sql語句,避免出現游標不夠用的情況 * @param con 與數據庫的連接 * @param sql sql語句 * @param getStr 要返回的數據對應的數據表中的字段 * @return 查詢結果里面對應的字段的值 */ public static String executeSql(Connection con,String sql,String getStr){ //查詢方法 PreparedStatement ps =null; ResultSet result1 =null; String str=null; try { ps = con.prepareStatement(sql); result1 = ps.executeQuery(); if(result1.next()){ str = result1.getString(getStr); } } catch (SQLException e) { e.printStackTrace(); } finally { if (ps != null){ try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if (result1 != null){ try { result1.close(); } catch (SQLException e) { e.printStackTrace(); } } } return str; }