您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 上饶分类信息网,免费分类信息发布

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

2024/4/26 14:39:38发布33次查看
以下是主要的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 < 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 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 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 < listsize; i++) { table = list.get(i); string sql_select = sql_s_td_p+table+ group by dnt order by null;; arraylistlistdata = 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 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 < 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); } }}
上饶分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录