即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

java导入导出mysql数据库

编程语言 wujie_cnhn123456 11℃ 0评论

导出:

runtime.exec("cmd /c mysqldump -uroot -p123456 -h127.0.0.1 -P3306 --routines --default-character-set=utf8 --no-create-db --no-create-info --tables talos t_project_ext> "+sqlPath.toString(), null, new File(buff.toString()));



导入:

Runtime runtime = Runtime.getRuntime();
runtime.exec("cmd /c mysql -h localhost -uroot -p123456 talos



==================================================================


public boolean getExportBak(String tableName) {

        StringBuffer buff = new StringBuffer(PathKit.getWebRootPath()+"/plugin"); // 插件目录
        String os = System.getProperty("os.name");
        String modelDir = (os.toLowerCase().startsWith("win") ? PropKit.get("export.data.window.path") : PropKit.get("export.data.linux.path")) + "/"+System.currentTimeMillis(); // sql文件导出目录
        StringBuffer sqlPath = new StringBuffer(modelDir+"/" + tableName + ".sql"); // 导出文件路径
        File fileParent = new File(modelDir);
        if(!fileParent.exists()){ // 创建文件夹
            fileParent.mkdirs();
        }
        try {
            StringBuffer cmdStr = new StringBuffer();

            Runtime runtime = Runtime.getRuntime();
            runtime.exec("cmd /c mysqldump -u"+PropKit.get("user")+" -p"+PropKit.get("password")+" -h127.0.0.1 -P3306 --routines --default-character-set=utf8 --no-create-db --no-create-info --tables talos "+tableName+"> "+sqlPath.toString(), null, new File(buff.toString()));
            File cfile = new File(sqlPath.toString());
            while (!cfile.exists()) {
                Thread.sleep(200);
            }
            // 加密文本
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    new FileInputStream(cfile), "UTF-8"));
            String line = null;
            StringBuffer textStr = new StringBuffer();
            while((line = in.readLine())!=null) {
                textStr.append(line);
            }
            String aesText = AESKit.encode(PropKit.get("export.data.secret"), textStr.toString()); // 文本加密
            PrintStream ps = null;
            try {
                File file = new File(sqlPath.toString());
                ps = new PrintStream(new FileOutputStream(file));
                ps.println(aesText);// 往文件里写入字符串
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return false;
            } finally {
                in.close();
                ps.close();
            }
//            System.out.println(AESKit.dncode(PropKit.get("export.data.secret"), aesText)); // 解密
        } catch(Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }






转载请注明:CodingBlog » java导入导出mysql数据库

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情