对oracle数据库中的数据表进行备份
2010-03-17 23:20
99 查看
public ActionForward backUpAction(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Calendar cDay = Calendar.getInstance();
SimpleDateFormat myf = new SimpleDateFormat("yyyyMMddHHmmss");
String day = myf.format(cDay.getTime());
// 获得Constant中定义的数据库备份文件存放路径,格式如:
// public static final String DataBaseBackUpPath = "D://sOADB//";
String outputFile = Constant.DataBaseBackUpPath;
// 如果服务器端硬盘上不存在outputFile目录,则创建这个目录
File mydir = new File(outputFile);
if (!(mydir.exists())) {
mydir.mkdir();
}
// 备份文件名
String fileName = "soa" + day + ".dmp";
// 将备份好的数据库文件放在服务器端的outputFile目录下
// 得到数据库的连接信息
// 得到物理路径,取得applicationContext.xml中数据库定义,
//如url,usrname,password,O/R映射文件等内容
// ServletContext m_application = this.getServlet().getServletContext();
// String path = m_application.getRealPath("/");
// String filename = path + "WEB-INF//applicationContext.xml";
// WriteXMLWithDom wxf = new WriteXMLWithDom();
// String[] str = wxf.GetOralAddrWithDom(filename);
/* if (str[0].equalsIgnoreCase("")) {
return mapping.findForward("failure");
}*/
// str[1] 用户名 ,[2] 用户密码 ,
// 服务器地址,[0]
String command = "exp "+"bgxx_name"+"/"+"bgxx"+"@"+"bgxx"+" file=" + outputFile+ fileName;
Runtime rt = Runtime.getRuntime();
Process process=null;
try {
process=rt.exec(command);
//rt.exec(command);
process.waitFor();
// 假等待一下,目的是让其能在文件列表中列出,可以process.destroy()得到
//Thread.sleep(20000);
//rt.freeMemory();
process.destroy();
} catch (Exception e) {
//捕捉异常
// String errorSORT = Constant.errorBeifen;
request.setAttribute("ERRORSORT", "坏了");
String error = "数据库备份操作失败,请稍候再试! ";
request.setAttribute("PUBLICINFERROR", error);
return mapping.findForward("backUp");
}
// p.destroy();
String success="数据库备份成功,您的数据已经备份在!"+ Constant.DataBaseBackUpPath;
request.setAttribute("PUBLICINFERROR", success);
return mapping.findForward("backUp");
}
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Calendar cDay = Calendar.getInstance();
SimpleDateFormat myf = new SimpleDateFormat("yyyyMMddHHmmss");
String day = myf.format(cDay.getTime());
// 获得Constant中定义的数据库备份文件存放路径,格式如:
// public static final String DataBaseBackUpPath = "D://sOADB//";
String outputFile = Constant.DataBaseBackUpPath;
// 如果服务器端硬盘上不存在outputFile目录,则创建这个目录
File mydir = new File(outputFile);
if (!(mydir.exists())) {
mydir.mkdir();
}
// 备份文件名
String fileName = "soa" + day + ".dmp";
// 将备份好的数据库文件放在服务器端的outputFile目录下
// 得到数据库的连接信息
// 得到物理路径,取得applicationContext.xml中数据库定义,
//如url,usrname,password,O/R映射文件等内容
// ServletContext m_application = this.getServlet().getServletContext();
// String path = m_application.getRealPath("/");
// String filename = path + "WEB-INF//applicationContext.xml";
// WriteXMLWithDom wxf = new WriteXMLWithDom();
// String[] str = wxf.GetOralAddrWithDom(filename);
/* if (str[0].equalsIgnoreCase("")) {
return mapping.findForward("failure");
}*/
// str[1] 用户名 ,[2] 用户密码 ,
// 服务器地址,[0]
String command = "exp "+"bgxx_name"+"/"+"bgxx"+"@"+"bgxx"+" file=" + outputFile+ fileName;
Runtime rt = Runtime.getRuntime();
Process process=null;
try {
process=rt.exec(command);
//rt.exec(command);
process.waitFor();
// 假等待一下,目的是让其能在文件列表中列出,可以process.destroy()得到
//Thread.sleep(20000);
//rt.freeMemory();
process.destroy();
} catch (Exception e) {
//捕捉异常
// String errorSORT = Constant.errorBeifen;
request.setAttribute("ERRORSORT", "坏了");
String error = "数据库备份操作失败,请稍候再试! ";
request.setAttribute("PUBLICINFERROR", error);
return mapping.findForward("backUp");
}
// p.destroy();
String success="数据库备份成功,您的数据已经备份在!"+ Constant.DataBaseBackUpPath;
request.setAttribute("PUBLICINFERROR", success);
return mapping.findForward("backUp");
}
///
create or replace procedure backup_table_to_file /*功能:将ORACLE的任意表数据导出(标准格式) */ ( v_tablename varchar2, /*需要导出的表名*/ v_path varchar2, /*服务器上导出数据文件的目录*/ v_string varchar2, /*服务器上导出数据文件的文件名*/ v_where varchar2, /*查询条件*/ v_flag varchar2, /*写数据文件的方式 w:重写 a:追加*/ outflag out varchar2 /*返回处理结果 0 成果、1 写入目录有误、2 表名有误、3 写入目录不能为空、4 写入文件方式有误、5 查询条件有误、6 其他错误*/ ) is file_handle utl_file.file_type; path_notinput_exception EXCEPTION; table_notfind_exception EXCEPTION; write_type_exception EXCEPTION; type ref_cursor_type is REF CURSOR; cursor_select ref_cursor_type; outputline varchar2(1000) ; select_cname varchar2(1000) ; w_flag varchar2(10); get_cname varchar2(1000) ; put_cname varchar2(1000) ; temp varchar2(1000) ; resault varchar2(1000) ; filepath varchar2(100) ; filename varchar2(100) ; i integer; begin outflag :='0'; IF (v_path is null) THEN --初始化服务器文件夹 RAISE path_notinput_exception; ELSE filepath := rtrim(trim(v_path),'/'); END IF; --初始化服务器文件夹 get_cname := ''; temp := nls_upper(v_tablename); if (v_flag is null) then --初始化写文件方式 w_flag := 'w'; else w_flag := v_flag; end if; --初始化写文件方式 if w_flag in ('W','w','A','a') then select_cname := 'select cname from col where tname = '||''''||temp||''''; OPEN cursor_select for select_cname; FETCH cursor_select into get_cname; IF (get_cname is null) THEN RAISE table_notfind_exception; END IF; put_cname := ''''||'"'||''''||'||'||get_cname||'||'||''''||'"'||''''; WHILE cursor_select%FOUND LOOP FETCH cursor_select into get_cname; EXIT WHEN cursor_select%NOTFOUND; put_cname :=put_cname ||'||'||''''||','||''''||'||'||''''||'"'||''''||'||'||get_cname||'||'||''''||'"'||''''; END LOOP; CLOSE cursor_select; IF (v_string is null) then select_cname := 'select to_char(sysdate,'||''''||'yyyymmdd'||''''||') from DUAL'; OPEN cursor_select for select_cname; FETCH cursor_select into filename; CLOSE cursor_select; filename := v_tablename||'.'||filename||'.txt'; ELSE filename := v_string; END IF; file_handle :=utl_file.fopen(filepath,filename,w_flag); select_cname := 'select '||put_cname||' from '||temp||' '||v_where; resault := ''; OPEN cursor_select for select_cname; FETCH cursor_select into resault; WHILE cursor_select%FOUND LOOP outputline := resault; utl_file.put_line(file_handle,outputline); FETCH cursor_select into resault; END LOOP; CLOSE cursor_select; utl_file.fclose(file_handle); ELSE RAISE write_type_exception; end if; exception when utl_file.invalid_path then outflag :='1'; --raise_application_error(-20001,'错误:主机的写入文件目录有误!'); when table_notfind_exception then outflag :='2'; --raise_application_error(-20002,'错误:输入的表名有误!'); when path_notinput_exception then outflag :='3'; --raise_application_error(-20003,'错误:服务器的写入文件目录为必输项!'); when write_type_exception then outflag :='4'; --raise_application_error(-20004,'错误:写入文件方式有误!'); when others then if sqlcode like '-9%' then outflag :='5'; --raise_application_error(-20005,'错误:查询条件有误!'); else outflag :='6'; raise_application_error(sqlcode,sqlerrm); end if; end backup_table_to_file;
相关文章推荐
- Oracle 12c利用数据泵DataPump进行Oracle数据库备份
- Oracle数据库用户数据完整备份与恢复
- Oracle数据库按时间进行分组统计数据的方法
- java、Extjs导出数据库里的数据至Excel并下载至本地进行数据备份方法之二——使用Action
- 第7课 oracle数据库的管理及数据备份,恢复
- oracle数据库-备份ORACLE为dmp类型数据
- 编写脚本令Xtrabackup对MySQL数据进行备份的教程
- 使用mysqldump对MySQL的数据进行备份的操作教程
- oracle数据库备份(2)——Oracle 的数据导出导入命令:exp、imp
- 【EXPDP】使用11g的数据泵实现对逻辑备份数据进行加密
- 备份软件对oracle数据库进行恢复(BE对linux下oracle恢复)
- Oracle数据库备份与恢复数据_包(命名空间)
- 用触发器对sqlserver进行数据备份
- 用触发器对SQL Server数据库进行数据备份
- Oracle数据库备份数据缺失的问题
- 如何进行邮件数据备份
- oracle数据库直接对表数据进行修改
- 在Oracle数据库中,用于数据备份的触发器及表结构
- oracle中em不能进行数据备份等操作
- Oracle数据库日常管理之数据备份,恢复及迁移 (第九讲 )