Mysql ResultSet ResultSetMetaData 便捷查询

xiaoxiao2025-08-28  15

最近在做一些数据处理的事情,用的java的main函数来实现,数据来源于mysql数据库,也就是说我需要用mysql最简单粗暴的方式进行数据查询。但是遇到的问题是,每次查询的字段是不相同的,导致的结果就是每次都需要写大量不必要的代码,情况类似于这样:

ResultSet rs = statement.executeQuery(sql); List<Map<String, Object>> list = new ArrayList<>(); while (rs.next()) { Map<String, Object> map = new HashMap<>(); map.put("pri_id", rs.getString("pri_id")); map.put("id", rs.getString("id")); map.put("i1", rs.getInt("i1")); map.put("i2", rs.getInt("i2")); map.put("i3", rs.getInt("i3")); map.put("i4", rs.getInt("i4")); map.put("i5", rs.getInt("i5")); map.put("i6", rs.getInt("i6")); map.put("i7", rs.getInt("i7")); map.put("i8", rs.getInt("i8")); map.put("i9", rs.getInt("i9")); map.put("i10", rs.getInt("i10")); map.put("i11", rs.getInt("i11")); map.put("i12", rs.getInt("i12")); map.put("i13", rs.getInt("i13")); map.put("i14", rs.getInt("i14")); map.put("i15", rs.getInt("i15")); list.add(map); }

这样写不仅浪费时间,而且还容易写错。

于是,一个令人愉悦的查询方式产生了,代码如下:

package com.longlong.eventresult.util; import java.math.BigDecimal; import java.sql.*; import java.util.*; public class MySqlUtil { private Calendar cal = Calendar.getInstance(); private static Connection conn = null; public Connection getConnection() throws Exception { if (conn == null) { // 加载mysql驱动程序 Class.forName("com.mysql.jdbc.Driver"); // 连接localhost上的mysql,并指定使用test数据库,用户名为root,密码为*** conn = DriverManager.getConnection("jdbc:mysql://192.168.144.174:3306/test?allowMultiQueries=true", "root", "root"); } return conn; } public MySqlUtil() { try { getConnection(); } catch (Exception e) { } } public void insertIntoDb(String sql) throws Exception { getConnection(); Statement statement = conn.createStatement(); int ret = statement.executeUpdate(sql); System.out.println(ret); statement.close(); } public int queryTimes(String sql) throws Exception { getConnection(); Statement statement = conn.createStatement(); ResultSet rs = statement.executeQuery(sql); if (rs.next()) { return rs.getInt("cnt"); } statement.close(); return 0; } public BigDecimal queryAVG(String sql) throws Exception { getConnection(); Statement statement = conn.createStatement(); ResultSet rs = statement.executeQuery(sql); if (rs.next()) { return rs.getBigDecimal("avg"); } statement.close(); return BigDecimal.ZERO; } public List<Map<String, Object>> queryM3A(String sql) throws Exception { getConnection(); Statement statement = conn.createStatement(); ResultSet rs = statement.executeQuery(sql); List<Map<String, Object>> list = new ArrayList<>(); ResultSetMetaData rsmd = rs.getMetaData(); int colCount = rsmd.getColumnCount(); List<String> nameList = new ArrayList<>(); for (int i = 1; i <= colCount; i++) { nameList.add(rsmd.getColumnLabel(i)); } while (rs.next()) { Map<String, Object> map = new HashMap<>(); for (int i = 0; i < nameList.size(); i++) { map.put(nameList.get(i), rs.getObject(nameList.get(i))); } list.add(map); } statement.close(); return list; } }

重点是

public List<Map<String, Object>> queryM3A(String sql)

这个方法。再也不用写你要查询的字段了,要查询的字段完全交给sql去做。至于查询完后怎么处理,是这样的:

String sql_2 = "SELECT u2.i16 AS out_tmp,u2.i17 AS out_hum, SUBSTR(u2.created_at,1,16) as created_at_m FROM mg_u2 u2 WHERE u2.device_id = '" + gw_id + "' AND u2.create_int = date('" + d + "')"; List<Map<String, Object>> listm2 = mySqlUtil.queryM3A(sql_2); Map<String, Integer> tmpMap = new HashMap<>(); Map<String, Integer> humMap = new HashMap<>(); for (int k = 0; k < listm2.size(); k++) { tmpMap.put((String) listm2.get(k).get("created_at_m"), (int) listm2.get(k).get("out_tmp")); humMap.put((String) listm2.get(k).get("created_at_m"), (int) listm2.get(k).get("out_hum")); }

你要查询哪些字段(包括字段类型)你最清楚了,都在sql语句里面。

以上就是最简单粗暴的方式便捷查询的方式方法了。

转载请注明原文地址: https://www.6miu.com/read-5035368.html

最新回复(0)