| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611 7118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 1581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971 98199200201202203204205206207208209 |
Package test;package test; import java.io.file;import java.io.fileoutputstream;import java.io.OutputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.preparedstatement;import java.sql.sqlexception;import java.util.hashmap;import java.util.iterator;import java.util.map; import com.amarsoft.are.sql.asresultset;import com.amarsoft.are.sql.transaction;import com.amarsoft.are.util.dataconvert; public class comparetable { public static stringbuffer[] sb = { new StringBuffer (), new stringbuffer (), New stringbuffer (), new stringbuffer (), new stringbuffer (), New stringbuffer () }; public static Transaction Gettransaction_product () throws exception {class.forname ("Oracle.jdbc.driver.OracleDriver"); Connection conn = drivermanager.getcOnnection ("JDBC:ORACLE:THIN:@192.168.1.1:1621:ORCL", "Demo1", "Demo1");if (conn != NULL) SYSTEM.OUT.PRINTLN ("Database loaded successfully! "); Transaction transaction = new transaction (conn); return transaction;} public static transaction gettransaction_develop () throws exception { Class.forName ("Oracle.jdbc.driver.OracleDriver"); Connection conn = drivermanager.getconnection ("JDBC:ORACLE:THIN:@192.168.1.2:1621:ORCL", " Demo2 ", " Demo2 ");if (conn != null) System.out.println (" Database loading succeeded! "); Transaction transaction = new transaction (conn); return transaction;} public static void main (String[] args) throws exception { Comparetables (); //Compare Database WriteFile (); //Write file} public static void Comparetables () throws exception { //Production database connection transaction trans_product = GettranSaction_product (); Map<</span>string, table> map_product = gettables (trans_product); // Development database Connection Transaction trans_develop = gettransaction_develop (); Map<</span>string, table> map_develop = gettables (trans_develop); // Traverse the Development Library mapfor (Iterator iter_table = map_develop.keyset (). Iterator (); iter_ Table.hasnext ();) {String key_table = (String) iter_table.next (); Table table_develop = map_develop.get (key_table);//Get tables in the development library table table_product = map_product.get (key_table);//try to get table if (Table_product == null) from the production library with the same name { //if you get the table empty , stating that development exists, production does not exist append (table_develop, null, 2);} The else { //table is the same, judging the field, field type, field length for (iterator iter_column = table_ Develop.columns.keySet (). iterator (); iter_column.hasnext ();) {String key_column = ( String) iter_column.nexT (); Column column_develop = table_develop.columns.get (key_column);//Get the columns in the development library Column column_ Product = table_product.columns.get (key_column);//try to get the same rank if from the production library (column_product == NULL) {//If the column name is empty, the development exists, the production does not exist append (table_develop, column_develop, 4);} else {//description both exist if (!column_develop.datatype.equals (Column_product.datatype))//Inconsistent field type append ( table_develop, column_develop, 5);if (column_develop.length != column_product.length) Field length inconsistent append (table_develop, column_develop, 6);}}} //traverse the Production library mapfor (Iterator iter_table = map_product.keyset (). Iterator (); iter_ Table.hasnext ();) {String key_table = (String) iter_table.next (); Table table_product = map_product.get (key_table);//try to get a table with the same name from the production library table table_develop = map_develop.get (key_table);//Get Table if (table_develop == null) { //in the development libraryIf the obtained table is empty, the description development exists, the production does not exist append (table_product, null, 1);} The else { //table is the same, judging the field, field type, field length for (iterator iter_column = table_ Product.columns.keySet (). iterator (); iter_column.hasnext ();) {String key_column = ( String) iter_column.next (); Column column_product = table_product.columns.get (key_column);//Get the columns in the production library Column column_ Develop = table_develop.columns.get (key_column);//try to get the same rank if from the development library (column_develop == NULL) {//If the column name is empty, indicating that the production exists, development does not exist append (table_product, column_product, 3);}}} public static map<</span>string, table> gettables (Transaction transaction) throws exception { string ssql = "Select Table_name,Column_Name , Data_type, "+ " DECODE (data_type, ' number ', data_precision, ' VARCHAR2 ', "+ " data_length, ' VARCHAR ', Data_ Length, ' CHAR ', data_length,0) length, "+ " NVL (data_scale, 0) SCale,decode (NULLABLE, ' N ', ' 1 ', ' 0 ') NULLABLE "+ " from User_tab_columns where 1=1 Order by Table_name,column_name "; nbsp Asresultset rs = transaction.getasresultset (sSQL); map<</span>string, table > map = new HashMap<</SPAN>String, Table> (); string tablename = ""; table table = null;while (Rs.next ()) {if (!tablename.equals (rs.getstring ("Table_ Name ")) {//a new table tablename = rs.getstring (" table_name "); Table = new table ( TableName); Column column = new column (rs.getstring ("column_name"), Rs.getstring ("Data_Type"), Rs.getint ("Length")); Table.columns.put (Column.columnname, column); Map.put (rs.getstring ("table_name"), table);} else {//the table that already exists, add the field Column column = new column (rs.getstring ("Column_Name"), Rs.getstring ("Data_type"), rs.getint ("Length")); Table.columns.put (column.coLumnname, column);}} if (Null != rs) rs.close (); Transaction.finalize (); return map;} public static void append (table table, column column, int Flag) throws exception {switch (flag) {case 1:system.out.println ("1, production presence, development of non-existent tables:" + table.gettablename ());//Skip Sb[0].append (Table.gettablename () + "\ n"); Break;case 2: System.out.println ("2, the production does not exist, the development exists in the table:" + table.gettablename ());//requires human judgment script Sb[1].append ( Table.gettablename () + "\ n"); Break;case 3:system.out.println ("3, production exists, developing fields that do not exist:" + Table.gettablename () + "| " + column.getcolumnname ());//need to manually determine how to handle Sb[2].append (Table.gettablename () + " | " + column.getcolumnname () + " \ n "); Break;case 4:system.out.println (" 4, production does not exist, the field of development exists: " + table.gettablename () + "| " + column.getcolumnname ());//requires human Judgment script Sb[3].append (Table.gettablename () + "| " + column.getcolumnname () + " \ n "); Break;case 5:system.out.println (" 5, table and field are the same, but the field type is different: " + table.gettablename () + "| " + column.getcolumnname () + " | "+ column.getdatatype ());//Need Human Judgment script Sb[4].append (Table.gettablename () + " | " + column.getcolumnname () + " | " + column.getdatatype () + " \ n "); Break;case 6:system.out.println (" 6, Table and field, field type are the same, But the length of the field is different: "+ table.gettablename () + " | " + column.getcolumnname () + " | " + column.getlength ());//Need Human Judgment script Sb[5].append (Table.gettablename () + " | " + column.getcolumnname () + " | " + column.getlength () + " \ n "); break;}} public static void writefile () throws Exception {String[] filename = { "d://table//production exists, the development of non-existent tables of. txt", "d://table//production does not exist, the development exists of the table. txt", "d://table//production exists , develop a field that does not exist. txt "," d://table//production does not exist, the development exists in the field of. txt "," d://table//tables and fields are the same, but the fields of different types of content. txt "," d://table//tables and fields, field types are the same, but field lengths differ in content. txt "&NBSP; ; for (int i = 0; i <</span> filename.length; i++) {file file = new file (filename[i]);outputstream os = new FileOutputStream (file); Os.write (Sb[i].tostring (). GetBytes ()); Os.flush (); Os.close ();}} |