访问sql server数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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;
    }
}

  

posted @   红尘沙漏  阅读(108)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示