场景
在只知道表名,不知道表包含哪些字段情况下,查询该表信息的场景
解决方案
@Test
public void test() {
Connection connection;
String DB_URL = "jdbc:mysql://192.168.20.75:9950/geespace_bd_platform_dev?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&";
List<Map<String, Object>> data = new ArrayList<>();
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(DB_URL, "geespace", "gee123456");
stmt = connection.createStatement();
// 获取sql
// rs = stmt.executeQuery("SELECT nth_interval_of_temperature1,number_of_temperature1 FROM 114_interval_statistical_table GROUP BY nth_interval_of_temperature1,number_of_temperature1 order BY nth_interval_of_temperature1 asc");
rs = stmt.executeQuery("SELECT * from ge_drag_spark_task");
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next()) {
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
Map<String, Object> map = new HashMap<>(1);
map.put(rsmd.getColumnName(i), rs.getObject(i));
data.add(map);
}
}
for (Map<String, Object> map: data) {
log.info(" map:{}", map);
}
} catch (SQLException e) {
log.error("[getColumnData Exception] --> the exception message is:{}", e.getMessage());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally {
JdbcUtils.close(rs);
JdbcUtils.close(stmt);
}
}
重要信息