<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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.tkk7.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 山風小子 閱讀(4083) 評論(2)  編輯  收藏 所屬分類: Groovy & Grails
    主站蜘蛛池模板: 一级毛片免费全部播放| 国产精品亚洲专区一区| 在线观看视频免费国语| 中文字幕亚洲精品| 国产曰批免费视频播放免费s| 免费无码又爽又刺激高潮| 亚洲精品午夜无码专区| 中国黄色免费网站| 亚洲AV无码不卡无码| 亚洲AV无码资源在线观看| 国产传媒在线观看视频免费观看| 午夜亚洲国产精品福利| 4338×亚洲全国最大色成网站| 亚洲精品美女久久久久9999| 一本久久免费视频| 无码人妻精品中文字幕免费东京热| 亚洲国产中文在线视频| 亚洲欧洲免费视频| 亚洲一卡二卡三卡| 国产人妖ts在线观看免费视频| 国产精品偷伦视频免费观看了| 国产美女无遮挡免费视频网站 | 日韩中文字幕在线免费观看| 含羞草国产亚洲精品岁国产精品 | 无码av免费一区二区三区试看| 亚洲视频在线观看一区| v片免费在线观看| 国产亚洲婷婷香蕉久久精品| 97公开免费视频| 亚洲啪啪综合AV一区| 免费国产黄网站在线观看动图| 女人张腿给男人桶视频免费版| 免费大片av手机看片高清| 久久精品国产亚洲AV网站| 无遮免费网站在线入口| 成人免费夜片在线观看| 亚洲第一永久在线观看| 无码欧精品亚洲日韩一区夜夜嗨| 久久国产亚洲精品| 成在人线AV无码免费| 亚洲乱亚洲乱妇24p|