场景
在只知道表名,不知道表包含哪些字段情况下,查询该表信息的场景
解决方案
@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);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/106235.html