在春季启动中将所有mysql表数据转换为JSON

时间:2019-11-08 19:02:14

标签: mysql json spring

我只想指定表名,它将自动将其所有数据转换为JSON格式

我尝试了使用JDBC将表中的数据存储到JSON中的代码。


public class DataBaseToJson {
    public static ResultSet RetrieveData() throws Exception {
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        String mysqlUrl = "jdbc:mysql://localhost/studentsDB?autoReconnect=true&useSSL=false";
        Connection con = DriverManager.getConnection(mysqlUrl, "root", "root");
        Statement statement = con.createStatement();
        ResultSet resultSet = statement.executeQuery("Select * from students");
        return resultSet;
    }

    public static void main(String args[]) throws Exception {
        JSONObject jsonObject = new JSONObject();
        JSONArray array = new JSONArray();
        ResultSet resultSet = RetrieveData();
        while (resultSet.next()) {
            JSONObject record = new JSONObject();
            record.put("students ID", resultSet.getInt("students_id"));
            record.put("students Name", resultSet.getString("students_name"));
            array.add(record);
        }
        jsonObject.put("students Information", array);
        try {
            FileWriter file = new FileWriter("output.json");
            file.write(jsonObject.toJSONString());
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我不想在文件中指定文件。我想使其通用,以便我们只能放入表名,它将自动转换该表中的所有数据并保存到JSON文件中。

2 个答案:

答案 0 :(得分:1)

您可以使用SELECT COLUMN_NAME FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE TABLE_NAME LIKE 'table_name' 选择给定表的列列表:

ResultSet

现在,将上述查询中的List<String>转换为列名的ResultSet。之后,我们可以使用它将最终的JSON Object转换为Connection connection = createConnection(); List<String> columns = loadColumns(connection, tableName); ResultSet dataSet = loadData(connection, tableName); while (dataSet.next()) { JSONObject record = new JSONObject(); for (String column : columns) { record.put(column, dataSet.getObject(column)); } array.add(record); } // save array to file

伪代码:

ResultSet

Streaming API很大时,我们应该考虑使用JacksonGson库中的SQL,以避免“内存不足”问题。

另请参阅:

更新

似乎我们不需要使用额外的ResultSet查询来选择列名称,因为export interface ITodo { id:number; subject:string; body:string; status: number; result:number; } interface ITodoObject { [key: number]: ITodo } 具有getMetaData方法:

  

检索此ResultSet对象的编号,类型和属性   列。

另请参阅:

答案 1 :(得分:0)


    public static void main(String args[]) throws Exception {
        String tableName = "books";
        Connection connection = createConnection();
        JSONArray array = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        List<String> columns = loadColumns(connection, tableName);
        ResultSet dataSet = loadData(connection, tableName);
        while (dataSet.next()) {
            JSONObject record = new JSONObject();
            for (String column : columns) {
                record.put(column, dataSet.getObject(column));
            }
            array.add(record);
        }
        jsonObject.put(tableName, array);
        try {
            FileWriter file = new FileWriter("output.json");
            file.write(jsonObject.toJSONString());
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static Connection createConnection() throws SQLException {
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        String mysqlUrl = "jdbc:mysql://localhost/library?autoReconnect=true&useSSL=false";
        Connection connection = DriverManager.getConnection(mysqlUrl, "root", "root");
        return connection;
    }


    public static List<String> loadColumns(Connection connection, String tableName) throws SQLException {
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("SELECT COLUMN_NAME FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE TABLE_NAME LIKE '"+tableName+"'");
        List<String> columnsName = new ArrayList<String>();
        while(resultSet.next()) {
            columnsName.add(resultSet.getString("COLUMN_NAME"));
        }
        return columnsName;
    }

    public static ResultSet loadData(Connection connection, String tableName) throws SQLException {
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("Select * from "+tableName+"");
        return resultSet;
    }