适配TinyInt

This commit is contained in:
liangdaliang
2025-03-07 15:31:18 +08:00
parent b80c0cf2ba
commit 9df6645ba8

View File

@@ -1,6 +1,7 @@
package com.test.common.utils;
import java.sql.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -40,7 +41,14 @@ public class MySQLExecutor {
// 获取字段名和字段值
String columnName = metaData.getColumnLabel(i);
Object columnValue = resultSet.getObject(i);
row.put(columnName, columnValue);
int columnType = metaData.getColumnType(i);
if (columnType == Types.TINYINT) {
row.put(columnName, resultSet.getInt(columnName));
} else if (columnType == Types.TIMESTAMP) {
row.put(columnName, ((Timestamp) columnValue).toLocalDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
} else {
row.put(columnName, columnValue);
}
if (i == 1) {
columnNameList.add(columnName);
}
@@ -53,4 +61,17 @@ public class MySQLExecutor {
}
return result;
}
// public static void main(String[] args) {
// String url = "jdbc:mysql://47.103.142.5:3306/cmcf-test";
// List<String> columnNameList = new ArrayList<>();
// List<Map<String, Object>> resultMapList = MySQLExecutor.executeQuery("select * from test_task t", url, "test", "Test123@", columnNameList);
// ObjectMapper objectMapper = new ObjectMapper();
// try {
// String result = objectMapper.writeValueAsString(resultMapList);
// System.out.println(result);
// } catch (JsonProcessingException e) {
// throw new RuntimeException(e);
// }
// }
}