java生成mysql数据库建表语句、字段、字段类型、字段注释,可实现不用mysqldump备份数据库

网友投稿 840 2022-05-29

使用 mysqldump 备份数据库也是可行的,因为每次备份的时候都需要mysqldump这个文件, 我在windows备份时没问题,但是放到linux上面时,centos系统死活不认这个文件,但又不想装mysql,一气之下自己研究了个不需要mysqldump就可以备份的程序,

如果看了以下代码还有不懂的地方,这个网站有我的联系方式http://www.huashuku.top/about.htm, 站长就是我本人

废话不多说,直接上代码

添加jdbc驱动 和httpClient的 maven依赖

org.apache.httpcomponents

httpclient

4.1.2

org.apache.httpcomponents

httpclient-cache

4.1.2

org.apache.httpcomponents

httpmime

4.1.2

mysql

mysql-connector-Java

5.1.15

runtime

备份程序

package com.mysql.bak;

import java.math.BigDecimal;

import java.sql.Connection;

import java.sql.DatabaseMetaData;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.List;

import com.utils.FileUtils;

/**

* 利用jdbc备份MySql数据库--不用mysqldump

*

*/

public class BakDateBase {

private String DRIVER = "com.mysql.jdbc.Driver";

private String URL = null; // "jdbc:mysql://182.xxx.xxx.xxx:3306/xd_love_dev?useUnicode=true&characterEncoding=utf8";

private String USERNAME = null;// "root";

private String PASSWORD = null;//"woaini";

// 备份的文件地址

private String filePath;

private Connection conn = null;

private String SQL = "SELECT * FROM ";// 数据库操作

/**

*

* <构造函数>

*

* @param ip

* 数据库ip地址

* @param database

* 数据库名称

* @param userName

* 数据库用户名

* @param password

* 密码

* @param bakFilePath

* 备份的地址

*/

public BakDateBase(String ip, String database, String userName, String password, String bakFilePath) {

try {

Class.forName(this.DRIVER);

this.URL = String.format("jdbc:mysql://%s:3306/%s?useUnicode=true&characterEncoding=utf8", ip, database);

this.USERNAME = userName;

this.PASSWORD = password;

SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-ddHH时mm分ss秒");

String datetime = tempDate.format(new java.util.Date());

//自动加上时间戳

datetime = datetime + "_数据库名称:" + database ;

if(bakFilePath.indexOf(".") != -1) {

bakFilePath = bakFilePath.replace(".", datetime+".");

} else {

bakFilePath = datetime + ".sql";

}

this.filePath = bakFilePath;

} catch (ClassNotFoundException e) {

e.printStackTrace();

System.err.println("can not load jdbc driver");

}

}

/**

* 获取数据库连接

*

* @return

*/

private Connection getConnection() {

try {

if (null == conn) {

conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);

}

} catch (SQLException e) {

e.printStackTrace();

System.err.println("get connection failure");

}

return conn;

}

/**

* 关闭数据库连接

*

* @param conn

*/

private void closeConnection(Connection conn) {

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

System.err.println("close connection failure");

}

}

}

/**

* 获取数据库下的所有表名

*/

private List getTableNames() {

List tableNames = new ArrayList();

Connection conn = getConnection();

ResultSet rs = null;

try {

// 获取数据库的元数据

DatabaseMetaData db = conn.getMetaData();

// 从元数据中获取到所有的表名

rs = db.getTables(null, null, null, new String[] { "TABLE" });

while (rs.next()) {

tableNames.add(rs.getString(3));

}

} catch (SQLException e) {

e.printStackTrace();

System.err.println("getTableNames failure");

} finally {

try {

if (null != rs) {

rs.close();

}

} catch (SQLException e) {

e.printStackTrace();

System.err.println("close ResultSet failure");

}

}

return tableNames;

}

/**

* 获取表中所有字段名称

*

* @param tableName

* 表名

* @return

*/

private List getColumnNames(String tableName) {

List columnNames = new ArrayList();

// 与数据库的连接

Connection conn = getConnection();

PreparedStatement pStemt = null;

String tableSql = SQL + tableName;

try {

pStemt = conn.prepareStatement(tableSql);

// 结果集元数据

ResultSetMetaData rsmd = pStemt.getMetaData();

// 表列数

int size = rsmd.getColumnCount();

for (int i = 0; i < size; i++) {

columnNames.add(rsmd.getColumnName(i + 1));

}

} catch (SQLException e) {

System.err.println("getColumnNames failure");

e.printStackTrace();

} finally {

if (pStemt != null) {

try {

pStemt.close();

} catch (SQLException e) {

e.printStackTrace();

System.err.println("getColumnNames close pstem and connection failure");

}

}

}

return columnNames;

}

/**

* 获取表中所有字段类型

*

* @param tableName

* @return

*/

