如果ZYName为char型,name为前端获取的String,如何比较二者一样
时间: 2024-03-04 20:48:19 浏览: 84
如果`ZYName`为`char`类型,需要将其转换为`String`类型后再进行比较。可以使用`String`类的`valueOf()`方法将`char`类型转换为`String`类型,然后使用`equals()`方法进行比较。具体代码可以这样写:
```java
String name = new String(request.getParameter("ZY").getBytes("ISO-8859-1"), "UTF-8");
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 = String.valueOf(rs.getChar("ZYName")).trim(); // 转换为String类型并去除两端空格
if (name.equalsIgnoreCase(ZYName)) { // 忽略大小写比较
// ...
}
}
} catch (Exception e) {
// ...
} finally {
// ...
}
}
```
阅读全文