oracle - Java Class for retrieve all tables from database -
this question exact duplicate of:
i wrote java class print tables of db2 database,but problem is: not print columns(i wrote hints in code). , every time print previous values too!
public class automateexport { static string value; public static void main(string[] args) throws sqlexception, classnotfoundexception { string table_name; string column_name; string tablename = null; string columntype; int precision; stringbuilder sb = new stringbuilder(1024); connection db2 = getconnection(); string sql = "select tabschema,tabname,colname,typename,length syscat.columns tabschema not 'sys%' "; preparedstatement mainstmt = db2.preparestatement(sql); resultset rs = mainstmt.executequery(); resultsetmetadata rsmd = rs.getmetadata(); int columncount = rsmd.getcolumncount(); sb.append("create table").append(" "); rs.next();// if comment line not work correctly.but not contain 1 column of table table_name = rs.getstring(2); sb.append(table_name).append("("); string str1 = "admin2"; while (rs.next()) { table_name = rs.getstring(2); if (table_name.equalsignorecase(str1)) { column_name = rs.getstring(3); columntype = rs.getstring(4); sb.append(" ").append(column_name).append(" ").append(columntype); precision = rs.getint(5); if (precision != 0) { sb.append("( ").append(precision).append(" )"); sb.append(", "); } } else { sb.append(" ) ").append(";"); system.out.println(sb.tostring()); str1 = str1.replaceall(str1, table_name); sb.append("create table").append(" "); sb.append(table_name).append("("); //rs.previous(); } // sb.append(" ) ").append(";"); //system.out.println(sb.tostring()); } class.forname("oracle.jdbc.driver.oracledriver"); connection codal = drivermanager.getconnection("jdbc:oracle:thin:@192.168.01.53:1521:orcl", "navid", "oracle"); /* string sql2 = sb.tostring(); preparedstatement m = db2.preparestatement(sql2); m.executequery();*/ } private static connection getconnection() throws classnotfoundexception, sqlexception { class.forname("com.ibm.db2os390.sqlj.jdbc.db2sqljdriver"); connection connection = drivermanager.getconnection("jdbc:db2://localhost:50000/navid", "navid", "oracle"); return connection; } }
the out put this:
run: create table admin2( name varchar( 30 ), role varchar( 50 ), ) ; create table admin2( name varchar( 30 ), role varchar( 50 ), ) ;create table document( id decimal( 10 ), name varchar( 50 ), ) ; build successful (total time: 1 second)
you have initialize stringbulder in while loop:
... while (rs.next()) { stringbuilder sb = new stringbuilder(1024); ...
Comments
Post a Comment