Created
August 11, 2008 22:29
-
-
Save russellkt/4962 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.sql.* | |
| import com.ibm.as400.access.* | |
| class DbMetaClass{ | |
| Connection c = null | |
| DatabaseMetaData dbMeta = null | |
| String[] types =["TABLE", "VIEW"] | |
| DbMetaClass(){ | |
| Class.forName("com.ibm.as400.access.AS400JDBCDriver") | |
| this.c = DriverManager.getConnection("jdbc:as400:192.168.2.1","user","pass") | |
| this.dbMeta = c.getMetaData() | |
| } | |
| void printTableInfo(){ | |
| while( tables.next() ){ | |
| println("${tables.getString(1)},${tables.getString(2)},${tables.getString(3)}") | |
| def columns = dbMeta.getColumns(null,schema,tables.getString(3),null) | |
| while(columns.next()){ | |
| columns.metaData.columnCount.times{ | |
| println("\t${columns.metaData.getColumnName(it+1)}\t${columns.getString(it+1)}") | |
| } | |
| } | |
| } | |
| } | |
| void toCsv(String filename, ResultSet results){ | |
| new Csv().write(filename, results, null) | |
| } | |
| ResultSet tablesIn(String schema, String pattern){ | |
| return dbMeta.getTables(null, schema, pattern, types) | |
| } | |
| ResultSet columnsIn(String schema){ | |
| return dbMeta.getColumns(null, schema, "%", null) | |
| } | |
| ResultSet indexesIn(String schema, String table){ | |
| return dbMeta.getIndexInfo(null,schema,table,false,false) | |
| } | |
| ResultSet dataIn(schema,table){ | |
| Statement statement = c.createStatement() | |
| return statement.executeQuery("SELECT * FROM ${schema}.${table}") | |
| } | |
| void writeAllTableDataToCsvFiles(path,schema){ | |
| ResultSet tables = tablesIn(schema,"%") | |
| while( tables.next() ){ | |
| def tableName = tables.getString(3).trim() | |
| def schemaName = tables.getString(2).trim() | |
| println tableName | |
| try{ | |
| ResultSet data = dataIn(schemaName,tableName) | |
| toCsv(path,data) | |
| } catch(Exception e){ | |
| println e.message | |
| continue | |
| } | |
| } | |
| } | |
| void writeSchemaToCsvFile(path,schema){ | |
| def columns = columnsIn(schema) | |
| toCsv(path,columns) | |
| } | |
| void close(){ | |
| this.c.close() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment