Mysql database backup-java code
Import java. io. bufferedReader; import java. io. bufferedWriter; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java. io. outputStream; import java. io. outputStreamWriter; // database backup public class MySQLDump {public static boolean sqlDump (String cmd, String filePath) {boolean falg = false; try {Runtime run = Runtime. getRuntime (); // cmd command: "C:/Program Files/MySQL Server 5.1/bin/mysqldump-uroot-proot email" Process p = run.exe c (cmd ); inputStream is = p. getInputStream (); // console output information as input stream InputStreamReader isr = new InputStreamReader (is, "UTF-8"); // Set input stream encoding format BufferedReader br = new BufferedReader (isr ); // write the console input information to the file output stream. FileOutputStream fos = new FileOutputStream (filePath); BufferedWriter bw = new Buffer EdWriter (new OutputStreamWriter (fos, "UTF-8"); String temp = null; while (temp = br. readLine ())! = Null) {bw. write (temp); bw. newLine ();} bw. flush (); bw. close (); br. close (); falg = true; System. out. println ("/* Dump SQL File" + filePath + "OK! */");} Catch (IOException e) {throw new RuntimeException (" add the mysql command to path! ", E) ;}return falg ;}// restore database/***** import **/public static void sqlLoad (String cmd, String sqlPath) {try {Runtime rt = Runtime. getRuntime (); // call mysql cmd: C:/Program Files/MySQL Server 5.1/bin/mysql.exe-uroot-proot email Process child = rt.exe c (cmd ); outputStream out = child. getOutputStream (); // The input information in the console is used as the output stream // the input stream BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputS Tream (sqlPath), "utf8"); // output stream OutputStreamWriter writer = new OutputStreamWriter (out, "utf8"); String inStr; while (inStr = br. readLine ())! = Null) {writer. write (inStr); writer. write ("/r/n");} writer. flush (); // do not forget to close the input/output stream out. close (); br. close (); writer. close (); System. out. println ("/* Load SQL File" + sqlPath + "OK! */");} Catch (Exception e) {e. printStackTrace () ;}} public static void main (String [] args) {sqlDump ("C: /Program Files/MySQL Server 5.1/bin/mysqldump-uroot-proot email "," c:/email. SQL "); sqlLoad (" C:/Program Files/MySQL Server 5.1/bin/mysql.exe-uroot-proot email "," c:/email. SQL ");}}