mysql数据库备份

Wesley13
• 阅读 420
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;


//数据库备份
public class MySQLDump {

    public static boolean sqlDump(String cmd,String filePath){
        boolean falg = false;
        try {
            Runtime run = Runtime.getRuntime();
            //cmd 命令:"C:/Program Files/MySQL/MySQL Server 5.1/bin/mysqldump -uroot -proot  email"
            Process p = run.exec(cmd);
            InputStream is =     p.getInputStream();// 控制台的输出信息作为输入流  
            InputStreamReader isr = new InputStreamReader(is,"UTF-8");//设置输入流编码格式
            BufferedReader br = new BufferedReader(isr);
            //将控制台输入信息写入到文件输出流中
            FileOutputStream fos = new FileOutputStream(filePath);
            BufferedWriter bw = new BufferedWriter(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("请将mysql命令添加到path中!",e);
        }
        return falg;
    }
    
    //恢复数据库
    /**  
     * 导入  
     *  
     */  
    public static void sqlLoad(String cmd,String sqlPath) {   
        try {   
            Runtime rt = Runtime.getRuntime();   
  
            // 调用 mysql 的 cmd: C:/Program Files/MySQL/MySQL Server 5.1/bin/mysql.exe -uroot -proot email  
            Process child = rt.exec(cmd);   
            OutputStream out = child.getOutputStream();//控制台的输入信息作为输出流   
            //输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(   
                    new FileInputStream(sqlPath), "utf8"));   
            //输出流
            OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");   
            String inStr;   
            while ((inStr = br.readLine()) != null) {   
                writer.write(inStr);  
                writer.write("\r\n");
            }   
            writer.flush();   
            // 别忘记关闭输入输出流   
            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/MySQL Server 5.1/bin/mysqldump -uroot -proot  email","c:/email.sql");
        sqlLoad("C:/Program Files/MySQL/MySQL Server 5.1/bin/mysql.exe -uroot -proot email","c:/email.sql");
    }

}
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
Java调用bat sh脚本通用类
java调用bat、shell脚本通用类,工作记录。packagetest;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.IOException;importjava.io.InputStr
Stella981 Stella981
3年前
JetBrains IDEs
importjava.io.BufferedReader;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;i
Wesley13 Wesley13
3年前
Java 操作ini文件(往ini文件中读写数据(key
importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStream
Stella981 Stella981
3年前
Android蓝牙连接汽车OBD设备
//设备连接public class BluetoothConnect implements Runnable {    private static final UUID CONNECT_UUID  UUID.fromString("0000110100001000800000805F9B34FB");
Wesley13 Wesley13
3年前
thinkphp 基本配置
12returnarray(34//定义数据库连接信息5'DB\_TYPE''mysql',//指定数据库是mysql67'DB\_HOST''localhost',89'DB\_NAME''uchome',//数据库名1011'DB\_USER''root
Wesley13 Wesley13
3年前
JAVA以UTF
packagetest;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOExc
Wesley13 Wesley13
3年前
Java连接excel实现:通过姓名查找id和通过id查找姓名
注意每个方法结束都要关闭workbook;还有getIdbyname()方法中字符串flag与name的比较,一定要用equals()方法!!!;剩下的不多解释,注释都在代码中:importjava.io.FileInputStream;importjava.io.IOException;importjava.i
Wesley13 Wesley13
3年前
FTP上传下载文件工具类
packagecom.taotao.utils;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;i