|
|
@@ -0,0 +1,445 @@
|
|
|
+package com.xunmei.system.util;
|
|
|
+
|
|
|
+import com.xunmei.common.security.utils.SecurityUtils;
|
|
|
+
|
|
|
+import java.io.BufferedWriter;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileWriter;
|
|
|
+import java.io.IOException;
|
|
|
+import java.sql.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class ExportDatabaseUtil {
|
|
|
+ /**
|
|
|
+ * 根据数据库连接信息,导出数据库
|
|
|
+ * @param url 数据库连接信息
|
|
|
+ * @param username 数据库用户名
|
|
|
+ * @param password 数据库密码
|
|
|
+ */
|
|
|
+ public static void exportSql(String url, String username, String password, File file){
|
|
|
+ Connection conn = null;
|
|
|
+ Statement stmt = null;
|
|
|
+ ResultSet rs = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 建立与数据库的连接
|
|
|
+ conn = DriverManager.getConnection(url, username, password);
|
|
|
+
|
|
|
+ // 创建Statement对象
|
|
|
+ stmt = conn.createStatement();
|
|
|
+
|
|
|
+ // 获取数据库表名
|
|
|
+ List<String> tables = getTables(stmt,rs);
|
|
|
+ FileWriter fw = new FileWriter(file, false);
|
|
|
+ BufferedWriter bw = new BufferedWriter(fw);
|
|
|
+
|
|
|
+ //写入table
|
|
|
+ writeTable(tables,stmt,rs,bw);
|
|
|
+ // 关闭资源
|
|
|
+ fw.close();
|
|
|
+
|
|
|
+ System.out.println("数据库已成功导出!");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ // 关闭资源
|
|
|
+ try {
|
|
|
+ if (rs != null) {
|
|
|
+ rs.close();
|
|
|
+ }
|
|
|
+ if (stmt != null) {
|
|
|
+ stmt.close();
|
|
|
+ }
|
|
|
+ if (conn != null) {
|
|
|
+ conn.close();
|
|
|
+ }
|
|
|
+ } catch (SQLException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据表名导出sql语句
|
|
|
+ * @param url
|
|
|
+ * @param username
|
|
|
+ * @param password
|
|
|
+ * @param tableName
|
|
|
+ */
|
|
|
+ public static void exportTableSql(String url,String username,String password,String tableName, File file){
|
|
|
+ Connection conn = null;
|
|
|
+ Statement stmt = null;
|
|
|
+ ResultSet rs = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 建立与数据库的连接
|
|
|
+ conn = DriverManager.getConnection(url, username, password);
|
|
|
+
|
|
|
+ // 创建Statement对象
|
|
|
+ stmt = conn.createStatement();
|
|
|
+
|
|
|
+ // 获取数据库表名
|
|
|
+ List<String> tables = getTables(stmt,rs);
|
|
|
+
|
|
|
+
|
|
|
+ FileWriter fw = new FileWriter(file, false);
|
|
|
+ BufferedWriter bw = new BufferedWriter(fw);
|
|
|
+
|
|
|
+ //写入table
|
|
|
+ writeTable(tables,stmt,rs,bw,tableName);
|
|
|
+ // 关闭资源
|
|
|
+ fw.close();
|
|
|
+
|
|
|
+ System.out.println("数据库已成功导出!");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ // 关闭资源
|
|
|
+ try {
|
|
|
+ if (rs != null) {
|
|
|
+ rs.close();
|
|
|
+ }
|
|
|
+ if (stmt != null) {
|
|
|
+ stmt.close();
|
|
|
+ }
|
|
|
+ if (conn != null) {
|
|
|
+ conn.close();
|
|
|
+ }
|
|
|
+ } catch (SQLException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据表名导出sql语句
|
|
|
+ * @param url
|
|
|
+ * @param username
|
|
|
+ * @param password
|
|
|
+ * @param tableNames
|
|
|
+ */
|
|
|
+ public static void exportTableSql(String url,String username,String password,List<String> tableNames, File file){
|
|
|
+ Connection conn = null;
|
|
|
+ Statement stmt = null;
|
|
|
+ ResultSet rs = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 建立与数据库的连接
|
|
|
+ conn = DriverManager.getConnection(url, username, password);
|
|
|
+
|
|
|
+ // 创建Statement对象
|
|
|
+ stmt = conn.createStatement();
|
|
|
+
|
|
|
+ // 获取数据库表名
|
|
|
+ List<String> tables = getTables(stmt,rs);
|
|
|
+
|
|
|
+
|
|
|
+ FileWriter fw = new FileWriter(file, false);
|
|
|
+ BufferedWriter bw = new BufferedWriter(fw);
|
|
|
+
|
|
|
+ //写入table
|
|
|
+ writeTable(tables,stmt,rs,bw,tableNames);
|
|
|
+ // 关闭资源
|
|
|
+ fw.close();
|
|
|
+
|
|
|
+ System.out.println("数据库已成功导出!");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ // 关闭资源
|
|
|
+ try {
|
|
|
+ if (rs != null) {
|
|
|
+ rs.close();
|
|
|
+ }
|
|
|
+ if (stmt != null) {
|
|
|
+ stmt.close();
|
|
|
+ }
|
|
|
+ if (conn != null) {
|
|
|
+ conn.close();
|
|
|
+ }
|
|
|
+ } catch (SQLException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出指定表名列表sql
|
|
|
+ * @param tables
|
|
|
+ * @param stmt
|
|
|
+ * @param rs
|
|
|
+ * @param bw
|
|
|
+ * @param tableNames
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private static void writeTable(List<String> tables,Statement stmt,ResultSet rs,BufferedWriter bw,List<String> tableNames) throws Exception {
|
|
|
+
|
|
|
+ //写入文件头部
|
|
|
+ wirteHeader(bw);
|
|
|
+
|
|
|
+ for (String tableName : tables) {
|
|
|
+ //写入表格导出头部
|
|
|
+ writeTableHeader(bw,tableName);
|
|
|
+
|
|
|
+ //写入删表建表语句
|
|
|
+ writeCreateTableSql(stmt,rs,bw,tableName);
|
|
|
+
|
|
|
+ //写入插入语句
|
|
|
+ writeInsertSql(stmt,rs,bw,tableName);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出指定表名sql
|
|
|
+ * @param tables
|
|
|
+ * @param stmt
|
|
|
+ * @param rs
|
|
|
+ * @param bw
|
|
|
+ * @param tableName
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private static void writeTable(List<String> tables,Statement stmt,ResultSet rs,BufferedWriter bw,String tableName) throws Exception {
|
|
|
+
|
|
|
+ //写入文件头部
|
|
|
+ wirteHeader(bw);
|
|
|
+
|
|
|
+ //写入表格导出头部
|
|
|
+ writeTableHeader(bw,tableName);
|
|
|
+
|
|
|
+ //写入删表建表语句
|
|
|
+ writeCreateTableSql(stmt,rs,bw,tableName);
|
|
|
+
|
|
|
+ //写入插入语句
|
|
|
+ writeInsertSql(stmt,rs,bw,tableName);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 写入sql语句
|
|
|
+ * @param tables 数据库所有表名
|
|
|
+ * @param stmt Statement对象
|
|
|
+ * @param rs 结果对象
|
|
|
+ * @param bw 写入对象
|
|
|
+ * @throws SQLException
|
|
|
+ */
|
|
|
+ private static void writeTable(List<String> tables,Statement stmt,ResultSet rs,BufferedWriter bw) throws Exception {
|
|
|
+
|
|
|
+ //写入文件头部
|
|
|
+ wirteHeader(bw);
|
|
|
+
|
|
|
+ for (String tableName : tables) {
|
|
|
+
|
|
|
+ //写入表格导出头部
|
|
|
+ writeTableHeader(bw,tableName);
|
|
|
+
|
|
|
+ //写入删表建表语句
|
|
|
+ writeCreateTableSql(stmt,rs,bw,tableName);
|
|
|
+
|
|
|
+ //写入插入语句
|
|
|
+ writeInsertSql(stmt,rs,bw,tableName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 写入文件头部
|
|
|
+ * @param bw
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private static void wirteHeader(BufferedWriter bw) throws IOException {
|
|
|
+ bw.write("SET NAMES utf8mb4;");
|
|
|
+ bw.newLine();
|
|
|
+
|
|
|
+ bw.write("SET FOREIGN_KEY_CHECKS = 0;");
|
|
|
+ bw.newLine();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 写入表头部
|
|
|
+ * @param bw
|
|
|
+ * @param tableName
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private static void writeTableHeader(BufferedWriter bw,String tableName) throws IOException {
|
|
|
+ bw.write("");
|
|
|
+ bw.newLine();
|
|
|
+
|
|
|
+ bw.write("-- ----------------------------");
|
|
|
+ bw.newLine();
|
|
|
+
|
|
|
+ bw.write("-- Table structure for " + tableName);
|
|
|
+ bw.newLine();
|
|
|
+
|
|
|
+ bw.write("-- ----------------------------");
|
|
|
+ bw.newLine();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 写入建表语句
|
|
|
+ * @param stmt Statement对象
|
|
|
+ * @param rs ResultSet结果对象
|
|
|
+ * @param bw 写入对象
|
|
|
+ * @param tableName 表名
|
|
|
+ * @throws SQLException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private static void writeCreateTableSql(Statement stmt,ResultSet rs,BufferedWriter bw,String tableName) throws SQLException, IOException {
|
|
|
+ String showCreateTable = "SHOW CREATE TABLE `" + tableName + "`";
|
|
|
+
|
|
|
+ // 生成CREATE TABLE语句
|
|
|
+ rs = stmt.executeQuery(showCreateTable);
|
|
|
+
|
|
|
+ if (rs.next()) {
|
|
|
+
|
|
|
+ //写入删除表语句
|
|
|
+ String dropTableSql = "DROP TABLE IF EXISTS " + tableName + ";";
|
|
|
+ bw.write(dropTableSql);
|
|
|
+ bw.newLine();
|
|
|
+
|
|
|
+ // 写入建表语句
|
|
|
+ String createTableSql = rs.getString(2).replaceAll("\n\t", "\n") + ";";
|
|
|
+ bw.write(createTableSql);
|
|
|
+ bw.newLine();
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 写入insert语句
|
|
|
+ * @param stmt Statement对象
|
|
|
+ * @param rs ResultSet结果对象
|
|
|
+ * @param bw 写入对象
|
|
|
+ * @param tableName 表名
|
|
|
+ */
|
|
|
+ private static void writeInsertSql(Statement stmt,ResultSet rs,BufferedWriter bw,String tableName) throws SQLException, IOException {
|
|
|
+ String columnStr = getColumnStr(stmt, tableName);
|
|
|
+
|
|
|
+ String selectSql = "SELECT "+ columnStr +" FROM `" + tableName + "`";
|
|
|
+ // 生成INSERT INTO语句
|
|
|
+ rs = stmt.executeQuery(selectSql);
|
|
|
+
|
|
|
+
|
|
|
+ int columnCount = rs.getMetaData().getColumnCount();
|
|
|
+ while (rs.next()) {
|
|
|
+ StringBuilder insertIntoBuilder = new StringBuilder();
|
|
|
+ insertIntoBuilder.append("INSERT INTO " ).append(tableName).append(" (").append(columnStr).append(") ")
|
|
|
+ .append(" VALUES (");
|
|
|
+
|
|
|
+ for (int i = 1; i <= columnCount; i++) {
|
|
|
+ int columnType = rs.getMetaData().getColumnType(i);
|
|
|
+ String value = rs.getString(i);
|
|
|
+ if (value == null) {
|
|
|
+ insertIntoBuilder.append("NULL");
|
|
|
+ } else {
|
|
|
+ String val = dealColumnType(columnType,value);
|
|
|
+ insertIntoBuilder.append(val);
|
|
|
+ }
|
|
|
+ if (i < columnCount) {
|
|
|
+ insertIntoBuilder.append(", ");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ insertIntoBuilder.append(");");
|
|
|
+
|
|
|
+ System.out.println(insertIntoBuilder.toString());
|
|
|
+ // 写入输出流
|
|
|
+ bw.write(insertIntoBuilder.toString());
|
|
|
+ bw.newLine();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取数据库的所有表名
|
|
|
+ * @param stmt Statement对象
|
|
|
+ * @param rs 查询结果对象
|
|
|
+ * @return
|
|
|
+ * @throws SQLException
|
|
|
+ */
|
|
|
+ private static List<String> getTables(Statement stmt,ResultSet rs) throws SQLException {
|
|
|
+
|
|
|
+ // 获取所有表名
|
|
|
+ rs = stmt.executeQuery("SHOW TABLES");
|
|
|
+
|
|
|
+ List<String> tables = new ArrayList<>();
|
|
|
+ while (rs.next()){
|
|
|
+ String tableName = rs.getString(1);
|
|
|
+ tables.add(tableName);
|
|
|
+ }
|
|
|
+ return tables;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串转义
|
|
|
+ * @param jsonValue 待转义字符串
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static String escapeJsonSpecialCharacters(String jsonValue) {
|
|
|
+ return jsonValue
|
|
|
+ .replace("\\", "\\\\")
|
|
|
+ .replace("'", "\\'");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 对应字符处理
|
|
|
+ * @param columnType 字段类型
|
|
|
+ * @param value 字段值
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static String dealColumnType(int columnType,String value){
|
|
|
+ if (Types.CHAR == columnType || Types.VARCHAR == columnType || Types.LONGVARCHAR == columnType) {
|
|
|
+ //字符类型处理
|
|
|
+ value = escapeJsonSpecialCharacters(value);
|
|
|
+ value = "'" + value + "'";
|
|
|
+ }else if(Types.SMALLINT == columnType || Types.INTEGER == columnType
|
|
|
+ || Types.BIGINT == columnType || Types.FLOAT == columnType
|
|
|
+ || Types.DOUBLE == columnType || Types.NUMERIC == columnType
|
|
|
+ || Types.DECIMAL == columnType|| Types.TINYINT == columnType){
|
|
|
+ return value;
|
|
|
+ }else if(Types.DATE == columnType || Types.TIME == columnType || Types.TIMESTAMP == columnType){
|
|
|
+ value = "'" + value + "'";
|
|
|
+ }
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 评价查询字段字符串
|
|
|
+ * @param statement 数据库连接信息
|
|
|
+ * @param tableName 表名
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static String getColumnStr(Statement statement,String tableName){
|
|
|
+ String query = "DESCRIBE " + tableName + ";"; // 将mytable替换为你想要获取字段信息的表名
|
|
|
+ try {
|
|
|
+ ResultSet resultSet = statement.executeQuery(query);
|
|
|
+ List<String> cls = new ArrayList<>();
|
|
|
+ while (resultSet.next()) {
|
|
|
+ String columnName = resultSet.getString("Field");
|
|
|
+ columnName = "`" + columnName + "`";
|
|
|
+ cls.add(columnName);
|
|
|
+ }
|
|
|
+ String colums = String.join(",", cls);
|
|
|
+ return colums;
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ //加密
|
|
|
+ String encryptSm4 = SecurityUtils.encryptSm4("select * from core_message_send");
|
|
|
+
|
|
|
+ System.out.println(encryptSm4);
|
|
|
+ //解密
|
|
|
+ String decryptSm4 = SecurityUtils.decryptSm4(encryptSm4);
|
|
|
+
|
|
|
+ System.out.println(decryptSm4);
|
|
|
+ }
|
|
|
+}
|