jsp+mysql数据库备份与还原实例

jsp+mysql数据库备份与还原实例,第1张

package mysql

import java.io.BufferedReader

import java.io.BufferedWriter

import java.io.File

import java.io.FileInputStream

import java.io.FileOutputStream

import java.io.IOException

import java.io.InputStream

import java.io.InputStreamReader

import java.io.LineNumberReader

import java.io.OutputStream

import java.io.OutputStreamWriter

public class MySQL {

public MySQL(){

}

public void backup(String mysql) throws IOException{

//String command="cmd /c mysqldump -u "+username+" -p "+pwd+" "+dataname+" >"+path

Runtime r=Runtime.getRuntime()

Process p

p = r.exec(mysql)

BufferedReader br=new BufferedReader(new InputStreamReader(p.getErrorStream()))

StringBuffer sb=new StringBuffer()

String inline

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

sb.append(inline).append("\n")

}

}

public static void load() {

try {

String fPath = "e:/zizhu/bak/20091112003349.sql"

Runtime rt = Runtime.getRuntime()

// 调用 mysql 的 cmd:

Process child = rt.exec("mysql -u root -p admin info")

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)

writer.flush()

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

out.close()

br.close()

writer.close()

System.out.println("/* Load OK! */")

} catch (Exception e) {

e.printStackTrace()

}

}

public static void main(String[] args) throws IOException {

MySQL my = new MySQL()

String command="cmd /c mysqldump -u "+"root"+" -p"+"admin"+" "+"info"+" >"+"d:/info.sql"

//my.load()

my.backup(command)

//String command="cmd /c mysqldump -u "+"username"+" -p "+"pwd"+" "+"dataname"+" >"+"path"

//System.out.println(command)

}

}

调用Runtime.getRuntime()的exec函数

执行

String command="mysqldump -u用户名 -p密码 haha >d:\haha.sql"

备份数据库试试搂主


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存