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 12:48:19 浏览: 70
可能是因为在数据库中查询出来的`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 {
// ...
}
}
```
阅读全文