JDBC获取数据库所有表

当初在JDBC还没研究透的时候,就偷懒去用了hibernate,现在回过头来做jdbc的开发,发现自己真的了解的太少了,惭愧惭愧。

public   static   List   getAllTableName(Connection   cnn)   throws   SQLException{  
  List   tables   =   new   ArrayList();  
   
  DatabaseMetaData   dbMetaData   =   cnn.getMetaData();  
   
  //可为:"TABLE",   "VIEW",   "SYSTEM   TABLE",    
  //"GLOBAL   TEMPORARY",   "LOCAL   TEMPORARY",   "ALIAS",   "SYNONYM"  
  String[]   types   =   {"TABLE"};  
   
  ResultSet   tabs   =   dbMetaData.getTables(null,   null,   null,types/*只要表就好了*/);  
  /*记录集的结构如下:  
      TABLE_CAT       String   =>   table   catalog   (may   be   null)    
      TABLE_SCHEM   String   =>   table   schema   (may   be   null)    
      TABLE_NAME     String   =>   table   name    
      TABLE_TYPE     String   =>   table   type.    
      REMARKS           String   =>   explanatory   comment   on   the   table    
      TYPE_CAT         String   =>   the   types   catalog   (may   be   null)    
      TYPE_SCHEM     String   =>   the   types   schema   (may   be   null)    
      TYPE_NAME       String   =>   type   name   (may   be   null)    
      SELF_REFERENCING_COL_NAME   String   =>   name   of   the   designated   "identifier"   column   of   a   typed   table   (may   be   null)    
      REF_GENERATION   String   =>   specifies   how   values   in   SELF_REFERENCING_COL_NAME   are   created.   Values   are   "SYSTEM",   "USER",   "DERIVED".   (may   be   null)    
    */  
  while(tabs.next()){  
  //只要表名这一列  
  tables.add(tabs.getObject("TABLE_NAME"));  
   
  }  
  System.out.println(tables);  
  return   tables;  
   
  }
 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值