Commit cce0385e authored by niuzhaodi's avatar niuzhaodi

改用SQLServer方式读取卡号

parent 27db6e81
......@@ -73,6 +73,11 @@
<artifactId>nochump</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.4.0.jre8</version>
</dependency>
</dependencies>
<build>
<plugins>
......
package com.wafersystems.accessTransit.controller;
import com.wafersystems.accessTransit.model.UserCardInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Api(tags = "sqlServer数据库")
@RequestMapping("/sqlServer")
@RestController
public class SqlServerCtl {
@Value("${sqlS.sDriver}")
private String sDriver;
@Value("${sqlS.sName}")
private String sName;
@Value("${sqlS.sPwd}")
private String sPwd;
@Value("${sqlS.sUrl}")
private String sUrl;
@ApiOperation(value = "查询门禁用户表")
@GetMapping("queryConsumerInSqlServer")
public Object queryConsumerInSqlServer() {
log.info("--外部应用调用查询sqlServer数据库接口[开始]--");
log.info("--sqlServer数据库sDriver==>{}", sDriver);
log.info("--sqlServer数据库sName==>{}", sName);
log.info("--sqlServer数据库sPwd==>{}", sPwd);
log.info("--sqlServer数据库sUrl==>{}", sUrl);
Map<String, Object> back = new HashMap<String, Object>();
back.put("status", 1);//1代表请求失败
if (sDriver == null || sName == null || sPwd == null || sUrl == null) {
log.warn("数据库参数配置存在空值");
return back;
}
try {
List<UserCardInfo> userCardInfoList = queryInfoInSqlServer();
int total = userCardInfoList != null && userCardInfoList.size() > 0 ? userCardInfoList.size() : 0;
log.info("--查询数据库记录总数==>{}条", total);
back.put("data", userCardInfoList);
back.put("status", 0);//0代表请求成功
} catch (Exception ex) {
log.error("--外部应用调用查询sqlServer数据库接口[异常]==>", ex);
} finally {
log.info("--外部应用调用查询sqlServer数据库接口[结束]--");
return back;
}
}
private List<UserCardInfo> queryInfoInSqlServer() throws Exception {
List<UserCardInfo> userCardInfoList = null;
String url = sUrl;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(sDriver);
log.info("[JDBC]建立数据库连接==>[开始]");
conn = DriverManager.getConnection(url, sName, sPwd);
log.info("[JDBC]建立数据库连接==>[结束]{}", conn);
log.info("[JDBC]建立数据库连接Statement==>[开始]");
stmt = conn.createStatement();
log.info("[JDBC]建立数据库连接Statement==>[结束]{}", stmt);
String sqlQ = "SELECT f_ConsumerName, f_ConsumerNO, f_CardNO FROM t_b_Consumer";
log.info("[JDBC]建立数据库连接SQL查询语句==>{}", sqlQ);
rs = stmt.executeQuery(sqlQ);
userCardInfoList = new ArrayList<UserCardInfo>();
while (rs.next()) {
String userName = rs.getString("f_ConsumerName");
userName = userName != null ? userName.trim() : "";
String consumerNO = rs.getString("f_ConsumerNO");
consumerNO = consumerNO != null ? consumerNO.trim() : "";
String cardNO = rs.getString("f_CardNO");
cardNO = cardNO != null ? cardNO.trim() : "";
log.info("userName =" + userName + " f_ConsumerNO =" + consumerNO + " f_CardNO =" + cardNO);
UserCardInfo userCardInfo = new UserCardInfo();
userCardInfo.setUserName(userName);
userCardInfo.setUserId(consumerNO);
userCardInfo.setCardNo(cardNO);
userCardInfoList.add(userCardInfo);
}
log.info("[JDBC]关闭ResultSet==>[开始]");
rs.close();
log.info("[JDBC]关闭ResultSet==>[结束]");
log.info("[JDBC]关闭Statement==>[开始]");
stmt.close();
log.info("[JDBC]关闭Statement==>[结束]");
log.info("[JDBC]关闭Connection==>[开始]");
conn.close();
log.info("[JDBC]关闭Connection==>[结束]");
} catch (Exception e) {
log.error("[查询Access数据库]异常==>", e);
if (rs != null) {
log.info("[异常][JDBC]关闭ResultSet==>[开始]");
rs.close();
log.info("[异常][JDBC]关闭ResultSet==>[结束]");
}
if (stmt != null) {
log.info("[异常][JDBC]关闭Statement==>[开始]");
stmt.close();
log.info("[异常][JDBC]关闭Statement==>[结束]");
}
if (conn != null) {
log.info("[异常][JDBC]关闭Connection==>[开始]");
conn.close();
log.info("[异常][JDBC]关闭Connection==>[结束]");
}
}
return userCardInfoList;
}
}
......@@ -27,3 +27,14 @@ access:
pwd: 168168
# access数据库存储文件路径
dir: D:/iCCard3000.mdb
# sqlServer数据库配置
sqlS:
# sqlServer数据库驱动
sDriver: com.microsoft.sqlserver.jdbc.SQLServerDriver
# sqlServer数据库用户
sName: sa
# sqlServer数据库密码
sPwd: Wafer!123
# sqlServer数据库连接
sUrl: jdbc:sqlserver://192.168.203.145:1433;DatabaseName=smt-meeting-5.1.x
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment