import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 读取属性文件
* @author cf
* @Title
* @param
*/
public class ReadProperties {
/*
* 读取属性文件
*/
public static Properties getPprVue(String properName) {
InputStream inputStream = ReadProperties.class.getClassLoader()
.getResourceAsStream(properName);
Properties p = new Properties();
try {
p.load(inputStream);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return p;
}
public static void main(String[] args) {
// Properties p = getPprVue("db.properties");
// System.out.println(p.getProperty("jdbc.url"));
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import com.gh.LuckDrawSys.utlis.GetDate;
/**
* MySQL数据库备份
*
* @author cf
*/
public class MySQLDatabaseBackup {
/**
* Java代码实现MySQL数据库导出
*
* @author cf
* @param hostIP
* MySQL数据库所在服务器地址IP
* @param userName
* 进入数据库所需要的用户名
* @param password
* 进入数据库所需要的密码
* @param savePath
* 数据库导出文件保存路径
* @param fileName
* 数据库导出文件文件名
* @param dbOrTbName
* 要导出的数据库名 表名
* @return 返回true表示导出成功,否则返回false。
*/
public static boolean exportDatabaseTool(String hostIP, String userName,
String password, String savePath, String fileName, String dbOrTbName)
throws InterruptedException {
File saveFile = new File(savePath);
if (!saveFile.exists()) {// 如果目录不存在
System.out.println("目录不存在");
// saveFile.mkdirs();// 创建文件夹
return false;
}
if (!savePath.endsWith(File.separator)) {
savePath = savePath + File.separator;
}
PrintWriter printWriter = null;
BufferedReader bufferedReader = null;
try {
printWriter = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(savePath + fileName), "utf8"));
Process process = Runtime.getRuntime().exec(
" mysqldump -h" + hostIP + " -u" + userName + " -p"
+ password + " --set-charset=UTF8 " + dbOrTbName);
InputStreamReader inputStreamReader = new InputStreamReader(
process.getInputStream(), "utf8");
bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
printWriter.println(line);
}
printWriter.flush();
if (process.waitFor() == 0) {// 0 表示线程正常终止。
return true;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
if (printWriter != null) {
printWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
/**
* java实现mysql数据库还原
*
* @param username 用户名
* @param password 密码
* @param sqlPath 还原文件路径
* @param databaseName 数据库名称
* @return boolean
* @throws InterruptedException
*/
public static boolean importDatabaseTool(String username, String password,
String sqlPath, String databaseName) throws InterruptedException {
// 创建数据库
// String stmt1 = "mysqladmin -u" + username + " -p" + pwd
// + " create finacing";
String stmt2 = "mysql -u" + username + " -p" + password + databaseName
+ " < " + sqlPath;
String[] cmd = { "cmd", "/c", stmt2 };
try {
// Runtime.getRuntime().exec(stmt1);
Process p = Runtime.getRuntime().exec(cmd);
System.out.println("命令执行完毕");
if (p.waitFor() == 0) {// 0 表示线程正常终止。
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static void main(String[] args) throws IOException,
InterruptedException {
// 读取数据库信息
Properties pros = ReadProperties.getPprVue("db.properties");
String username = pros.getProperty("jdbc.username");
String password = pros.getProperty("jdbc.password");
String hostIP = pros.getProperty("jdbc.ip");
// 备份路径
String savePath = "F:/backupDatabase";
// 获取时间拼接备份文件名
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = GetDate.getTime(new Date(), 0);
String time = sdf.format(date);
String fileName = time + ".sql";
// 备份数据库或者具体表
String dbOrTbName = "cq_edu_activity record prizerecord";
try {
// 调用备份方法
if (exportDatabaseTool(hostIP, username, password, savePath,
fileName, dbOrTbName)) {
System.out.println("数据库成功备份!!!");
} else {
System.out.println("数据库备份失败!!!");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
// 条用还原方法
// if (importDatabaseTool(username, password, savePath+"/"+fileName, " cq_edu_activity")) {
// System.out.println("数据已从 " + savePath+"/"+fileName + " 导入到数据库中");
// }else{
// System.out.println("数据库还原失败!!!");
// }
}
}
import java.io.InputStream;
import java.util.Properties;
/**
* 读取属性文件
* @author cf
* @Title
* @param
*/
public class ReadProperties {
/*
* 读取属性文件
*/
public static Properties getPprVue(String properName) {
InputStream inputStream = ReadProperties.class.getClassLoader()
.getResourceAsStream(properName);
Properties p = new Properties();
try {
p.load(inputStream);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return p;
}
public static void main(String[] args) {
// Properties p = getPprVue("db.properties");
// System.out.println(p.getProperty("jdbc.url"));
}
}
import java.io.BufferedReader;
import java.io.File;import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import com.gh.LuckDrawSys.utlis.GetDate;
/**
* MySQL数据库备份
*
* @author cf
*/
public class MySQLDatabaseBackup {
/**
* Java代码实现MySQL数据库导出
*
* @author cf
* @param hostIP
* MySQL数据库所在服务器地址IP
* @param userName
* 进入数据库所需要的用户名
* @param password
* 进入数据库所需要的密码
* @param savePath
* 数据库导出文件保存路径
* @param fileName
* 数据库导出文件文件名
* @param dbOrTbName
* 要导出的数据库名 表名
* @return 返回true表示导出成功,否则返回false。
*/
public static boolean exportDatabaseTool(String hostIP, String userName,
String password, String savePath, String fileName, String dbOrTbName)
throws InterruptedException {
File saveFile = new File(savePath);
if (!saveFile.exists()) {// 如果目录不存在
System.out.println("目录不存在");
// saveFile.mkdirs();// 创建文件夹
return false;
}
if (!savePath.endsWith(File.separator)) {
savePath = savePath + File.separator;
}
PrintWriter printWriter = null;
BufferedReader bufferedReader = null;
try {
printWriter = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(savePath + fileName), "utf8"));
Process process = Runtime.getRuntime().exec(
" mysqldump -h" + hostIP + " -u" + userName + " -p"
+ password + " --set-charset=UTF8 " + dbOrTbName);
InputStreamReader inputStreamReader = new InputStreamReader(
process.getInputStream(), "utf8");
bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
printWriter.println(line);
}
printWriter.flush();
if (process.waitFor() == 0) {// 0 表示线程正常终止。
return true;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
if (printWriter != null) {
printWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
/**
* java实现mysql数据库还原
*
* @param username 用户名
* @param password 密码
* @param sqlPath 还原文件路径
* @param databaseName 数据库名称
* @return boolean
* @throws InterruptedException
*/
public static boolean importDatabaseTool(String username, String password,
String sqlPath, String databaseName) throws InterruptedException {
// 创建数据库
// String stmt1 = "mysqladmin -u" + username + " -p" + pwd
// + " create finacing";
String stmt2 = "mysql -u" + username + " -p" + password + databaseName
+ " < " + sqlPath;
String[] cmd = { "cmd", "/c", stmt2 };
try {
// Runtime.getRuntime().exec(stmt1);
Process p = Runtime.getRuntime().exec(cmd);
System.out.println("命令执行完毕");
if (p.waitFor() == 0) {// 0 表示线程正常终止。
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static void main(String[] args) throws IOException,
InterruptedException {
// 读取数据库信息
Properties pros = ReadProperties.getPprVue("db.properties");
String username = pros.getProperty("jdbc.username");
String password = pros.getProperty("jdbc.password");
String hostIP = pros.getProperty("jdbc.ip");
// 备份路径
String savePath = "F:/backupDatabase";
// 获取时间拼接备份文件名
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = GetDate.getTime(new Date(), 0);
String time = sdf.format(date);
String fileName = time + ".sql";
// 备份数据库或者具体表
String dbOrTbName = "cq_edu_activity record prizerecord";
try {
// 调用备份方法
if (exportDatabaseTool(hostIP, username, password, savePath,
fileName, dbOrTbName)) {
System.out.println("数据库成功备份!!!");
} else {
System.out.println("数据库备份失败!!!");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
// 条用还原方法
// if (importDatabaseTool(username, password, savePath+"/"+fileName, " cq_edu_activity")) {
// System.out.println("数据已从 " + savePath+"/"+fileName + " 导入到数据库中");
// }else{
// System.out.println("数据库还原失败!!!");
// }
}
}