将Oracle分区表的定义导到文件中

   分区表的定义,特别是复合分区表的定义,在pl/sql developer里面是看不了的。


import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class ImportTableDefine {
    
    static final String driver_class = "oracle.jdbc.driver.OracleDriver";
    
    static final String connectionURL = "jdbc:oracle:thin:@10.10.100.16:1521:orcl";
    
    static final String userID = "DEVELOP";
    
    static final String userPassword = "DEVELOP";
    

    public void readClob(String fileName) {
        Connection con = null;
        Statement stmt = null;
        ResultSet rset = null;
        StringBuffer sb = new StringBuffer();
        sb.append(" with tt as(select distinct table_name from user_tab_partitions) ");
        sb.append(" select  dbms_metadata.get_ddl('TABLE',table_name) CLOB_TEXT from tt ");
        try {
            Class.forName(driver_class).newInstance();
            con = DriverManager.getConnection(connectionURL, userID, userPassword);
            stmt = con.createStatement();
            rset = stmt.executeQuery(sb.toString());
            while (rset.next()) {
                Clob clob = rset.getClob("CLOB_TEXT");
                Reader rd = clob.getCharacterStream();
                char[] str = new char[(int) clob.length()];
                while (rd.read(str) != -1) {
                    writer.write(str);
                    writer.flush();
                }
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rset != null) {
                    rset.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void readClobToTxt(String fileName, String content) {
        FileWriter writer = null;
        try {
            // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
            writer = new FileWriter(fileName, true);
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) {
        ImportTableDefine importTableDefine = new ImportTableDefine();
        importTableDefine.readClob("d:/分区表定义.txt");
    }


用数据库的话  impdp test/test directory=dump_0420 dumpfile=2.dmp sqlfile=11.sql include=TABLE/TABLE


你可能感兴趣的:(将Oracle分区表的定义导到文件中)