JSP 페이지 에서 JAVA 방법 을 호출 하여 MySQL 데이터베이스 백업 과 복 구 를 실현 합 니 다.
package cn.qm.db;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Command {
/*
public static void main(String[] args) throws IOException {
Command com = new Command();
com.backupDatebase("localhost","root","root", "JXC", "D:/jxc.sql");
}
/**
* dos
* @param cmd
* @return
*/
public String execCmd(String cmd) {
StringBuffer sb = new StringBuffer("");
StringBuffer str = new StringBuffer();
str.append("cmd.exe /c \"").append(cmd).append("\"");
System.out.println(str); //
Process ls_proc;
try {
ls_proc = Runtime.getRuntime().exec(str.toString());
BufferedReader in = new BufferedReader(
new InputStreamReader(
new DataInputStream(ls_proc.getInputStream())));
String ss = "";
while((ss = in.readLine()) != null) {
sb.append(ss).append("
");
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* mysql
* @param ip
* @param username
* @param password
* @param datebaseName
* @param filePath
* @return
*/
public boolean backupDatebase(String ip, String username, String password,String datebaseName, String filePath) {
String strCommand = "mysqldump -h "+ip+" -u" + username + " -p" + password + " " + datebaseName + " > " + filePath;
String result = execCmd(strCommand);
System.out.println(result);
return true;
}
/**
*
* @param result
* @return
*/
public boolean check(String result) {
return true;
}
}
JSP 페이지 에서 이 JAVA 클래스 만 호출 하면 됩 니 다.(파일 이름 은.sql 만 가능 합 니 다)
<%@ page language="java" import="java.util.*,cn.qm.db.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title> </title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Command com = new Command();
String ip = "localhost";//ip
String username = "root";//MySQL
String password = "root";//MySQL
String database = "JXC";//
String url = "D:/jxc.sql";//
boolean check = com.backupDatebase(ip,username,password,database,url);
if(check){
%>
<%} %>
</body>
</html>
다음은 데 이 터 를 복구 하 는 코드 입 니 다.
package cn.qm.db;
import java.io.*;
import java.lang.*;
/*
* MySql
* */
public class Recover {
public boolean load(){
String filepath = "d:\\jxc.sql"; //
// test
String stmt1 = "mysqladmin -u root -proot create jxctest";
String stmt2 = "mysql -u root -proot jxctest < " + filepath;
String[] cmd = { "cmd", "/c", stmt2 };
try {
Runtime.getRuntime().exec(stmt1);
Runtime.getRuntime().exec(cmd);
System.out.println(" " + filepath + " ");
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
}
<%@ page language="java" import="java.util.*,cn.qm.db.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title> </title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Recover com = new Recover();
String url = "D:/jxc.sql";
boolean check = com.load();
if(check){
%>
<%} %>
</body>
</html>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.