问题描述
在控制台用mysqldump命令备份陈宫,然后也是在控制台恢复数据也行。但是在程序中用Runtime.getRuntime().exec("")调用命令就是不行,加上waitFor()语句的话,程序就挂起不往下执行了,从网上看有人说要把控制台得输入给读出来,我把源码复制改了改还是不行,生成的sql文件只有一小部分内容。想请高手们帮帮忙,急啊
解决方案
解决方案二:
把代码贴出来分析一下。
解决方案三:
publicstaticvoidbackup(){try{Runtimert=Runtime.getRuntime();//调用mysql的cmd:Processchild=rt.exec("mysqldump-hlocalhost-uroot-p803net--default-character-set=utf8subway>D:/subway.sql");//设置导出编码为utf8。这里必须是utf8//把进程执行中的控制台输出信息写入.sql文件,即生成了备份文件。注:如果不对控制台信息进行读出,则会导致进程堵塞无法运行InputStreamin=child.getInputStream();//控制台的输出信息作为输入流InputStreamReaderxx=newInputStreamReader(in,"utf8");//设置输出流编码为utf8。这里必须是utf8,否则从流中读入的是乱码StringinStr;StringBuffersb=newStringBuffer("");StringoutStr;//组合控制台输出信息字符串BufferedReaderbr=newBufferedReader(xx);while((inStr=br.readLine())!=null){System.out.println(inStr);//**************************************************sb.append(inStr+"rn");}outStr=sb.toString();//要用来做导入用的sql目标文件:FileOutputStreamfout=newFileOutputStream("D:/subway.sql");OutputStreamWriterwriter=newOutputStreamWriter(fout,"utf8");writer.write(outStr);//注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免writer.flush();//别忘记关闭输入输出流in.close();xx.close();br.close();writer.close();fout.close();System.out.println("/*OutputOK!*/");}catch(Exceptione){e.printStackTrace();}}/***导入**/publicstaticvoidload(){try{StringfPath="D:/subway.sql";Runtimert=Runtime.getRuntime();//调用mysql的cmd:Processchild=rt.exec("mysql-uroot-p803netsubway<D:/subway.sql");OutputStreamout=child.getOutputStream();//控制台的输入信息作为输出流StringinStr;StringBuffersb=newStringBuffer("");StringoutStr;BufferedReaderbr=newBufferedReader(newInputStreamReader(newFileInputStream(fPath),"utf8"));while((inStr=br.readLine())!=null){sb.append(inStr+"rn");}outStr=sb.toString();OutputStreamWriterwriter=newOutputStreamWriter(out,"utf8");writer.write(outStr);//注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免writer.flush();//别忘记关闭输入输出流out.close();br.close();writer.close();System.out.println("/*LoadOK!*/");}catch(Exceptione){e.printStackTrace();}}
解决方案四:
用“mysqldump-hlocalhost-uroot-p803net--default-character-set=utf8subway>D:/subway.sql”命令在控制台备份,生成的subway.sql文件内容为:--MySQLdump10.13Distrib5.1.41,forWin32(ia32)----Host:localhostDatabase:subway----------------------------------------------------------Serverversion5.1.41-community/*!40101SET@OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT*/;/*!40101SET@OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS*/;/*!40101SET@OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION*/;/*!40101SETNAMESutf8*/;/*!40103SET@OLD_TIME_ZONE=@@TIME_ZONE*/;/*!40103SETTIME_ZONE='+00:00'*/;/*!40014SET@OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS,UNIQUE_CHECKS=0*/;/*!40014SET@OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS,FOREIGN_KEY_CHECKS=0*/;/*!40101SET@OLD_SQL_MODE=@@SQL_MODE,SQL_MODE='NO_AUTO_VALUE_ON_ZERO'*/;/*!40111SET@OLD_SQL_NOTES=@@SQL_NOTES,SQL_NOTES=0*/;----Tablestructurefortable`subway_device`--DROPTABLEIFEXISTS`subway_device`;/*!40101SET@saved_cs_client=@@character_set_client*/;/*!40101SETcharacter_set_client=utf8*/;CREATETABLE`subway_device`(`dev_id`varchar(40)NOTNULL,`seq_no`varchar(40)DEFAULT'',`city`varchar(100)DEFAULT'',`line`varchar(100)DEFAULT'',`dev_code`varchar(40)DEFAULT'',`dev_type`varchar(200)DEFAULT'',`dev_desc`varchar(400)DEFAULT'',`train_code`varchar(150)DEFAULT'',`sta_loc`varchar(150)DEFAULT'',`dt_create`varchar(50)DEFAULT'',`dev_addr`varchar(150)DEFAULT'',`dev_gateway`varchar(150)DEFAULT'',`dev_dns`varchar(150)DEFAULT'',`if_num`int(11)DEFAULT'0',`nic_count`int(11)DEFAULT'0',`rssi_threshold`int(11)DEFAULT'0',`rssi_diff_threshold`int(11)DEFAULT'0',`rssi_poll`int(11)DEFAULT'0',`pos`varchar(200)DEFAULT'',`mode`varchar(200)DEFAULT'',`remark`varchar(400)DEFAULT'',PRIMARYKEY(`dev_id`),KEY`dev_code`(`dev_code`))ENGINE=InnoDBDEFAULTCHARSET=utf8;/*!40101SETcharacter_set_client=@saved_cs_client*/;----Dumpingdatafortable`subway_device`--LOCKTABLES`subway_device`WRITE;/*!40000ALTERTABLE`subway_device`DISABLEKEYS*/;INSERTINTO`subway_device`VALUES('1','1','南京','1号线','AP1','AP','1号AP','','','2010-03-15','10.1.0.1','192.68.0.1','',4,0,0,0,0,'','',''),('2','2','南京','1号线','AP2','AP','2号AP','','','2010-03-15','10.1.0.2','192.168.0.1','',3,0,0,0,0,'','',''),('3','3','南京','1号线','AP3','AP','3号AP','','','2010-03-15','10.1.0.3','192.168.0.1','',3,0,0,0,0,'','',''),('4','4','南京','1号线','STA1','STA','1号STA','train1','front','2010-03-15','10.16.0.1','192.168.0.1','',4,1,10,10,10,'主','热备冗余',''),('402892812f6659ef012f666398f10001',NULL,NULL,NULL,'APD123','AP','123123',NULL,NULL,'11111111-四月四月-1818','12.12.12.12',NULL,NULL,0,0,0,0,0,NULL,NULL,NULL),('402892812f6659ef012f6663ecce0002',NULL,NULL,NULL,'AP2324','AP','wwer',NULL,NULL,'11111111-四月四月-1818','12.12.12.123',NULL,NULL,0,0,0,0,0,NULL,NULL,NULL),('5','5','南京','1号线','STA2','STA','2号STA','train1','back','2010-03-15','10.16.0.2','192.168.0.1','',4,0,0,0,0,'','',''),('6','6','南京','1号线','GW1','GW','1号通信网关服务器','','','2010-03-15','192.168.0.1','192.168.0.1','',2,0,0,0,0,'','',''),('7','7','南京','1号线','GW2','GW','2号通信网关服务器','','','2010-03-15','192.168.0.22','192.168.0.23','',2,0,0,0,0,'','','');/*!40000ALTERTABLE`subway_device`ENABLEKEYS*/;UNLOCKTABLES;。。。。在java程序中备份,生成的subway.sql内容为:--MySQLdump10.13Distrib5.1.41,forWin32(ia32)----Host:localhostDatabase:subway----------------------------------------------------------Serverversion5.1.41-community/*!40101SET@OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT*/;/*!40101SET@OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS*/;/*!40101SET@OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION*/;/*!40101SETNAMESutf8*/;/*!40103SET@OLD_TIME_ZONE=@@TIME_ZONE*/;/*!40103SETTIME_ZONE='+00:00'*/;/*!40014SET@OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS,UNIQUE_CHECKS=0*/;/*!40014SET@OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS,FOREIGN_KEY_CHECKS=0*/;/*!40101SET@OLD_SQL_MODE=@@SQL_MODE,SQL_MODE='NO_AUTO_VALUE_ON_ZERO'*/;/*!40111SET@OLD_SQL_NOTES=@@SQL_NOTES,SQL_NOTES=0*/;哪位大侠帮忙分析分析啊,急啊!!!