package org.springblade.consumption.utils; import org.springblade.consumption.constant.ConsumptionConstant; import org.springblade.core.tool.utils.Func; import java.sql.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ConsumptionUtil { /** * 消费机模块 连接数据库获取信息 * @return */ public static List<Map<String,Object>> getSqlResult(String sql,Map<String,String>map){ List<Map<String,Object>> list = new ArrayList(); try { //一开始必须填一个已经存在的数据库 String url = ConsumptionConstant.url; Connection conn = DriverManager.getConnection(url, ConsumptionConstant.userName, ConsumptionConstant.password);// 连接数据库 //Statement stat = conn.createStatement();//创建一个 Statement 对象来将 SQL 语句发送到数据库。 PreparedStatement stat = conn.prepareStatement(sql); if(Func.isNotEmpty(map)){ //餐别 if(map.containsKey("mealType")){ stat.setString(1, map.get("mealType")); } //消费时间 if(map.containsKey("date")){ stat.setDate(2,Date.valueOf(map.get("date"))); } } ResultSet resultSet=stat.executeQuery(); ResultSetMetaData md = resultSet.getMetaData(); int columnCount = md.getColumnCount(); while (resultSet.next()) { Map<String,Object> rowData = new HashMap(); for (int i = 1; i <= columnCount; i++) { rowData.put(md.getColumnName(i), resultSet.getObject(i)); } list.add(rowData); } System.out.println("list====="+list); resultSet.close(); } catch (Exception e) { e.printStackTrace(); } return list; } } package org.springblade.consumption.utils; import org.springblade.consumption.constant.ConsumptionConstant; import org.springblade.core.tool.utils.Func; import java.sql.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ConsumptionUtil { /** * 消费机模块 连接数据库获取信息 * @return */ public static List<Map<String,Object>> getSqlResult(String sql,Map<String,String>map){ List<Map<String,Object>> list = new ArrayList(); try { //一开始必须填一个已经存在的数据库 String url = ConsumptionConstant.url; Connection conn = DriverManager.getConnection(url, ConsumptionConstant.userName, ConsumptionConstant.password);// 连接数据库 //Statement stat = conn.createStatement();//创建一个 Statement 对象来将 SQL 语句发送到数据库。 PreparedStatement stat = conn.prepareStatement(sql); if(Func.isNotEmpty(map)){ //餐别 if(map.containsKey("mealType")){ stat.setString(1, map.get("mealType")); } //消费时间 if(map.containsKey("date")){ stat.setDate(2,Date.valueOf(map.get("date")));//转换成java sql类型 } } ResultSet resultSet=stat.executeQuery(); ResultSetMetaData md = resultSet.getMetaData(); int columnCount = md.getColumnCount(); while (resultSet.next()) { Map<String,Object> rowData = new HashMap(); for (int i = 1; i <= columnCount; i++) { rowData.put(md.getColumnName(i), resultSet.getObject(i)); } list.add(rowData); } System.out.println("list====="+list); resultSet.close(); } catch (Exception e) { e.printStackTrace(); } return list; } }