导入SQL
方法一:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
public static void exportSQL() {
try {
Runtime rt = Runtime.getRuntime();
Process child = rt.exec("/usr/local/mysql/bin/mysqldump -uroot -p123 -h127.0.0.1 yellow_db");
InputStream in = child.getInputStream();
InputStreamReader xx = new InputStreamReader(in, "utf-8");
String inStr;
StringBuffer sb = new StringBuffer("");
String outStr;
BufferedReader br = new BufferedReader(xx);
while ((inStr = br.readLine()) != null) {
sb.append(inStr + "\r\n");
}
outStr = sb.toString();
FileOutputStream fout;
Long startTs = System.currentTimeMillis();
StringBuffer p = new StringBuffer("/Users/hes/Documents/SQL/t_user.sql");
p.insert(31, startTs.toString());
fout = new FileOutputStream(p.toString());
OutputStreamWriter writer = new OutputStreamWriter(fout, "utf-8");
writer.write(outStr);
writer.flush();
in.close();
xx.close();
br.close();
writer.close();
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
方法二
public static boolean exportDatabaseTool(String hostIP, String userName, String password, String savePath,
String fileName, String databaseName) {
File saveFile = new File(savePath);
if (!saveFile.exists()) {
saveFile.mkdirs();
}
if (!savePath.endsWith(File.separator)) {
savePath = savePath + File.separator;
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(" --user=").append(userName).append(" --password=").append(password)
.append(" --lock-all-tables=true");
stringBuilder.append(" --result-file=").append(savePath + fileName).append(" --default-character-set=utf8 ")
.append(databaseName);
try {
Process process = Runtime.getRuntime().exec(stringBuilder.toString());
if (process.waitFor() == 0) {
return true;
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return false;
}
导入sql文件
import java.io.File;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.OutputStream;
private static String[] getImportCommand() {
String username = "root";
String password = "123";
String host = "localhost";
String port = "3306";
String importDatabaseName = "admin";
String importPath = "/Users/hes/Documents/SQL/temp.sql";
String loginCommand = new StringBuffer().append("/usr/local/mysql/bin/").append("mysql -u").append(username).append(" -p").append(password).append(" -h").append(host)
.append(" -P").append(port).toString();
String switchCommand = new StringBuffer("use ").append(importDatabaseName).toString();
String importCommand = new StringBuffer("source ").append(importPath).toString();
String[] commands = new String[] {loginCommand, switchCommand, importCommand};
return commands;
}
public static void importSql() {
try {
Runtime runtime = Runtime.getRuntime();
String cmdarray[] = getImportCommand();
Process process = runtime.exec(cmdarray[0]);
OutputStream os = process.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(os);
writer.write(cmdarray[1] + "\r\n" + cmdarray[2]);
writer.flush();
writer.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}