springboot项目 java写一个工具类,查找出mysql数据库中其中一个库的所有表和字段和字段长度并生成一个xml文件
时间: 2023-05-21 07:04:18 浏览: 215
java 从mysql数据库导出数据生成xml文件
5星 · 资源好评率100%
可以使用JDBC API连接MySQL数据库,使用ResultSet获取库中的表和字段信息,再利用DOM API动态生成一个XML格式的文件。以下是一个简单的示例:
```
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class MysqlSchemaToXmlUtil {
public static void generateXml(String jdbcUrl, String username, String password, String databaseName, String xmlFilePath)
throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(jdbcUrl, username, password);
Statement stmt = conn.createStatement();
// Get all table names in the database
String sql = "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = '" + databaseName + "'";
ResultSet rs = stmt.executeQuery(sql);
// Create the document builder and factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
// Create the root element for the XML file
Element root = doc.createElement("database");
root.setAttribute("name", databaseName);
doc.appendChild(root);
// Iterate through each table and its columns
while (rs.next()) {
String tableName = rs.getString("TABLE_NAME");
Element table = doc.createElement("table");
table.setAttribute("name", tableName);
root.appendChild(table);
sql = "SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='"
+ databaseName + "' AND TABLE_NAME='" + tableName + "'";
ResultSet columns = stmt.executeQuery(sql);
while (columns.next()) {
Element column = doc.createElement("column");
column.setAttribute("name", columns.getString("COLUMN_NAME"));
column.setAttribute("type", columns.getString("DATA_TYPE"));
column.setAttribute("length", columns.getString("CHARACTER_MAXIMUM_LENGTH"));
table.appendChild(column);
}
columns.close();
}
rs.close();
stmt.close();
conn.close();
// Write the document to a file
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer trans = transFactory.newTransformer();
trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource domSource = new DOMSource(doc);
File xmlFile = new File(xmlFilePath);
FileOutputStream fos = new FileOutputStream(xmlFile);
StreamResult streamResult = new StreamResult(fos);
trans.transform(domSource, streamResult);
fos.close();
}
}
```
调用示例:
```
String jdbcUrl = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "root";
String databaseName = "test";
String xmlFilePath = "schema.xml";
MysqlSchemaToXmlUtil.generateXml(jdbcUrl, username, password, databaseName, xmlFilePath);
```
该示例将在本地的test数据库中查找所有表和字段,生成一个名为"schema.xml"的XML文件。
阅读全文