zoukankan      html  css  js  c++  java
  • 访问sql server数据库

    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;
    	}
    }
    

      

  • 相关阅读:
    【UML九种图系列】之用例图
    数据结构之后缀数组
    Web Service实例——天气预报
    庞果网 合法字符串
    web 版发送邮件-已删除
    C语言实现双向链表删除节点、插入节点、双向输出等操作
    [置顶] String StringBuffer StringBuilder的区别剖析
    N!末尾有多少个零
    细说业务逻辑 -- 丢失的业务逻辑层
    你真的了解分层架构吗?——写给被PetShop"毒害"的朋友们
  • 原文地址:https://www.cnblogs.com/xianz666/p/14306125.html
Copyright © 2011-2022 走看看