Java实现SqlServer及MySql的备份与还原(经典版) 下载本文

内容发布更新时间 : 2024/11/18 12:34:46星期一 下面是文章的全部内容请认真阅读。

InputStreamReader input = new InputStreamReader(in,

\);// 设置输出流编码为utf8。这里必须是utf8,否则从流中读入的是乱码 String inStr;

StringBuffer sb = new StringBuffer(\); String outStr;

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

BufferedReader br = new BufferedReader(input); while ((inStr = br.readLine()) != null) { sb.append(inStr + \); }

outStr = sb.toString();

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

FileOutputStream fout = new FileOutputStream(fPath); OutputStreamWriter writer = new OutputStreamWriter(fout, \);

writer.write(outStr);

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

writer.flush(); //关闭输入输出流 in.close(); input.close(); br.close();

writer.close(); fout.close();

System.out.println(\备份成功\); flag=true;

} catch (Exception e) { flag=false;

e.printStackTrace(); }

response.setCharacterEncoding(\); try {

if(flag==true){

response.getWriter().print(

\

alert('MYSQL备份成功!');document.location.href='DataAction.do?action=toBackIndex';\);

}else{

response.getWriter().print(

\

alert('MYSQL

');document.location.href='DataAction.do?action=toBackIndex';\);

}

}

} catch (IOException e) { e.printStackTrace(); }

return null;

/**

* @Description: MySql还原 * @author 莫希柏

* @date Jul 4, 2012 4:39:10 PM */

public ActionForward doMySqlRecovery(ActionMapping mapping,

ActionForm form,HttpServletRequest request, HttpServletResponse response){ boolean flag=false; try {

String fPath =

request.getParameter(\);//路径

Runtime rt = Runtime.getRuntime();

String command=\

5.0/bin/mysql.exe -uroot -p123456 datatest \; // 调用mysql的cmd

Process child = rt.exec(command);

OutputStream out = child.getOutputStream();//控制台的输

入信息作为输出流

String inStr;

StringBuffer sb = new StringBuffer(\); String outStr; BufferedReader br

= new BufferedReader(new InputStreamReader(

new FileInputStream(fPath), \)); while ((inStr = br.readLine()) != null) { sb.append(inStr + \); }

outStr = sb.toString(); OutputStreamWriter writer = new OutputStreamWriter(out,

\);

writer.write(outStr);

//这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法

则可以避免

writer.flush(); //关闭输入输出流 out.close(); br.close();

writer.close();

System.out.println(\还原成功\); flag=true;

} catch (Exception e) { flag=false;

e.printStackTrace(); }

response.setCharacterEncoding(\); try {

if(flag==true){

response.getWriter().print(

\

alert('MYSQL还原成功!');document.location.href='DataAction.do?action=toBackIndex';\);

}else{

response.getWriter().print(

\

alert('MYSQL还原失败!');document.location.href='DataAction.do?action=toBackIndex';\);

}

} catch (IOException e) { e.printStackTrace(); }

return null; } /**

* @Description: 返回主页 * @author 莫希柏

* @date Jul 5, 2012 9:14:46 AM * @throws */

public ActionForward toBackIndex(ActionMapping mapping,

ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

return mapping.findForward(\); } }

3.存储过程

create proc killrestore (@dbname varchar(20),@dbpath varchar(40)) as begin

declare @sql nvarchar(500) declare @spid int

set @sql='declare getspid cursor for select spid from master..sysprocesses where dbid=db_id('''+@dbname+''')' exec (@sql) open getspid

fetch next from getspid into @spid while @@fetch_status <> -1 begin

exec('kill '+@spid)

fetch next from getspid into @spid end

close getspid deallocate getspid

restore database @dbname from disk= @dbpath with replace end 4.Jsp页面展示

<%@ page language=\import=\pageEncoding=\%> <%

String path = request.getContextPath(); String basePath = request.getScheme()+\+request.getServerName()+\+request.getServerPort()+path+\; %>

\>

数据备份与还原

content=\>