亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

通過java備份恢復(fù)mysql數(shù)據(jù)庫的實(shí)現(xiàn)代碼

 更新時(shí)間:2013年09月09日 11:51:59   作者:  
這篇文章主要介紹了如何通過java備份恢復(fù)mysql數(shù)據(jù)庫,其實(shí)一般情況下通過bat或sh就可以,這里主要是介紹了java的實(shí)現(xiàn)思路,喜歡的朋友可以參考下
復(fù)制代碼 代碼如下:

import java.io.BufferedReader;
import java.io.File;
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;

public class Test {
    public static void main(String[] args) throws IOException{
        backup("d:\\\\d.sql");
        recover("d:\\\\d.sql");
    }
    public static void backup(String path) throws IOException{
        Runtime runtime = Runtime.getRuntime();
        //-u后面是用戶名,-p是密碼-p后面最好不要有空格,-family是數(shù)據(jù)庫的名字
        Process process = runtime.exec("mysqldump -u root -p123456 family");
        InputStream inputStream = process.getInputStream();//得到輸入流,寫成.sql文件
        InputStreamReader reader = new InputStreamReader(inputStream);
        BufferedReader br = new BufferedReader(reader);
        String s = null;
        StringBuffer sb = new StringBuffer();
        while((s = br.readLine()) != null){
            sb.append(s+"\\r\\n");
        }
        s = sb.toString();
        System.out.println(s);
        File file = new File(path);
        file.getParentFile().mkdirs();
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(s.getBytes());
        fileOutputStream.close();
        br.close();
        reader.close();
        inputStream.close();
    }
    public static void recover(String path) throws IOException{
        Runtime runtime = Runtime.getRuntime();
        //-u后面是用戶名,-p是密碼-p后面最好不要有空格,-family是數(shù)據(jù)庫的名字,--default-character-set=utf8,這句話一定的加
        //我就是因?yàn)檫@句話沒加導(dǎo)致程序運(yùn)行成功,但是數(shù)據(jù)庫里面的內(nèi)容還是以前的內(nèi)容,最好寫上完成的sql放到cmd中一運(yùn)行才知道報(bào)錯(cuò)了
        //錯(cuò)誤信息:
        //mysql: Character set 'utf-8' is not a compiled character set and is not specified in the '
        //C:\\Program Files\\MySQL\\MySQL Server 5.5\\share\\charsets\\Index.xml' file ERROR 2019 (HY000): Can't
        // initialize character set utf-8 (path: C:\\Program Files\\MySQL\\MySQL Server 5.5\\share\\charsets\\),
        //又是討人厭的編碼問題,在恢復(fù)的時(shí)候設(shè)置一下默認(rèn)的編碼就可以了。
        Process process = runtime.exec("mysql -u root -p123456 --default-character-set=utf8 family");
        OutputStream outputStream = process.getOutputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
        String str = null;
        StringBuffer sb = new StringBuffer();
        while((str = br.readLine()) != null){
            sb.append(str+"\\r\\n");
        }
        str = sb.toString();
        System.out.println(str);
        OutputStreamWriter writer = new OutputStreamWriter(outputStream,"utf-8");
        writer.write(str);
        writer.flush();
        outputStream.close();
        br.close();
        writer.close();
    }
}

相關(guān)文章

最新評論