Java vs. two tables and fields in a database, write something that's so unpopular.

Source: Internet
Author: User

Java vs. two tables and fields in a database, write something that's so unpopular.

Reprint of the Source network

Now that the project team is close to the next release, there is a problem: Developers add data tables, data fields, or change field types or field lengths as needed on the development library.

Due to the time is more urgent, resulting in the development process is not possible to record the DDL database script, in the larger projects, such as my project development system probably contains about 800 tables, the field of 10,000 cases, manual processing is obviously not possible, so we have to pass the program to determine the comparison , which are the new tables we need to add, which are the fields we need to add, and which ones we need to modify.

Because I am developing a project that works for a bank, so the amount of data is undoubtedly large, so this Java class can be used in almost most cases, the current situation is that a running production server has a database---production library, we have a database on the developer server---development library, We need to compare the two library differences, I spent about 1 hours to write the following classes, run it can be a different place to write to the file, so that the subsequent write DDL script processing.

The first is a table class that represents one of the tables in our database, with table names of type string, and HashMap () that hold several fields.

Java
1234567891011121314151617181920212223242526 Package Test;import Java.util.hashmap;public class Table {public String tablename;public HashMap columns = new HashMap (); Public Table (String tableName) {this.tablename = TableName,} public String Gettablename () {return tableName;} public void Settablename (String tableName) {this.tablename = tableName;} public HashMap GetColumns () {return columns;} public void Set Columns (HashMap Columns) {this.columns = Columns;}}

Then there is a column class, which represents a field in the database, where the property is the field name, field type, field length, and of course you can add more features to your needs

Java
123456789101112131415161718192021222324252627282930 Packag e test;public class column {public string columnname;public string  Datatype;public int length; public column (string columnname, string  Datatype, int length)  {this.columnName = columnName;this.dataType =  Datatype;this.length = length;} Public string getcolumnname ()  {return columnname;} Public void setcolumnname (String columnname)  {this.columnname = columnname;} Public string getdatatype ()  {return datatype;} Public void setdatatype (String datatype)  {this.datatype = datatype;} Public int getlength ()  {return length;} Public void setlength (Int length)  {this.length = length;}}

In fact, this method can be completely without the above two classes, but in order to write it easy to understand, so it is used, the implementation of efficiency is actually good, hundreds of tables a few seconds ran out

The following is the main implementation of the requirements of the class, written out of the main purpose is to help me improve, after all, their own program does not have too many design ideas and bigger picture, I hope that the person can modify the changes:

Java
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 ();}}

Epilogue: The whole procedure is not complicated, I feel a little cumbersome to write, I hope I can streamline a bit

Share:

Java vs. two tables and fields in a database, write something that's so unpopular.

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.