欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

自制mysql主从复制(实时)软件实现_MySQL

程序员文章站 2022-05-10 15:00:07
...
以下是主要的java实现代码:具体设计参考

自制mysql主从复制(实时)软件——实现 http://my.oschina.net/u/1462678/blog/227152

1、create table

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() {		ArrayListlist = 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 > 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  table_row = sql_create_list.get(k);			String sql_create_p = null;			for (int l = 1; l0) {//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;	}}		

2、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				ArrayListlist = MySql.executeQuery(sql_s_t,1);// datacenterb 中查询表		int listsize = list.size();		String table = null;		for (int i = 0; i >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  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 row){//private 构造insert 表的语句		int size = row.size(); // 获取集合大小  		StringBuilder sql_insert_data = new StringBuilder();//StringBuilder 处理字符串拼接过程		for(int i = 1; i