データベースのテーブル構造を取得する方法。

1. 注入中にこれら 2 つのテンプレートを初期化します。

コードをコピーする
    /** 
     * Spring 構成ファイルで構成されたデータ ソースを挿入します。
     * 注入時にこれら 2 つのテンプレートを初期化します。
     * @param dataSource 
     * メソッド作成者: yanwei 
     * メソッド作成日時: 2011-11-2 03:43 PM :13 
     * メソッド更新作成者: 
     * メソッド更新日時: 
     */ 
    @Resource public void setDataSource(DataSource dataSource) {
         this .dataSource = dataSource; 
        jdbcTemplate = new JdbcTemplate(dataSource); 
        simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource); 
    }
     
コードをコピーする

2. テーブル構造情報を取得します。

コードをコピーする
1  /** 2      * テーブル構造情報の取得
3      * @param tableName テーブル名
4      * @return 5      * @throws例外
6      * メソッド作成者: yanwei
 7      * メソッド作成日時: 2011-12-21 01:01:17 PM
 8      * メソッド更新作成者:
 9      * メソッド更新日時:
 10 */ 11 public List<DsClientColumnInfo> getDsTableColumnInfo(String tableName) throws DataAccessFailureException{
 12 13          ResultSet resultSet = null ;

   
      
              
14         接続 connection = null ;
 15          java.util.List<DsClientColumnInfo> clientTableInfos = new ArrayList<DsClientColumnInfo>();
 16          try {
 17              connection = this .jdbcTemplate.getDataSource().getConnection();
 18              //列情報を取得する19              resultSet = connection.getMetaData().getColumns( null , null , tableName, null );
 20 while (resultSet.next()) {
 21 //フィールド名を取得する22
                               
                  String name = resultSet.getString("COLUMN_NAME");
 23                   //フィールド型名を取得する24                   String type = resultSet.getString("TYPE_NAME");
 25 //フィールドサイズを取得する26 int size = resultSet.getInt("COLUMN_SIZE") ") ;
 27 //フィールドの注釈を取得します28                   String respect = resultSet.getString("REMARKS");
 29                   DsClientColumnInfo info = new DsClientColumnInfo( null , null , null , name, 備考, size, type, "false");
 30                   clientTableInfos .add (情報);
 31
                  
                                    
             }
 32              
33              //主キー情報の取得34              resultSet = connection.getMetaData().getPrimaryKeys( null , null , tableName);
 35 while (resultSet.next()){
 36                   String PrimaryKey = resultSet.getString("COLUMN_NAME");
 37 //主キーかどうかを設定します38 for (DsClientColumnInfo dsClientColumnInfo : clientTableInfos) {
 39 if (primaryKey != null && PrimaryKey.equals(dsClientColumnInfo.getClientColumnCode()))
 40                          dsClientColumnInfo.setIsParmaryKey("true");
 41
                               
                                                            else  
42                          dsClientColumnInfo.setIsParmaryKey("false");
 43                  }
 44              }
 45              
46              //外部キー情報を取得する47              resultSet = connection.getMetaData().get輸入キーs( null , null , tableName);
 48 while (resultSet.next()) {
 49                  String exportedKey = resultSet.getString("FKCOLUMN_NAME");
 50 //外部キーかどうかを設定51 for (DsClientColumnInfo dsClientColumnInfo : clientTableInfos) {
 52 if (exportedKey != null
                              
                                           &&exportedKey.equals(dsClientColumnInfo.getClientColumnCode()))
 53                              dsClientColumnInfo.setIs ImportKey("true");
 54                          else  
55                              dsClientColumnInfo.setIs輸入キー("false");
 56                  }
 57              }
 58              
59              
60          } catch (例外 e) {
 6              1e .printStackTrace();
 62              throw  new RuntimeException("フィールド情報の取得に失敗しました。管理者に問題を報告してください。" + e.getMessage(), e); 63 }
 finally          { 64 if
 (              resultSet !=null )
 65                  try {
 66                      resultSet.close();
 67                  } catch (SQLException e) {
 68                      e.printStackTrace();
 69                         throw  new DataAccessFailureException("結果セット resultSet を閉じることができませんでした。",e);
 70                  } finally {
 71                      if (接続 != null )
 72                          try {
 73                              connection.close();
 74                          } catch (SQLException e) {
 75                             e.printStackTrace();
 76                                 throw  new DataAccessFailureException("接続接続を閉じることができませんでした。",e);
 77                          }
 78                  }
 79          }
 80          
81          Set set = new HashSet();
 82          set.addAll(clientTableInfos);
 83          clientTableInfos。クリア ();
 84          clientTableInfos.addAll(set);
 85          return clientTableInfos;
 86      }
コードをコピーする

3. データベース内のすべてのテーブルを取得します。

コードをコピーする
 1 /**
 2      * 获得数据库中所有的表
 3      * @return
 4      * Method create author: yanwei
 5      * Method create dateTime: 2012-1-5 上午11:23:54
 6      * Method update author:
 7      * Method update dateTime:
 8      * @throws SQLException 
 9 */
10     public Map<String, String> getDatabaseTables() throws DataAccessFailureException{
11         ResultSet resultSet = null;
12         Connection connection = null;
13         Map<String, String> map = new HashMap<String, String>();
14         try {
15              String[]   types   =   {"TABLE"};    
16             connection = this.jdbcTemplate.getDataSource().getConnection();
17             String databaseName = SynXmlAnalysis.getElementValueByName(DATABASE_NAME);
18             resultSet = connection.getMetaData().getTables(null, databaseName, null, types);
19             while(resultSet.next()){
20                 String tableName = resultSet.getString("TABLE_NAME");
21                 String remark = resultSet.getString("REMARKS");
22                 map.put(tableName, remark);
23             }
24         } catch (SQLException e) {
25             e.printStackTrace();
26             throw new DataAccessFailureException(e);
27         }catch (Exception e) {
28             e.printStackTrace();
29         }finally{
30             if(resultSet != null)
31                 try {
32                     resultSet.close();
33                 } catch (SQLException e) {
34                     e.printStackTrace();
35                        throw new DataAccessFailureException("关闭结果集resultSet失败。",e);
36                 }finally{
37                     if(connection != null)
38                         try {
39                             connection.close();
40                         } catch (SQLException e) {
41                             e.printStackTrace();
42                                throw new DataAccessFailureException("接続を閉じることができませんでした。",e);
 43                          }
 44                  }
 45          
46          }
 47          return map;
 48      }
コードをコピーする

おすすめ

転載: blog.csdn.net/linwei_hello/article/details/21639657