First they ignore you
          then they ridicule you
          then they fight you
          then you win
              -- Mahatma Gandhi
          Chinese => English     英文 => 中文             
          隨筆-221  評論-1047  文章-0  trackbacks-0
          利用Groovy對數據庫進行操作是極其方便的,有時為了熟悉數據庫中的表,需要將表結構導出,并保存為EXCEL格式。
          下面所展示的源代碼就能夠很好的滿足我們的需求。(這段代碼依賴jxl和Oracle的jdbc驅動)


          功能保持不變的條件下,代碼做了一些小調整,利用Groovy中的強大特性Mixin,使代碼更優雅。

          導出效果:
          表名 表注釋 字段名稱 是否主鍵 字段類型 字段長度 整數位數 小數位數 允許空值 缺省值 字段注釋
          CUSTOMER 用戶表 USER_ID P VARCHAR2 10

          N
          客戶ID
          USER_BALANCE
          NUMBER 22 18 2 N 0 客戶余額
          USER_GENDER
          VARCHAR2 10

          Y
          客戶性別
          USER_BIRTHDAY
          DATE 7

          N
          客戶生日
          USER_NAME
          VARCHAR2 20

          N
          客戶名






















          LOG 日志表 R
          VARCHAR2 200

          Y
          結果
          D
          DATE 7

          Y
          時間


          conf.properties
          filename=table_structures.xls
          tables.to.export=%
          column.width=15
          url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
          user=DANIEL
          password=123456
          driver=oracle.jdbc.driver.OracleDriver


          GroovySql.groovy
          /*
          ?*?Copyright?2008?the?original?author?or?authors.
          ?*?
          ?*?Licensed?under?the?Apache?License,?Version?2.0?(the?"License");
          ?*?you?may?not?use?this?file?except?in?compliance?with?the?License.
          ?*?You?may?obtain?a?copy?of?the?License?at
          ?*?
          ?*???????
          http://www.apache.org/licenses/LICENSE-2.0
          ?*?
          ?*?Unless?required?by?applicable?law?or?agreed?to?in?writing,?software
          ?*?distributed?under?the?License?is?distributed?on?an?"AS?IS"?BASIS,
          ?*?WITHOUT?WARRANTIES?OR?CONDITIONS?OF?ANY?KIND,?either?express?or?implied.
          ?*?See?the?License?for?the?specific?language?governing?permissions?and
          ?*?limitations?under?the?License.
          ?*
          ?*?Author:?山風小子(
          http://www.aygfsteel.com/BlueSUN )
          ?*?Email?:?realbluesun@hotmail.com
          ?
          */ ?


          import ?groovy.sql.Sql
          import ?jxl. *
          import ?jxl.write. *

          Properties?properties?
          = ? new ?Properties();
          properties.load(
          new ?FileInputStream( " conf.properties " ));

          def?filename?
          = ?properties.getProperty( ' filename ' )
          def?tablesToExport?
          = ?properties.getProperty( ' tables.to.export ' )
          def?columnWidth?
          = ?properties.getProperty( ' column.width ' )
          def?url?
          = ?properties.getProperty( ' url ' )
          def?user?
          = ?properties.getProperty( ' user ' )
          def?password?
          = ?properties.getProperty( ' password ' )
          def?driver?
          = ??properties.getProperty( ' driver ' )

          def?sql?
          = ?Sql.newInstance(url,?user,?password,?driver)

          def?sqlStmt?
          = ? """
          ??select?
          ????????a.table_name,?
          ????????a.column_name,?
          ????????(select?
          ????????????d.constraint_type?
          ?????????from
          ????????????all_constraints?d,
          ????????????all_cons_columns?e
          ?????????where?
          ????????????c.owner?
          = ?d.owner?and
          ????????????d.owner?
          = ?e.owner?and
          ????????????c.table_name?
          = ?d.table_name?and
          ????????????d.table_name?
          = ?e.table_name?and
          ????????????b.column_name?
          = ?e.column_name?and
          ????????????d.constraint_name?
          = ?e.constraint_name?and
          ????????????d.constraint_type?
          = ? ' P ' ?and?
          ????????rownum?
          = ? 1
          ????????)?as?constraint_type,
          ????????a.data_type,?
          ????????a.data_length,?
          ????????a.data_precision,?
          ????????a.data_scale,?
          ????????a.nullable,?
          ????????a.data_default,?
          ????????b.comments,
          ????????c.comments?as?tab_comments
          ???from?
          ????????all_tab_columns?a,?
          ????????all_col_comments?b,
          ????????all_tab_comments?c
          ???where?
          ????????a.owner?
          = ?b.owner?and
          ????????b.owner?
          = ?c.owner?and
          ????????a.table_name??
          = ??b.table_name?and
          ????????b.table_name??
          = ??c.table_name?and
          ????????a.column_name??
          = ??b.column_name?and
          ????????a.table_name?like??
          ? ??and?
          ????????a.owner?
          = ? ? ?
          """

          Map?tables?
          = ? new ?HashMap()

          sql.eachRow(sqlStmt,?[tablesToExport,?user]){?row?
          ->

          ????Map?column?
          = ? new ?HashMap()
          ????column.put(
          ' column_name ' ,?row.column_name);
          ????column.put(
          ' constraint_type ' ,?row.constraint_type);
          ????column.put(
          ' data_type ' ,?row.data_type);
          ????column.put(
          ' data_length ' ,?row.data_length);
          ????column.put(
          ' data_precision ' ,?row.data_precision);
          ????column.put(
          ' data_scale ' ,?row.data_scale);
          ????column.put(
          ' nullable ' ,?row.nullable);
          ????column.put(
          ' data_default ' ,?row.data_default);
          ????column.put(
          ' comments ' ,?row.comments);


          ????String?tableName?
          = ?row.table_name
          ????String?tableComments?
          = ?row.tab_comments
          ????Set?columns?
          = ?tables.get(tableName) ? .columns
          ????
          ????
          if ?( null ? == ?columns)?{
          ????????columns?
          = ? new ?HashSet();
          ????????columns?
          << ?column
          ????????
          ????????tables.put(tableName,?[tableComments:tableComments,?columns:columns])
          ????}?
          else ?{
          ????????columns?
          << ?column
          ????}
          }

          println?
          " to?export?table?structures "

          class ?WritableSheetCategory?{
          ????
          static ?insertRow(WritableSheet?writableSheet,?List?row,? int ?x,? int ?y)?{
          ????????row.eachWithIndex?{?col,?i?
          ->
          ????????????Label?cell?
          = ? new ?Label(x? + ?i,?y,?col)
          ????????????writableSheet.addCell(cell)
          ????????}
          ????}????
          }


          WritableWorkbook?writableWorkBook?
          = ?
          ????Workbook.createWorkbook(
          ????????
          new ?File(filename))
          WritableSheet?writableSheet?
          = ?writableWorkBook.createSheet( " 第一頁 " ,? 0 )


          WritableFont?writableFontForTableName?
          =
          ????
          new ?WritableFont(WritableFont.TIMES,? 10 ,?WritableFont.BOLD)
          WritableCellFormat?writableCellFormatForTableName?
          = ?
          ????
          new ?WritableCellFormat(writableFontForTableName)
          // writableCellFormatForTableName.setAlignment(jxl.format.Alignment.CENTRE)
          writableCellFormatForTableName.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE)


          WritableFont?writableFontForTableComments?
          =
          ????
          new ?WritableFont(WritableFont.TIMES,? 10 ,?WritableFont.NO_BOLD)
          WritableCellFormat?writableCellFormatForTableComments?
          = ?
          ????
          new ?WritableCellFormat(writableFontForTableComments)
          // writableCellFormatForTableComments.setAlignment(jxl.format.Alignment.CENTRE)
          writableCellFormatForTableComments.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE)


          int ?line? = ? 0
          List?titleRow?
          = ?[
          ????
          ' 表名 ' ,? ' 表注釋 ' ,? ' 字段名稱 ' ,? ' 是否主鍵 ' ,? ' 字段類型 ' ,? ' 字段長度 ' ,? ' 整數位數 ' ,? ' 小數位數 ' ,? ' 允許空值 ' ,? ' 缺省值 ' ,? ' 字段注釋 '
          ]


          try ?{
          ????columnWidth?
          = ?Integer.parseInt(columnWidth)?
          }?
          catch ?(Exception?e)?{
          ????columnWidth?
          = ? 15
          ????System.err.println(e.getMessage())
          }
          for ?( int ?i? = ? 0 ;?i? < ?titleRow.size();?i ++ )?{
          ????writableSheet.setColumnView(i,?columnWidth)
          }

          use?(WritableSheetCategory)?{
          ????writableSheet.insertRow(titleRow,?line
          ++ ,? 0 )
          }

          tables.each?{?tableName,?tableInfo?
          ->
          ????String?tableComments?
          = ?tableInfo.tableComments
          ????Set?columns?
          = ?tableInfo.columns

          ????Label?tableNameCell?
          = ? new ?Label( 0 ,?line,?tableName,?writableCellFormatForTableName)
          ????writableSheet.addCell(tableNameCell)

          ????Label?tableCommentsCell??
          = ? new ?Label( 1 ,?line,?tableComments? ? ? "" ? + ?tableComments?:? "" ,?writableCellFormatForTableComments)
          ????writableSheet.addCell(tableCommentsCell)

          ????columns.each?{?column?
          ->
          ????????List?row?
          = ?[
          ????????????column.column_name?
          ? ? "" ? + ?column.column_name?:? "" ,
          ????????????column.constraint_type?
          ? ? "" ? + ?column.constraint_type?:? "" ,
          ????????????column.data_type?
          ? ? "" ? + ?column.data_type?:? "" ,
          ????????????column.data_length?
          ? ? "" ? + ?column.data_length?:? "" ,
          ????????????column.data_precision?
          ? ? "" ? + ?column.data_precision?:? "" ,
          ????????????column.data_scale?
          ? ? "" ? + ?column.data_scale?:? "" ,
          ????????????column.nullable?
          ? ? "" ? + ?column.nullable?:? "" ,
          ????????????column.data_default?
          ? ? "" ? + ?column.data_default?:? "" ,
          ????????????column.comments?
          ? ? "" ? + ?column.comments?:? ""
          ????????]
          ????????
          ????????use?(WritableSheetCategory)?{
          ????????????writableSheet.insertRow(row,?
          2 ,?line ++ )
          ????????}
          ????}

          ????writableSheet.mergeCells(
          0 ,?line? - ?columns.size(),? 0 ,?line? - ? 1 )
          ????writableSheet.mergeCells(
          1 ,?line? - ?columns.size(),? 1 ,?line? - ? 1 )

          ????line?
          += ? 2
          }

          writableWorkBook.write();
          writableWorkBook.close();

          println?
          " done! "



          附:朝花夕拾——Groovy & Grails
          posted on 2008-01-26 20:05 山風小子 閱讀(4088) 評論(2)  編輯  收藏 所屬分類: Groovy & Grails

          轉載文章,請保留原始鏈接和署名



          Dict.CN
          <2008年1月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(71)

          隨筆分類

          隨筆檔案

          相冊

          Documentations

          Groovy & Grails

          友情鏈接

          好友 & 鄰居

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 795912
          • 排名 - 53

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 鹤峰县| 闸北区| 尉犁县| 宿松县| 垣曲县| 临漳县| 嘉善县| 昭觉县| 靖边县| 梅河口市| 东台市| 寿宁县| 嘉禾县| 谷城县| 涡阳县| 芮城县| 东乡族自治县| 溧水县| 广平县| 平昌县| 上虞市| 迁西县| 桃江县| 赤峰市| 禄丰县| 遂平县| 博客| 衡阳县| 长白| 京山县| 祁连县| 新昌县| 金川县| 出国| 霍林郭勒市| 驻马店市| 普格县| 彭山县| 昌宁县| 威远县| 普宁市|