Oracle database scripts are converted to SQL Server, DB2

Source: Internet
Author: User
Tags db2 readfile readline

 Packageutil;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.File;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;/** * @authorWyl * @Description TODO * @date 2016-5-19 **/ Public classOracletoothers {/*** @Description: TODO *@paramargs*/     Public Static voidMain (string[] args) {/** Original Oracle database script is exported by Plsql, only select Create Tables option when exporting * for comments on tables or columns in the original Oracle database, if there is a newline character in the comment, the resulting target script will have errors in the generated target script Make Changes*/        //Original file pathString oraclefile = "C:\\users\\admin\\desktop\\table.sql"; //Destination file pathString sqlserverfile = "C:\\users\\admin\\desktop\\table_sqlserver.sql"; String Db2file= "C:\\users\\admin\\desktop\\table_db2.sql";        Oracletosqlserver (Oraclefile,sqlserverfile);            OracleToDB2 (Oraclefile,db2file); }    /*** @Description: Converted from Oracle script to SQL Server script *@paramoraclefile Original Oracle Database script file path *@paramSqlserverfile to the target path to the SQL Server script file*/     Public Static voidoracletosqlserver (String oraclefile,string sqlserverfile) {File readFile=NewFile (Oraclefile); File WriteFile=NewFile (Sqlserverfile); BufferedReader Reader=NULL; BufferedWriter writer=NULL; Try{Reader=NewBufferedReader (NewFileReader (readFile)); Writer=NewBufferedWriter (NewFileWriter (WriteFile)); StringBuffer SB=NewStringBuffer (); String tempstring=NULL; String TempStr=NULL; String TempS=NULL; String TableName=NULL; String column=NULL; String Common=NULL; intEndIndex = 0; //read one line at a time until NULL is read to the end of the file             while((tempstring = Reader.readline ())! =NULL) {                //Show Line Numbers//System.out.println ("line" + Line + ":" + tempstring); //Converting data typesTempstring=tempstring.replaceall ("VARCHAR2", "VARCHAR"); TempString=tempstring.replaceall ("number", "DECIMAL"); TempString=tempstring.replaceall ("CLOB", "TEXT"); if(Tempstring.indexof ("TIMESTAMP")!=-1) {tempstring=tempstring.replaceall ("TIMESTAMP", "DATETIME"); intstr = Tempstring.indexof ("("); intEnd = Tempstring.indexof (")"); SYSTEM.OUT.PRINTLN (tempstring.substring (str, End+1)); TempString=tempstring.replaceall ("\ \ (" +tempstring.substring (str+1, end) + "\ \)", ""); }                                //Add Comment                if(Tempstring.startswith ("Prompt") | | Tempstring.startswith ("set") | | Tempstring.startswith ("commit;") | | Tempstring.startswith ("Spool") ) {tempstring= "--"+tempstring; }                                //change the SQL comment script                if(Tempstring.startswith ("comment on") ) {TempStr=tempstring; Continue; }                if(Tempstring.startswith ("is")) ) {tempstring= TempStr +tempstring; if(!tempstring.endswith ("';") ) {TempS=tempstring; TempStr= ""; Continue; }                                    }                if(Tempstring.startswith ("';") ) {tempstring= temps+tempstring; TempS= ""; }                //Table Notes                if(Tempstring.startswith ("comment on table") &&tempstring.endswith ("';") ) {EndIndex= Tempstring.indexof ("is")); //Table nameTableName = tempstring.substring (17, EndIndex); String[] Stra= Tempstring.split ("'")); Common= Stra[1]; TempString= "EXECUTE sp_addextendedproperty n ' ms_description ', n '" "+common+" ', n ' user ', n ' dbo ', n ' table ', n ' "+tablename+" ', NULL, NULL "; }                //Column Comments                if(Tempstring.startswith ("Comment on column") &&tempstring.endswith ("';") ) {EndIndex= Tempstring.indexof ("."); //Table nameTableName = tempstring.substring (18, EndIndex); String[] Stra= Tempstring.split ("'")); intEndin = Stra[0].indexof ("is"); Column= Stra[0].substring (endindex+1, Endin); Common= Stra[1]; TempString= "EXECUTE sp_addextendedproperty n ' ms_description ', n '" "+common+" ', n ' user ', n ' dbo ', n ' table ', n ' "+tablename+" ', n ' Column ', N ' "+column+" ' "; }                //PrintSystem.out.println (tempstring); Sb.append (tempstring+ "\ r \ n");            } reader.close (); //write to destination fileWriter.write (sb.tostring ());        Writer.close (); } Catch(IOException e) {e.printstacktrace (); } finally {            if(Reader! =NULL) {                Try{reader.close (); } Catch(IOException E1) {}}} }    /*** @Description: Converted from Oracle script to DB2 script *@paramoraclefile Original Oracle Database script file path *@paramdb2file destination file path to DB2 script*/     Public Static voidoracleToDB2 (String oraclefile,string db2file) {File readFile=NewFile (Oraclefile); File WriteFile=NewFile (Db2file); BufferedReader Reader=NULL; BufferedWriter writer=NULL; Try{Reader=NewBufferedReader (NewFileReader (readFile)); Writer=NewBufferedWriter (NewFileWriter (WriteFile)); StringBuffer SB=NewStringBuffer (); String tempstring=NULL; //read one line at a time until NULL is read to the end of the file             while((tempstring = Reader.readline ())! =NULL) {                //Show Line Numbers//System.out.println ("line" + Line + ":" + tempstring); //Converting data typesTempstring=tempstring.replaceall ("VARCHAR2", "VARCHAR"); TempString=tempstring.replaceall ("number", "DECIMAL"); TempString=tempstring.replaceall ("CLOB", "CLOB"); //Add Comment                if(Tempstring.startswith ("Prompt") | | Tempstring.startswith ("set") | | Tempstring.startswith ("commit;") | | Tempstring.startswith ("Spool") ) {tempstring= "--"+tempstring;                } System.out.println (tempstring); Sb.append (tempstring+ "\ r \ n");            } reader.close (); //write to destination fileWriter.write (sb.tostring ());        Writer.close (); } Catch(IOException e) {e.printstacktrace (); } finally {            if(Reader! =NULL) {                Try{reader.close (); } Catch(IOException E1) {}}} }}

Oracle database scripts are converted to SQL Server, DB2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.