我一直在将MYSQL表值转换为JSON。我的表有以下专栏 entry_time timestamp。以下内容对于数据转换非常有效,但是在将数据转换为JSON时,转换为JSON后我具有以下值
public String convertTableDatatoJSON(String tableNAME) throws SQLException {
String tableName = tableNAME;
Connection connection = createConnection();
JSONArray array = new JSONArray();
JSONObject jsonObject = new JSONObject();
List 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("src/main/resources/JSONdata/" + tableName + ".json");
file.write(jsonObject.toJSONString());
file.close();
} catch (IOException e) {
e.printStackTrace();
}
return "Data from table " + tableNAME + " is converted to JSON successfully. ";
}
private Connection createConnection() throws SQLException {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String mysqlUrl = "jdbc:mysql://localhost/parking?autoReconnect=true&useSSL=false";
Connection connection = DriverManager.getConnection(mysqlUrl, "root", "root");
return connection;
}
public List 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 columnsName = new ArrayList();
while (resultSet.next()) {
columnsName.add(resultSet.getString("COLUMN_NAME"));
}
return columnsName;
}
public ResultSet loadData(Connection connection, String tableName) throws SQLException {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from " + tableName + "");
return resultSet;
}
实际结果:“ entry_time”:0000-00-00 00:00:00
预期结果:“ entry_time”:“ 0000-00-00 00:00:00”
我只想在将表数据转换为时间戳数据类型为JSON 的列时放入逗号。
问题来源于stack overflow