All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dennisit.vplus.data.utils.DbUtils Maven / Gradle / Ivy

There is a newer version: 2.0.8
Show newest version
package com.github.dennisit.vplus.data.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;

/**
 * Created by Elon.su on 2018/5/9.
 */
@Slf4j
public class DbUtils {

    /**
     * 备份数据库
     *
     * @param hostIp       ip
     * @param userName     用户名
     * @param password     密码
     * @param savePath     保存路径
     * @param fileName     文件名
     * @param databaseName 数据库名
     * @return boolean
     * @throws InterruptedException InterruptedException
     */
    public static boolean exportDatabase(String hostIp, String userName, String password, String savePath, String fileName, String databaseName) throws InterruptedException {
        File saveFile = new File(savePath);
        if (!saveFile.exists()) {
            saveFile.mkdirs();
        }
        if (!savePath.endsWith(File.separator)) {
            savePath = savePath + File.separator;
        }

        PrintWriter printWriter = null;
        BufferedReader bufferedReader = null;
        try {
            printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName), "utf-8"));
            Process process = Runtime.getRuntime().exec(" mysqldump -h" + hostIp + " -u" + userName + " -p" + password + " --set-charset=UTF8 " + databaseName);
            InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                printWriter.println(line);
            }
            printWriter.flush();
            if (process.waitFor() == 0) {
                return true;
            }
        } catch (IOException e) {
            log.error(e.getLocalizedMessage(), e);
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (printWriter != null) {
                    printWriter.close();
                }
            } catch (IOException e) {
                log.error(e.getLocalizedMessage(), e);
            }
        }
        return false;
    }

    /**
     * 将结果集存储为CSV文件
     *
     * @param rs           结果集
     * @param pathWithName 要存储到文件的路径
     * @param charset      字符集
     */
    public static void toCSV(ResultSet rs, String pathWithName, String charset) {
        try {
            PrintWriter writer = FileWriteUtils.getPrintWriter(pathWithName, charset, false);
            while (rs.next()) {
                int count = rs.getMetaData().getColumnCount();
                // 处理一行
                StringBuffer sb = new StringBuffer();
                for (int i = 1; i <= count; i++) {
                    sb.append(rs.getObject(i));
                    if (i == count)
                        break;
                    sb.append(",");
                }
                String line = sb.toString();
                log.debug("写入:" + line);
                writer.println(line);
            }
            writer.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy