SSM项目中连接其他系统中的数据库

我是研究僧i 提交于 2020-03-09 11:01:26

在项目开发中,本地系统需要连接其他系统中的数据库获取数据,此时采用jdbc连接数据库,代码为:

 1 private Connection connection;//jdbc连接数据库
 2 private void init() {
 3         if(!hasInit) {
 4             String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
 5             String dbURL = "jdbc:sqlserver://172.18.199.214:1433; DatabaseName=VEG_SYS";
 6             String username = "DBW";
 7             String pwd = "123456";
 8             try {
 9                 Class.forName(driverName);
10                 connection = DriverManager.getConnection(dbURL, username, pwd);
11             }catch (Exception e) {
12                 e.printStackTrace();
13             }
14             hasInit = true;
15         }
16 }
17 //拼接sql,返回结果集,将结果集转化成List<Map<String,Object>>格式返回
18 public List<Map<String, Object>> getCarInfoByCarNo(String where) throws Exception{
19         init();
20         List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
21         Statement statement =  connection.createStatement();
22         String sql1 = "select\r\n" + 
23                 "        top 10\r\n"+
24                 "        t.I_IntId  as Id\r\n"+
25                 "      ,t.C_CarsNo as CarsNo\r\n" + 
26                 "      ,t.C_InCarsNo as OutInCarsNo\r\n" + 
27                 "      ,t.C_PastName as PastName\r\n" + 
28                 "      ,t.C_RulesName as RulesName\r\n" + 
29                 "      ,t.I_InTime as OutInTime\r\n" + 
30                 "      ,t.C_CarsOwnerPhoneNo as Phone\r\n" + 
31                 "      ,t.C_InDepart as OutInDept\r\n" + 
32                 "      ,t.C_CustomNo as ICCard\r\n" + 
33                 "      ,t.C_AreaName as AreaName\r\n" + 
34                 "      ,t.C_CarsOwner as CarsOwner\r\n" + 
35                 "      ,t.C_PlateColor as PlateColor\r\n" + 
36                 "      ,t.I_InRulesId as OutInRulesId\r\n" + 
37                 "      from\r\n" + 
38                 "      (select\r\n" + 
39                 "        a.I_IntId\r\n" +
40                 "      ,a.C_CarsNo\r\n" + 
41                 "      ,a.C_InCarsNo\r\n" + 
42                 "      ,b.C_PastName\r\n" + 
43                 "      ,a.I_InTime\r\n" + 
44                 "      ,h.C_CarsOwnerPhoneNo\r\n" + 
45                 "      ,c.C_RulesName\r\n" + 
46                 "      ,a.C_InDepart\r\n" + 
47                 "      ,h.C_CustomNo\r\n" + 
48                 "      ,f.C_AreaName\r\n" + 
49                 "      ,h.C_CarsOwner\r\n" + 
50                 "      ,g.C_PlateColor\r\n" + 
51                 "      ,a.I_InRulesId\r\n" + 
52                 "      ,SUBSTRING(CONVERT(varchar(100),  a.I_InTime, 20),1,16) as ctine\r\n" + 
53                 "      from [CR].Cars_InRecord a\r\n" + 
54                 "      join [CA].Cars_Permissions_Rules_Main c on c.I_IntId = a.I_InRulesId\r\n" + 
55                 "      join [GI].Cars_Guard_Past b on b.I_IntId = a.I_InPastId\r\n" + 
56                 "      join  GI.Cars_Guard_Area f  on f.I_IntId =a.I_AreaId\r\n" + 
57                 "      join CI.Cars_Info h on h.I_IntId=a.I_CarsId\r\n" + 
58                 "      left join CR.Cars_PlateInfo g  on a.C_InCarsNo=g.C_CarsNo\r\n" + 
59                 "      left join CI.InStatePerson j on a.C_IdentityNo=j.C_IdentityNo "+where+") t ORDER BY OutInTime DESC";
60 
61         ResultSet rs = statement.executeQuery(sql1);
62         System.out.println(sql1);
63         while(rs.next()){
64 //            System.out.println(rs.getString("C_EngineeringNo"));
65             Map<String, Object> car = new HashMap<String, Object>();
66             car.put("Id", rs.getString("Id"));
67             car.put("CarsNo", rs.getString("CarsNo"));
68             car.put("OutInCarsNo", rs.getString("OutInCarsNo"));
69             car.put("OutInDept", rs.getString("OutInDept"));
70             car.put("PastName", rs.getString("PastName"));
71             car.put("RulesName", rs.getString("RulesName"));
72             car.put("OutInTime", rs.getString("OutInTime"));
73             car.put("Phone", rs.getString("Phone"));
74             car.put("ICCard", rs.getString("ICCard"));
75             car.put("AreaName", rs.getString("AreaName"));
76             car.put("CarsOwner", rs.getString("CarsOwner"));
77             car.put("PlateColor", rs.getString("PlateColor"));
78             car.put("OutInRulesId", rs.getString("OutInRulesId"));
79             result.add(car);
80         }
81         statement =  connection.createStatement();
82         return result;
83 }

插入sql:

1 public void createBlackInfo(String sql) throws Exception{
2         init();
3         Statement statement =  connection.createStatement();
4         String sql1 = where;
5         statement.executeUpdate(sql1);
6         System.out.println(sql1);
7         statement =  connection.createStatement();
8 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!