m0_53192979 2022-04-05 10:50 采纳率: 100%
浏览 29
已结题

servlet调用java类进行数据库查询是否存在的操作,但是不管给什么值都返回true值

微信小程序前端传数据给后端,后端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;
    }
}


还有我数据库的表内容

img

运行结果如下

img

实在不知道怎么解决了,他要是报错还好,这不报错更难受。
最终我想呈现效果就是如果输入正确的身份证号,查询到对应的数据并传到jsp文件(jsp文件我还没写),然后小程序前端调用jsp文件把数据拿过去,如果输入错误的身份证号就直接返回前端错误。

感谢各位能看我跟屎一样的代码,本人从来没学过java,毕设需要,没办法,只能跳着学。

  • 写回答

3条回答 默认 最新

  • 未聞花名丶 2022-04-05 10:59
    关注

    resultSet.next() 用这个去判断

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月13日
  • 已采纳回答 4月5日
  • 创建了问题 4月5日

悬赏问题

  • ¥15 win32如何自绘编辑框的背景图片(语言-c++|操作系统-windows)
  • ¥15 微信夜间被转走了1w对,当天手机剪切板里就出现了这个乱码,有铁子可以看看是啥吗可以
  • ¥50 跑通github上的代码 深度学习 pytorch
  • ¥50 求写,批处理调用分区助手分区脚本
  • ¥15 求购HI3519AV100开发板
  • ¥15 请问1553 RT怎么测试,没有BC有方法吗
  • ¥100 业务编程如何选择学习方向和内容?
  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度