Exemplo n.º 1
0
 /**
  * 获得数据库中所有表的集合
  *
  * @param DBName 数据库名,区分大小写
  * @return
  * @throws Exception
  */
 public static List<Table> getTables(String DBName) throws Exception {
   String sql =
       "select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLUMN_DEFAULT,"
           + "IS_NULLABLE,DATA_TYPE,COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT "
           + "from information_schema.columns where table_schema = '"
           + DBName
           + "'";
   PreparedStatement ps = DBUtil.getPS(sql);
   ResultSet rs = ps.executeQuery();
   List<Table> list = new ArrayList<Table>();
   List<TableStructure> colNames = new ArrayList<TableStructure>();
   String tableName = "";
   while (rs.next()) {
     TableStructure ts = new TableStructure();
     java.lang.reflect.Field[] fields = ts.getClass().getDeclaredFields();
     for (java.lang.reflect.Field f : fields) {
       Type type = f.getType();
       String name = f.getName();
       Method m = ts.getClass().getDeclaredMethod("set" + MyUtil.initcap(name), (Class<?>) type);
       m.invoke(ts, rs.getString(name));
     }
     if (!tableName.equalsIgnoreCase(ts.getTable_name()) && tableName != "") {
       Table table = new Table();
       table.setDBName(DBName);
       table.setName(tableName);
       table.setTableStructures(colNames);
       list.add(table);
       colNames = new ArrayList<TableStructure>();
     }
     colNames.add(ts);
     tableName = ts.getTable_name();
   }
   DBUtil.closeCon();
   return list;
 }
Exemplo n.º 2
0
 // 新版开始
 private void getColumn(String tableName, Object... className) throws Exception {
   String sql =
       "select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLUMN_DEFAULT,"
           + "IS_NULLABLE,DATA_TYPE,COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT "
           + "from information_schema.columns where table_name='"
           + tableName
           + "' and table_schema = '"
           + DBName
           + "'";
   PreparedStatement ps = DBUtil.getPS(sql);
   ResultSet rs = ps.executeQuery();
   while (rs.next()) {
     TableStructure ts = new TableStructure();
     java.lang.reflect.Field[] fields = ts.getClass().getDeclaredFields();
     for (java.lang.reflect.Field f : fields) {
       Type type = f.getType();
       String name = f.getName();
       Method m = ts.getClass().getDeclaredMethod("set" + MyUtil.initcap(name), (Class<?>) type);
       m.invoke(ts, rs.getString(name));
     }
     colNames.add(ts);
   }
   giveValue(tableName, className);
   DBUtil.closeCon();
 }