以下是一个示例代码,可以查询数据库中某个表中的日期数据,并按照天数统计出现次数:\n\njava\nimport java.sql.*;\nimport java.util.HashMap;\nimport java.util.Map;\n\npublic class Main {\n public static void main(String[] args) {\n Connection conn = null;\n Statement stmt = null;\n ResultSet rs = null;\n try {\n // 连接数据库\n conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false", "root", "password");\n\n // 执行查询语句\n stmt = conn.createStatement();\n String sql = "SELECT date_column FROM your_table";\n rs = stmt.executeQuery(sql);\n\n // 统计出现次数\n Map<String, Integer> countMap = new HashMap<>();\n while (rs.next()) {\n String date = rs.getString("date_column");\n if (date != null) {\n String day = date.substring(0, 10); // 提取日期中的天数部分\n countMap.put(day, countMap.getOrDefault(day, 0) + 1);\n }\n }\n\n // 输出统计结果\n for (Map.Entry<String, Integer> entry : countMap.entrySet()) {\n System.out.println(entry.getKey() + ": " + entry.getValue());\n }\n } catch (SQLException e) {\n e.printStackTrace();\n } finally {\n // 关闭连接\n try {\n if (rs != null) rs.close();\n if (stmt != null) stmt.close();\n if (conn != null) conn.close();\n } catch (SQLException e) {\n e.printStackTrace();\n }\n }\n }\n}\n\n\n注意替换代码中的数据库连接信息和查询语句,其中your_table为要查询的表名,date_column为日期列的列名。运行该代码后,会输出按天数统计的结果。


原文地址: https://www.cveoy.top/t/topic/pQVC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录