private List getColumnTypes(String tableName) {

List columnTypes = new ArrayList();

// 与数据库的连接

Connection conn = getConnection();

PreparedStatement pStemt = null;

String tableSql = SQL + tableName;

try {

pStemt = conn.prepareStatement(tableSql);

// 结果集元数据

ResultSetMetaData rsmd = pStemt.getMetaData();

// 表列数

int size = rsmd.getColumnCount();

for (int i = 0; i < size; i++) {

columnTypes.add(rsmd.getColumnTypeName(i + 1));

}

} catch (SQLException e) {

e.printStackTrace();

System.err.println("getColumnTypes failure");

} finally {

if (pStemt != null) {

try {

pStemt.close();

} catch (SQLException e) {

e.printStackTrace();

System.err.println("getColumnTypes close pstem and connection failure");

}

}

}

return columnTypes;

}

/**

*

*

* 生成建表语句

*

*

* @param tableName

* @return

* @author 叶新东(18126064335) 2018年9月6日 上午9:35:49

*/

private String generateCreateTableSql(String tableName) {

String sql = String.format("SHOW CREATE TABLE %s", tableName);

Connection conn = null;

PreparedStatement pstmt = null;

try {

conn = getConnection();

pstmt = (PreparedStatement) conn.prepareStatement(sql);

ResultSet rs = pstmt.executeQuery();

while (rs.next()) {

// 返回建表语句语句,查询结果的第二列是建表语句,第一列是表名

return rs.getString(2);

}

} catch (Exception e) {

e.printStackTrace();

try {

if (null != pstmt) {

pstmt.close();

}

} catch (Exception e2) {

e.printStackTrace();

System.err.println("关闭流异常");

}

}

return null;

}

/**

* 获取表中字段的所有注释

*

* @param tableName

* @return

*/

