String name = new String(request.getParameter("ZY").getBytes("ISO-8859-1"), "UTF-8"); System.out.print(name); if (name == null || name.isEmpty()) { %> <a href="SouSuo.jsp">请输入中药名称,点击返回重新输入</a> <% }else{ Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = SQLConn.openDB(); stmt = conn.createStatement(); // 执行查询等操作 String sql = "select ZYName,YLName,YDY,YX,GX,ZhongYao.ZZ as ZZ from Yaolei,ZhongYao where Yaolei.YLID=ZhongYao.YLID"; PreparedStatement pstmt = conn.prepareStatement(sql); //pstmt.setString(1, "%" + name + "%"); rs = pstmt.executeQuery(); int count = 0; if(rs.next()){ while (rs.next()) { String ZYName = rs.getString("ZYName"); System.out.print(name); if (name.trim().equals(ZYName.trim())) { // if (name.trim().equals(A.trim())) { %>为什么name.trim().equals(ZYName.trim())不成立
时间: 2024-03-04 15:48:19 浏览: 29
可能是因为在数据库中查询出来的`ZYName`前或后有空格或其他不可见字符,导致字符串比较不相等。可以将`System.out.print()`语句放在字符串比较之前打印出两个字符串的值,看看是否有差异。另外,可以使用`String`类的`trim()`方法去除字符串两端的空格再进行比较,或者使用`equalsIgnoreCase()`方法忽略大小写进行比较。具体代码可以这样写:
```java
String name = new String(request.getParameter("ZY").getBytes("ISO-8859-1"), "UTF-8");
System.out.println("name: " + name); // 打印name的值
if (name == null || name.isEmpty()) {
// ...
} else {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = SQLConn.openDB();
stmt = conn.createStatement();
String sql = "select ZYName,YLName,YDY,YX,GX,ZhongYao.ZZ as ZZ from Yaolei,ZhongYao where Yaolei.YLID=ZhongYao.YLID";
PreparedStatement pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
int count = 0;
while (rs.next()) {
String ZYName = rs.getString("ZYName").trim(); // 去除两端空格
System.out.println("ZYName: " + ZYName); // 打印ZYName的值
if (name.equalsIgnoreCase(ZYName)) { // 忽略大小写比较
// ...
}
}
} catch (Exception e) {
// ...
} finally {
// ...
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
帮我找到代码中的错误 ReStu.connect(); //判断请求参数的合法性 String stuno = new String(request.getParameter("StuNo").trim().getBytes("iso8859_1")); String stuname = new String(request.getParameter("StuName").trim().getBytes("iso8859_1")); String password = new String(request.getParameter("Pswd").trim().getBytes("iso8859_1")); ResultSet rs = ReStu.QueryStu(stuno); //获取rs里查到的学号,和姓名,准备进行匹配 while (rs.next()){ String str1 = rs.getString(1); String str2 = rs.getString(2); if (str1.equals(stuno)&str2.equals(stuname)){ int i = ReStu.UpdPw(password,stuno); if(i>0){ out.println("注册成功");}%> 注册成功 <% }}%> 去登录 <% ReStu.close_all();%>
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
下列代码: <% ReStu.connect(); String stuno=new String(request.getParameter("StuNo").trim().getBytes("iso8859_1")); String password=new String(request.getParameter("Pswd").trim().getBytes("iso8859_1")); if(stuno==null || password==null || stuno.equals("") || password.equals("")){ out.println("信息不完整,请重新输入"); %> 返回登录 <%} else{ ResultSet rs=ReStu.QueryStuLog(stuno,password); if(rs.next()){ String stuname=rs.getString("StuName"); //这句要注意,session的还没看!! // session.setAttribute("StuName",stuname); out.println("succeess"); //response.sendRedirect("index.html"); }} ReStu.close_all();%>,运行后抛出异常:org.apache.jasper.JasperException: java.lang.NullPointerException org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:500) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:428) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717,找出错误原因
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
String name = new String(request.getParameter("ZY").getBytes("ISO-8859-1"), "UTF-8"); //System.out.print(name); if (name == null || name.isEmpty()) { %> 请输入中药名称,点击返回重新输入 <% }else{ Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = SQLConn.openDB(); stmt = conn.createStatement(); String sql = "select ZYName,YLName,YDY,YX,GX,ZhongYao.ZZ as ZZ from Yaolei,ZhongYao where Yaolei.YLID=ZhongYao.YLID and ZYName like ?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, "'%" + name + "%'"); rs = pstmt.executeQuery(); int count = 0; if(rs.next()){输入正确的数据,为什么rs.next()为空
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)