如何用Java实现MySQL数据库的备份和恢复

如何用Java实现MySQL数据库的备份和恢复,第1张

直接黏贴在IDEA上按格式查看

package com.liuzy.javaopen.servletimport java.io.BufferedReaderimport java.io.Fileimport java.io.FileInputStreamimport java.io.FileOutputStreamimport java.io.IOExceptionimport java.io.InputStreamimport java.io.InputStreamReaderimport java.io.OutputStreamimport java.io.OutputStreamWriterpublic class Test { public static void main(String[] args) throws IOException{ backup("d:\\d.sql") //recover("d:\\d.sql") } public static void backup(String path) throws IOException{ Runtime runtime = Runtime.getRuntime() //-u后面是用户名,-p是密码-p后面最好不要有空格,-family是数据库的名字 Process process = runtime.exec("mysqldump -u root -pmysql goldenwing") InputStream inputStream = process.getInputStream()//得到输入流,写成.sql文件 InputStreamReader reader = new InputStreamReader(inputStream) BufferedReader br = new BufferedReader(reader) String s = null StringBuffer sb = new StringBuffer() while((s = br.readLine()) != null){ sb.append(s+"\r\n") } s = sb.toString() System.out.println(s) File file = new File(path) file.getParentFile().mkdirs() FileOutputStream fileOutputStream = new FileOutputStream(file) fileOutputStream.write(s.getBytes()) fileOutputStream.close() br.close() reader.close() inputStream.close() } public static void recover(String path) throws IOException{ Runtime runtime = Runtime.getRuntime() //-u后面是用户名,-p是密码-p后面最好不要有空格,-family是数据库的名字,--default-character-set=utf8,这句话一定的加 //我就是因为这句话没加导致程序运行成功,但是数据库里面的内容还是以前的内容,最好写上完成的sql放到cmd中一运行才知道报错了 //错误信息: //mysql: Character set 'utf-8' is not a compiled character set and is not specified in the ' //C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\Index.xml' file ERROR 2019 (HY000): Can't // initialize character set utf-8 (path: C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\), //又是讨人厌的编码问题,在恢复的时候设置一下默认的编码就可以了。 Process process = runtime.exec("mysql -u root -pmysql --default-character-set=utf8 goldenwing") OutputStream outputStream = process.getOutputStream() BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path))) String str = null StringBuffer sb = new StringBuffer() while((str = br.readLine()) != null){ sb.append(str+"\r\n") } str = sb.toString() System.out.println(str) OutputStreamWriter writer = new OutputStreamWriter(outputStream,"utf-8") writer.write(str) writer.flush() outputStream.close() br.close() writer.close() }}

假设你要备份的数据条件是时间,只备份当天的数据。备份区:数据库。

匹配当天的数据读出来。

在将数据添加到备份区的数据库。

备份区表的创建问题:1.你可以事先手动建好。

2.也可以通过程序自动建表有两步;

a).判断当前备份数据,在备份区是否有对应的表。(有直接添加)

b).如果没有,拷贝当前备份数据的表结构,在备份区生成。

首先,设置mysql的环境变量(在path中添加%MYSQL_HOME%\bin),重启电脑。

完整代码:

备份:

public static void main(String[] args) {

backup()

load()

}

public static void backup() {

try {

Runtime rt = Runtime.getRuntime()

// 调用 mysql 的 cmd:

Process child = rt

.exec("mysqldump -u root --set-charset=utf8 bjse act_obj")// 设置导出编码为utf8。这里必须是utf8

// 把进程执行中的控制台输出信息写入.sql文件,即生成了备份文件。注:如果不对控制台信息进行读出,则会导致进程堵塞无法运行

InputStream in = child.getInputStream()// 控制台的输出信息作为输入流

InputStreamReader xx = new InputStreamReader(in, "utf8")// 设置输出流编码为utf8。这里必须是utf8,否则从流中读入的是乱码

String inStr

StringBuffer sb = new StringBuffer("")

String outStr

// 组合控制台输出信息字符串

BufferedReader br = new BufferedReader(xx)

while ((inStr = br.readLine()) != null) {

sb.append(inStr + "\r\n")

}

outStr = sb.toString()

// 要用来做导入用的sql目标文件:

FileOutputStream fout = new FileOutputStream(

"e:/mysql-5.0.27-win32/bin/bjse22.sql")

OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8")

writer.write(outStr)

// 注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免

writer.flush()

// 别忘记关闭输入输出流

in.close()

xx.close()

br.close()

writer.close()

fout.close()

System.out.println("")

} catch (Exception e) {

e.printStackTrace()

}

}

public static void load() {

try {

String fPath = "e:/mysql-5.0.27-win32/bin/bjse22.sql"

Runtime rt = Runtime.getRuntime()

// 调用 mysql 的 cmd:

Process child = rt.exec("mysql -u root bjse ")

OutputStream out = child.getOutputStream()//控制台的输入信息作为输出流

String inStr

StringBuffer sb = new StringBuffer("")

String outStr

BufferedReader br = new BufferedReader(new InputStreamReader(

new FileInputStream(fPath), "utf8"))

while ((inStr = br.readLine()) != null) {

sb.append(inStr + "\r\n")

}

outStr = sb.toString()

OutputStreamWriter writer = new OutputStreamWriter(out, "utf8")

writer.write(outStr)

// 注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免

writer.flush()

// 别忘记关闭输入输出流

out.close()

br.close()

writer.close()

System.out.println("")

} catch (Exception e) {

e.printStackTrace()

}

}

备份语句:

mysql>SELECT * INTO OUTFILE "D:\\data\\db_testtemp.txt" fields terminated by ',

' from db_testtemp where std_state='1'

Query OK, 1 row affected (0.00 sec)

mysql>SELECT * INTO OUTFILE "D:\\data\\db_testtemp.txt" fields terminated by ',

' from db_testtemp

Query OK, 2 rows affected (0.00 sec)

只生成一个只有数据的.txt:SELECT * INTO OUTFILE "D:\\data\\db_testtemp.txt" fields terminated by ',' lines terminated by '\r\n' from db_testtemp

只生成一个只有数据的.txt:mysqldump -uroot -pncae2010 -w "std_state='1'" -T D:\data --no-create-info --fields-terminated-by=, exam db_testtemp

生成一个创建数据库语句的.sql,一个只有数据的.txt:mysqldump -uroot -pncae2010 -w "std_state='1'" -T D:\data --fields-terminated-by=, exam db_testtemp

只生成insert语句:mysqldump -uroot -pncae2010 -w "std_state='1'" -t exam db_testtemp >D:\data\a.sql


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7126128.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-01
下一篇 2023-04-01

发表评论

登录后才能评论

评论列表(0条)

保存