private List getColumnComments(String tableName) {

// 与数据库的连接

Connection conn = getConnection();

PreparedStatement pStemt = null;

String tableSql = SQL + tableName;

List columnComments = new ArrayList();// 列名注释集合

ResultSet rs = null;

try {

pStemt = conn.prepareStatement(tableSql);

rs = pStemt.executeQuery("show full columns from " + tableName);

while (rs.next()) {

columnComments.add(rs.getString("Comment"));

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

if (rs != null) {

try {

rs.close();

} catch (SQLException e) {

e.printStackTrace();

System.err.println("getColumnComments close ResultSet and connection failure");

}

}

}

return columnComments;

}

/**

*

*

* 备份表数据

*

*

* @param tableName

* @return

* @author () 2018年9月6日 上午10:18:07

*/

private String bakTableData(String tableName) {

Connection conn = null;

PreparedStatement pstmt = null;

try {

// 备份建表语句

String createTableSql = generateCreateTableSql(tableName);

createTableSql = String.format(

"\n\n\n/**\n * table name :<%s>\n *\n */\n%s\n",

tableName, createTableSql);

FileUtils.writeFileContent(filePath, createTableSql);

// 获取字段类型

List columnTypes = getColumnTypes(tableName);

// 获取所有 字段

List columnNames = getColumnNames(tableName);

String columnArrayStr = null;

for (String column : columnNames) {

if (null == columnArrayStr) {

columnArrayStr = "`" + column + "`";

} else {

columnArrayStr = columnArrayStr + "," + "`" + column + "`";

}

}

String sql = String.format("select %s from %s", columnArrayStr, tableName);

conn = getConnection();

pstmt = (PreparedStatement) conn.prepareStatement(sql);

ResultSet rs = pstmt.executeQuery();

while (rs.next()) {

String rowValues = getRowValues(rs, columnNames.size(), columnTypes);

// 返回建表语句语句,查询结果的第二列是建表语句,第一列是表名

String insertSql = String.format("insert into %s (%s) values(%s);", tableName, columnArrayStr,

rowValues);

System.out.println(insertSql);

insertSql = insertSql.replaceAll("\n", "
");

insertSql = insertSql + "\n";

FileUtils.writeFileContent(filePath, insertSql);

}

} catch (Exception e) {

e.printStackTrace();

try {

if (null != pstmt) {

pstmt.close();

}

} catch (Exception e2) {

e.printStackTrace();

System.err.println("关闭流异常");

}

}

return null;

}

/**

*

*

* 获取表数据一行的所有值

*

*

* @param rs

* @param size

* @author 2018年9月6日 上午11:03:05

*/

private String getRowValues(ResultSet rs, int size, List columnTypeList) {

try {

String rowValues = null;

for (int i = 1; i <= size; i++) {

String columnValue = null;

// 获取字段值

columnValue = getValue(rs, i, columnTypeList.get(i - 1));

// 如果是空值不添加单引号

if (null != columnValue) {

columnValue = "'" + columnValue + "'";

}

// 拼接字段值

if (null == rowValues) {

rowValues = columnValue;

} else {

rowValues = rowValues + "," + columnValue;

}

}

return rowValues;

} catch (Exception e) {

e.printStackTrace();

System.out.println("获取表数据一行的所有值异常");

return null;

}

}

/**

*

*

* 根据类型获取字段值

*

*

* @param obj

* @return

* @author 2018年9月6日 上午11:16:00

*/

private String getValue(ResultSet resultSet, Integer index, String columnType) {

try {

if ("int".equals(columnType) || "INT".equals(columnType)) {

// 整数

Object intValue = resultSet.getObject(index);

if (null == intValue) {

return null;

}

return intValue + "";

} else if ("bigint".equals(columnType) || "BIGINT".equals(columnType)) {

// 长整形

Object value = resultSet.getObject(index);

if (null == value) {

return null;

}

return value + "";

} else if ("smallint".equals(columnType) || "SMALLINT".equals(columnType)) {

// 整数

Object value = resultSet.getObject(index);

if (null == value) {

return null;

}

return value + "";

} else if ("tinyint".equals(columnType) || "TINYINT".equals(columnType)) {

// 整数

Object value = resultSet.getObject(index);

if (null == value) {

return null;

}

return value + "";

} else if ("mediumint".equals(columnType) || "MEDIUMINT".equals(columnType)) {

// 长整形

Object value = resultSet.getObject(index);

if (null == value) {

return null;

}

return value + "";

} else if ("integer".equals(columnType) || "INTEGER".equals(columnType)) {

// 整数

Object value = resultSet.getObject(index);

if (null == value) {

return null;

}

return value + "";

} else if ("float".equals(columnType) || "FLOAT".equals(columnType)) {

// 浮点数

Object value = resultSet.getObject(index);

if (null == value) {

return null;

}

return value + "";

} else if ("double".equals(columnType) || "DOUBLE".equals(columnType)) {

// 浮点数

Object value = resultSet.getObject(index);

if (null == value) {

return null;

}

return value + "";

} else if ("decimal".equals(columnType) || "DECIMAL".equals(columnType)) {

// 浮点数-金额类型

BigDecimal value = resultSet.getBigDecimal(index);

if (null == value) {

return null;

java生成mysql数据库建表语句、字段、字段类型、字段注释,可实现不用mysqldump备份数据库

}

return value.toString();

} else if ("char".equals(columnType) || "CHAR".equals(columnType)) {

// 字符串类型

String value = resultSet.getString(index);

return value;

} else if ("varchar".equals(columnType) || "VARCHAR".equals(columnType)) {

// 字符串类型

String value = resultSet.getString(index);

return value;

} else if ("tinytext".equals(columnType) || "TINYTEXT".equals(columnType)) {

// 字符串类型

String value = resultSet.getString(index);

return value;

} else if ("text".equals(columnType) || "TEXT".equals(columnType)) {

// 字符串类型

String value = resultSet.getString(index);

return value;

} else if ("mediumtext".equals(columnType) || "MEDIUMTEXT".equals(columnType)) {

// 字符串类型

String value = resultSet.getString(index);

return value;

} else if ("longtext".equals(columnType) || "LONGTEXT".equals(columnType)) {

// 字符串类型

String value = resultSet.getString(index);

return value;

} else if ("year".equals(columnType) || "YEAR".equals(columnType)) {

// 时间类型:范围 1901/2155 格式 YYYY

String year = resultSet.getString(index);

if (null == year) {

return null;

}

// 只需要年的字符即可,

return year.substring(0, 4);

} else if ("date".equals(columnType) || "DATE".equals(columnType)) {

// 时间类型:范围 '1000-01-01'--'9999-12-31' 格式 YYYY-MM-DD

return resultSet.getString(index);

} else if ("time".equals(columnType) || "TIME".equals(columnType)) {

// 时间类型:范围 '-838:59:59'到'838:59:59' 格式 HH:MM:SS

return resultSet.getString(index);

} else if ("datetime".equals(columnType) || "DATETIME".equals(columnType)) {

// 时间类型:范围 '1000-01-01 00:00:00'--'9999-12-31 23:59:59' 格式 YYYY-MM-DD HH:MM:SS

return resultSet.getString(index);

} else if ("timestamp".equals(columnType) || "TIMESTAMP".equals(columnType)) {

// 时间类型:范围 1970-01-01 00:00:00/2037 年某时 格式 YYYYMMDD HHMMSS 混合日期和时间值,时间戳

return resultSet.getString(index);

} else {

return null;

}

} catch (Exception e) {

e.printStackTrace();

System.err.println("获取数据库类型值异常");

return null;

}

}

/**

*

* <开始备份>

*

* @author 2018年9月6日 下午3:30:43

*/

public void startBak() {

try {

List tableNames = getTableNames();

System.out.println("tableNames:" + tableNames);

for (String tableName : tableNames) {

bakTableData(tableName);

// System.out.println(generateCreateTableSql(tableName));

// System.out.println("ColumnNames:" + getColumnNames(tableName));

// System.out.println("ColumnTypes:" + getColumnTypes(tableName));

// System.out.println("ColumnComments:" + getColumnComments(tableName));

}

// 统一关闭连接

closeConnection(conn);

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

new BakDateBase("182.xxx.xxx.xxx", "xd_love_dev", "root", "woaini", "f:\\bak.sql").startBak();

}

}

执行 main 方法后会在磁盘指定位置生成 .sql  的文件,内容如下:

Java MySQL 数据库

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:一场由fork引发的超时,让我们重新探讨了Redis的抖动问题
下一篇:Go 泛型 【翻译】
相关文章