通过comment查找中文名的数据库表

通过comment查找中文名的数据库表

package com.augurit.pysz.common.excelUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.augur.wf.shark.common.Db.DbConnectionManager;
import com.augurit.pysz.common.excelUtil.model.TableValue;

/**
 * 从hibernate中将table的表结构找出来。 imcb 2007.6.26
 */
public class RetrieveTableContext {

 public Connection testDB() throws ClassNotFoundException, SQLException {
  Connection con = null;
  Class.forName("oracle.jdbc.driver.OracleDriver");
  con = DriverManager.getConnection(
    "jdbc:oracle:thin:@192.168.13.65:1521:pysz", "shark", "shark");
  return con;
 }

 /**
  * 通过表名查找表的英文名
  * 
  * @param dbName数据库实例名称
  * @return
  */
 public List getAllTableName(String dbName) {
  List ls = new ArrayList();
  Connection con = null;  
  try {
   // con = DbConnectionManager.getInstance().getConnection("idb");
   con = this.testDB();   
   String sql = "select * from sys.all_tab_comments t where t.table_type = 'TABLE' and t.owner = '"+dbName+"'";
   PreparedStatement psts = con.prepareStatement(sql);
   ResultSet rs = psts.executeQuery();
   int i=0;
   while(rs.next()){
    TableValue tv = new TableValue();
    System.out.println(++i+"条 :");
    System.out.print(rs.getString(1)+" : ");
    tv.setDbName(rs.getString(1));
    System.out.print(rs.getString(2)+" : ");
    tv.setTableName(rs.getString(2));
    System.out.print(rs.getString(3)+" : ");
    tv.setDbType(rs.getString(3));
    System.out.println(rs.getString(4));
    tv.setComment(rs.getString(4));
    ls.add(tv);
   }
  } catch (Exception we) {
   
  } finally {
   DbConnectionManager.getInstance().freeConnection("idb", con);
  } 

  return ls;
 }
 /**
  * 通过表名查找表的英文名
  *
  * @param str1
  * @param dbName数据库实例名称
  * @return
  */ 
 public String getTableName(String str1,String dbName){  
  String tableName = new String();
  List ls = this.getAllTableName(dbName);
  Iterator iterator =ls.iterator();
  while(iterator.hasNext()){
   TableValue tv =(TableValue)iterator.next();
   boolean hasStr = tv.getComment().contains(str1);
   if(hasStr)return tv.getTableName();
  }
  return tableName;
 }
 /**
  * 通过表名查找数据表属性值。
  *
  * @param tableName
  *            表的英文名
  * @return
  */
 public List getTableContext(String tableName) {
  List tList = new ArrayList();

  return tList;
 }
 
 public static void main(String[] str1){
//  RetrieveTableContext rtc = new RetrieveTableContext();
//  rtc.getTableName("d","SHARK");
  String u="string yu";
  String in = "s";
  String out = "e";  
  System.out.println(in.contains(in));//in是否包含in字符串
  System.out.println(u.contains(out));  
 }
}

你可能感兴趣的:(通过comment查找中文名的数据库表)