知识大全 Groovy高效编程—生成Oracle表结构信息文档
Posted 知
篇首语:宁可身冷,不可心冷;宁可人穷,不可志穷。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Groovy高效编程—生成Oracle表结构信息文档相关的知识,希望对你有一定的参考价值。
Groovy高效编程—生成Oracle表结构信息文档 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
利用Groovy对数据库进行操作是极其方便的 有时为了熟悉数据库中的表 需要将表结构导出 并保存为EXCEL格式
下面所展示的源代码就能够很好的满足我们的需求 (这段代码依赖jxl和Oracle的jdbc驱动)
功能保持不变的条件下 代码做了一些小调整 利用Groovy中的强大特性Mixin 使代码更优雅
导出效果
conf properties filename=table_structures xls tables to export=% column width= url=jdbc:oracle:thin:@ : :orcl user=DANIEL password= driver=oracle jdbc driver OracleDriver GroovySql groovy /* * Copyright the original author or authors * * Licensed under the Apache License Version (the License ); * you may not use this file except in pliance with the License * You may obtain a copy of the License at * * * Unless required by applicable law or agreed to in writing sofare * 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 * * Auth */ 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 lumn_name (select nstraint_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 lumn_name = lumn_name and nstraint_name = nstraint_name and nstraint_type = P and rownum = ) as constraint_type a data_type a data_length a data_precision a data_scale a nullable a data_default ments ments as tab_ments from all_tab_columns a all_col_ments b all_tab_ments 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 lumn_name = lumn_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 lumn_name); column put( constraint_type nstraint_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( ments ments); String tableName = row table_name String tableComments = row tab_ments Set columns = tables get(tableName) ?lumns 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( 第一页 ) WritableFont writableFontForTableName = new WritableFont(WritableFont TIMES 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 WritableFont NO_BOLD) WritableCellFormat writableCellFormatForTableComments = new WritableCellFormat(writableFontForTableComments) // writableCellFormatForTableComments setAlignment(jxl format Alignment CENTRE) writableCellFormatForTableComments setVerticalAlignment(jxl format VerticalAlignment CENTRE) int line = List titleRow = [ 表名 表注释 字段名称 是否主键 字段类型 字段长度 整数位数 小数位数 允许空值 缺省值 字段注释 ] try columnWidth = Integer parseInt(columnWidth) catch (Exception e) columnWidth = System err println(e getMessage()) for ( int i = ; i < titleRow size(); i ++ ) writableSheet setColumnView(i columnWidth) use (WritableSheetCategory) writableSheet insertRow(titleRow line ++ ) tables each tableName tableInfo > String tableComments = tableInfo tableComments Set columns = lumns Label tableNameCell = new Label( line tableName writableCellFormatForTableName) writableSheet addCell(tableNameCell) Label tableCommentsCell = new Label( line tableComments ? + tableComments : writableCellFormatForTableComments) writableSheet addCell(tableCommentsCell) columns each column > List row = [ lumn_name ? + lumn_name : nstraint_type ? + nstraint_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 : ments ? + ments : ] use (WritableSheetCategory) writableSheet insertRow(row line ++ ) rgeCells( line columns size() line ) rgeCells( line columns size() line ) line += writableWorkBook write(); writableWorkBook close(); println done!
cha138/Article/program/Java/hx/201311/26135相关参考
首先了解什么是外部表与其它表的区别建立一个简单的外部表(主要看操作过程)最后我们用外部表查看Oracle报警日志 一了解oracle外部表 外部表定义结构被存放在数据字典而表数据被放在OS文件
Groovy编程动态改变对象的能力 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 从Groovy
Groovy编程—Annotations的使用 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 从
Groovy编程——Mixin的使用 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 由于Java
Groovy编程用Closure替代匿名类 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 由于内
对组合列创建统计信息 检查表结构 select*fromscottempwhere=; 创建虚拟列会输出虚拟列名 selectdbms_statscreate_extended_stats
查询Oracle数据库表空间信息的方法 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 表空间是数
Oracle内存结构:SGA的区域信息 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! SGA的区
在cmd中输入expusername/password@连接串 回车在进入如下时输入no就ok了 导出表数据(yes/no)yes>no 进入plsql 找到table那个大项点
通过plsql打开对应的数据库 选择菜单tools下的exportusersobjects子菜单 选中需要导出表的表名称然后选择保存后的路径 单击export按钮即可导出选中的导出的表 导