微信小程序前端传数据给后端,后端servlet可以正常接收后调用db数据库操作类,但问题是不管传什么值,db类中boolean函数一直返回真值,即使数据库中并没有其值
以下是servlet类代码
package servlet;
import java.io.IOException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.management.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;
import db.db1;
import User.user;
public class WechatServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static db1 db1;
public WechatServlet() throws ClassNotFoundException, SQLException {
super();
this.db1 = new db1();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html;charset=utf-8");
//ajax跨域访问
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods","GET,POST");
//获取微信小程序前端数据
String passengerID = request.getParameter("passengerID");
String state = request.getParameter("state");
System.out.println(passengerID);
System.out.println(state);
//调用db1数据库操作
Writer out = response.getWriter();
String tip = new String();
if(db1.existInfo(passengerID) == true) {
tip = "查询成功";
out.write("success");
}
else if(db1.existInfo(passengerID) == false){
tip = "未查询到正在进行的单号";
out.write("error");
}
//查询所有用户(这块应该需要重新设计)
ArrayList<user> user = new ArrayList<>();
try {
user = db1.queryAllInfo();
}catch (SQLException e) {
e.printStackTrace();
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
request.setAttribute("user", user);
request.setAttribute("tip", tip);
System.out.println(tip);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
以下是操作数据库db类代码
package db;
import java.sql.*;
import java.util.ArrayList;
import User.user;
public class db1 {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=car;autoReconnectForPools=true;encrypt=true;trustServerCertificate=true;";
String userName = "sa";
String userPwd = "3018212009";
Connection connection = null;
Statement statement = null;
PreparedStatement preparestatement = null;
ResultSet resultset = null;
//预先定义sql语句
public String checkSQL = "select * from [dbo].[newtable] where state = 0";
public String existSQL = "select * from [dbo].[newtable] where passengerID = ? and state = 0 ";
//连接数据库
public db1() throws ClassNotFoundException, SQLException {
Class.forName(driverName);
connection = DriverManager.getConnection(dbURL,userName,userPwd);
statement = connection.createStatement();
}
//查询所有用户
public ArrayList<user> queryAllInfo() throws SQLException, ClassNotFoundException{
Class.forName(driverName);
connection = DriverManager.getConnection(dbURL,userName,userPwd);
statement = connection.createStatement();
ArrayList<user> resultlist = new ArrayList<>();
resultset = statement.executeQuery(checkSQL);
if (resultlist == null) return null;
while(resultset.next()) {
String passengerID = resultset.getString("passengerID");
String state = resultset.getString("state");
String carID = resultset.getString("carID");
String startTime = resultset.getString("startTime");
String orderID = resultset.getString("orderID");
String carType = resultset.getString("carType");
String passengerParentNum = resultset.getString("passengerParentNum");
String startAddress = resultset.getString("startAddress");
String endAddress = resultset.getString("endAddress");
String passengerName = resultset.getString("passengerName");
String passengerNum = resultset.getString("passengerNum");
String passengerParentName = resultset.getString("passengerParentName");
resultlist.add(new user(passengerID,state,carID,startTime,orderID,carType,passengerParentNum,startAddress,endAddress,passengerName,passengerNum,passengerParentName));
}
return resultlist;
}
//查询是否存在这个号以及状态,如果存在返回true,如果不存在返回false(state:0为正在进行,1已完成)
public boolean existInfo(String passengerID) {
resultset = null;
try {
preparestatement = connection.prepareStatement(existSQL);
preparestatement.setString(1, passengerID);
resultset = preparestatement.executeQuery();
}catch (SQLException e) {
e.printStackTrace();
}
if (resultset == null) return false;
return true;
}
}
还有一个user类
package User;
public class user{
String passengerID;
String state;
String carID;
String startTime;
String orderID;
String carType;
String passengerParentNum;
String startAddress;
String endAddress;
String passengerName;
String passengerNum;
String passengerParentName;
public user(String passengerID,String state,String carID,String startTime,String orderID,
String carType,String passengerParentNum,String startAddress,String endAddress,
String passengerName,String passengerNum,String passengerParentName)
{
this.passengerID = passengerID;
this.carID = carID;
this.carType = carType;
this.endAddress = endAddress;
this.orderID = orderID;
this.passengerName = passengerName;
this.passengerNum = passengerNum;
this.passengerParentName = passengerParentName;
this.passengerParentNum = passengerParentNum;
this.startAddress = startAddress;
this.startTime =startTime;
this.state = state;
}
public void user() {
// TODO Auto-generated constructor stub
}
public String getPassengerID() {
return this.passengerID;
}
public void setPassengerID(String passengerID) {
this.passengerID = passengerID;
}
public String getCarID() {
return this.carID;
}
public void setCarID(String carID) {
this.carID = carID;
}
public String getCarType() {
return this.carType;
}
public void setCarType(String carType) {
this.carType = carType;
}
public String getEndAddress() {
return this.endAddress;
}
public void setEndAddress(String endAddress) {
this.endAddress = endAddress;
}
public String getOrderID() {
return this.orderID;
}
public void setOrderID(String orderID) {
this.orderID = orderID;
}
public String getPassengerName() {
return this.passengerName;
}
public void setPassengerName(String passengerName) {
this.passengerName = passengerName;
}
public String getPassengerNum() {
return this.passengerNum;
}
public void setPassengerNum(String passengerNum) {
this.passengerNum = passengerNum;
}
public String getPassengerParentName() {
return this.passengerParentName;
}
public void setPassengerParentName(String passengerParentName) {
this.passengerParentName = passengerParentName;
}
public String getStartAddress() {
return this.startAddress;
}
public void setStartAddress(String startAddress) {
this.startAddress = startAddress;
}
public String getStartTime() {
return this.startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
}
还有我数据库的表内容
运行结果如下
实在不知道怎么解决了,他要是报错还好,这不报错更难受。
最终我想呈现效果就是如果输入正确的身份证号,查询到对应的数据并传到jsp文件(jsp文件我还没写),然后小程序前端调用jsp文件把数据拿过去,如果输入错误的身份证号就直接返回前端错误。
感谢各位能看我跟屎一样的代码,本人从来没学过java,毕设需要,没办法,只能跳着学。