Mysql database backup-java code _ MySQL-mysql tutorial

Source: Internet
Author: User
Tags mysql tutorial
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 ");}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.