123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
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("資料庫載入成功!");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("資料庫載入成功!");Transaction transaction = new Transaction(conn);return transaction;} public static void main(String[] args) throws Exception {compareTables(); // 比較資料庫writeFile(); // 寫入檔案} public static void compareTables() throws Exception { // 生產資料庫連接Transaction trans_product = getTransaction_product();Map<</SPAN>String, Table> map_product = getTables(trans_product); // 開發資料庫連接Transaction trans_develop = getTransaction_develop();Map<</SPAN>String, Table> map_develop = getTables(trans_develop); // 遍曆開發庫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);// 獲得開發庫中的表Table table_product = map_product.get(key_table);// 嘗試從生產庫中獲得同名表if (table_product == null) { // 如果獲得表為空白,說明開發存在,生產不存在append(table_develop, null, 2);} else { // 表相同,判斷欄位、欄位類型、欄位長度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);// 獲得開發庫中的列Column column_product = table_product.columns.get(key_column);// 嘗試從生產庫中獲得同名列if (column_product == null) {// 如果列名為空白,說明開發存在,生產不存在append(table_develop, column_develop, 4);} else {// 說明兩者都存在if (!column_develop.dataType.equals(column_product.dataType))// 欄位類型不一致append(table_develop, column_develop, 5);if (column_develop.length != column_product.length)// 欄位長度不一致append(table_develop, column_develop, 6);}}}} // 遍曆生產庫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);// 嘗試從生產庫中獲得同名表Table table_develop = map_develop.get(key_table);// 獲得開發庫中的表if (table_develop == null) { // 如果獲得表為空白,說明開發存在,生產不存在append(table_product, null, 1);} else { // 表相同,判斷欄位、欄位類型、欄位長度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);// 獲得生產庫中的列Column column_develop = table_develop.columns.get(key_column);// 嘗試從開發庫中獲得同名列if (column_develop == null) {// 如果列名為空白,說明生產存在,開發不存在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"; 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"))) {// 一張新表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 {// 已存在的表,增加欄位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、生產存在,開發不存在的表:" + table.getTableName());// 跳過sb[0].append(table.getTableName() + "\n");break;case 2:System.out.println("2、生產不存在,開發存在的表:" + table.getTableName());// 需要人工判斷指令碼sb[1].append(table.getTableName() + "\n");break;case 3:System.out.println("3、生產存在,開發不存在的欄位:" + table.getTableName()+ " | " + column.getColumnName());// 需人工判斷如何處理sb[2].append(table.getTableName() + " | " + column.getColumnName()+ "\n");break;case 4:System.out.println("4、生產不存在,開發存在的欄位:" + table.getTableName()+ " | " + column.getColumnName());// 需要人工判斷指令碼sb[3].append(table.getTableName() + " | " + column.getColumnName()+ "\n");break;case 5:System.out.println("5、表和欄位都相同,但欄位類型不同的內容:" + table.getTableName()+ " | " + column.getColumnName() + " | "+ column.getDataType());// 需要人工判斷指令碼sb[4].append(table.getTableName() + " | " + column.getColumnName()+ " | " + column.getDataType() + "\n");break;case 6:System.out.println("6、表和欄位、欄位類型都相同,但欄位長度不同的內容:"+ table.getTableName() + " | " + column.getColumnName()+ " | " + column.getLength());// 需要人工判斷指令碼sb[5].append(table.getTableName() + " | " + column.getColumnName()+ " | " + column.getLength() + "\n");break;}} public static void writeFile() throws Exception {String[] fileName = { "D://table//生產存在,開發不存在的表.txt","D://table//生產不存在,開發存在的表.txt", "D://table//生產存在,開發不存在的欄位.txt","D://table//生產不存在,開發存在的欄位.txt","D://table//表和欄位都相同,但欄位類型不同的內容.txt","D://table//表和欄位、欄位類型都相同,但欄位長度不同的內容.txt" }; 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();}}} |