如下是主要的java實現代碼:具體設計參考java
自制mysql主從複製(實時)軟件——實現 http://my.oschina.net/u/1462678/blog/227152mysql
一、create table sql
package migrate; import java.util.ArrayList; import java.util.Vector; import mysql.MySql; public class Create_Table { static private String sql_s_t1 = "select tablename from datacenterb.v_tables_2013 where tablename <> 'v_tables_2013';"; static private String sql_s_t_schema_p = "show create table datacenterb."; static private String sql_alt_t_p33 = " MODIFY COLUMN `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,ADD PRIMARY KEY (`id`);"; static private String sql_alt_t_p22 = " MODIFY COLUMN `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;"; public void create_table() { ArrayList<String>list = MySql.executeQuery(sql_s_t1,1);// 1 表明連接數據庫datacenterb int listsize = list.size(); String table = null; String sql_create[] = new String[2]; for (int i = 0; i < listsize; i++) {//get p2.table name table = list.get(i); /* * judge the create table languages */ sql_create = sqlCreate(table); try {//執行建表(正常建表) int rs = new MySql().update(sql_create[1],3); if(rs!=0){ Report_File.operate_log("2013create table success ,From: "+table); }else{ Report_File.operate_log("2013create table error ,From: "+table); } } catch (Exception e) { Report_File.operate_log("The table doesn't exist in database (datacenterb)"); }//end try //建完表後決定是否須要修改 if (sql_create[0].equals("22")) { String sql_alter = "ALTER TABLE datacenter2013."+table+sql_alt_t_p22; alter_table(sql_alter,table); }else if(sql_create[0].equals("33")) { String sql_alter = "ALTER TABLE datacenter2013."+table+sql_alt_t_p33; alter_table(sql_alter,table); }else { //do nothing }//end if else for judge }//end for i }//end method public void alter_table(String sql_alter,String table) { //修改表 try { int rs_alt = new MySql().update(sql_alter,3); if (rs_alt!=0) { Report_File.operate_log("2013alter table success ,From: "+table); }else{ Report_File.operate_log("2013alter table error ,From: "+table); } } catch (Exception e) { Report_File.operate_log("Alter the table error (maybe it is from operate error)"); } } public String[] sqlCreate(String table) { String sql_s_t_schema = sql_s_t_schema_p+table; ArrayList<Vector<String>> sql_create_list = MySql.executeQuery(sql_s_t_schema, 1, "string"); int sql_create_listsize = sql_create_list.size(); String sql_create[] = new String[2];//define dyadic array for (int k = 0; k < sql_create_listsize; k++) {//get table name and table schema Vector<String> table_row = sql_create_list.get(k); String sql_create_p = null; for (int l = 1; l< table_row.size(); l++) {//get table schema sql_language sql_create_p = table_row.get(l); try { //solve the sql_language for table_name int pk =sql_create_p.indexOf("PRIMARY KEY"); int begin =sql_create_p.lastIndexOf("AUTO_INCREMENT="); if (begin>0) {//string replace auto_increment = number to auto_increment = 1; int end =sql_create_p.lastIndexOf("DEFAULT"); sql_create[0] = "11";// has been replaced sql_create[1] =sql_create_p.replace(sql_create_p.subSequence(begin, end),"AUTO_INCREMENT=1 "); }else if (pk>0) {//has primary key but no auto_increment sql_create[0] = "22"; sql_create[1] =sql_create_p; }else{//no auto_increment = number ,we need to add it to the table; sql_create[0] = "33";// has not been replaced sql_create[1] =sql_create_p; } } catch (Exception e) { // TODO: handle exception System.out.println("replace the create table languages error"+e.toString()); } }//end for l }//end for k return sql_create; } }
二、insert table數據庫
package migrate; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Vector; import mysql.MySql; public class Insert_Table { static private String sql_s_t = "select tablename from datacenterb.v_tables_2013 where tablename <> 'v_tables_2013';"; static private String sql_s_td_p = "select SQL_NO_CACHE * from datacenterb."; public void insertExecute() { SimpleDateFormat sp=new SimpleDateFormat("yyyy-MM-dd");//格式化時間爲text ArrayList<String>list = MySql.executeQuery(sql_s_t,1);// datacenterb 中查詢表 int listsize = list.size(); String table = null; for (int i = 0; i < listsize; i++) { table = list.get(i); String sql_select = sql_s_td_p+table+" group by dnt order by null;"; ArrayList<Vector<String>>listdata = MySql.executeQuery(sql_select, 1,"select");//在遷移源頭表中查找數據 try { int listdatasize = listdata.size(); if (listdatasize==0) {//保存沒數據的表 Report_File.getNoData_Save(table, sp.format(new Date())); }else { StringBuilder sql_insert_p = new StringBuilder();//依然是StringBulider 方式處理 for (int j = 0; j < listdatasize; j++) { Vector<String> row = listdata.get(j); sql_insert_p.append(this.getInsert_SQL(row));//this private // if (j%1000==0||j==listsize-1) {//分批次提交效果 if (j==20000||j==35000||j==55000||j==70000||j==listdatasize-1) {//分批次提交效果 sql_insert_p.delete((sql_insert_p.length()-8), sql_insert_p.length());//去掉尾部的" ,(null,' " String sql_insert = sql_insert_p.toString(); this.insertTable(sql_insert, table);//執行插入數據 sql_insert_p.delete(0, sql_insert_p.length());// 清空StringBulider爲空 }//end if }//end for j }//end else }catch(Exception e) { e.printStackTrace(); Report_File.getError_save(table, sp.format(new Date()));//保存操做中出錯表 }//end try }//end for i }//end method private StringBuilder getInsert_SQL(Vector<String> row){//private 構造insert 表的語句 int size = row.size(); // 獲取集合大小 StringBuilder sql_insert_data = new StringBuilder();//StringBuilder 處理字符串拼接過程 for(int i = 1; i < size; i++) { // 寫入每一行 if (row.get(i)==null) {//斷定數據是否爲空 sql_insert_data.setLength(0);//丟掉數據 }else { sql_insert_data.append(row.get(i)); if (i==(size-1)) { sql_insert_data.append("'),(null,'");//最後一個 }else { sql_insert_data.append("','");}//不是最後一個}//丟掉數據 }//end else }// end for return sql_insert_data; } /* * 有id auto_increament * 一種是:insert into tablename values (null,'',''); * 另一種是:insert into tablename (item1,item2)values ('',''); * 一一對應的效果 */ private void insertTable(String sql,String table) {//執行插入操做 // int length = table.length();//合併年表須要處理插入的表名稱 // String insertTable = table.substring(0, length-2); String insert_sql= "insert into "+table+" values(null,'"+sql; int rs = new MySql().update(insert_sql,3); if(rs!=0){ Report_File.operate_log("insert data success,From :"+table); }else{ Report_File.operate_log("insert data error,From :"+table); } } }