[英]String Comparison not working in Java
這是我的代碼:為什么if ((rs.getString("order_status")) != ordstat)
不起作用?
String sql = "select order_id, order_dt, order_status, prod_name from orders where user_id = '"+user+"'";
try{
s = con.createStatement();
rs = s.executeQuery(sql);
String ordstat = "Pending";
%>
<%
while( rs.next() ){
if ((rs.getString("order_status")) != ordstat){
%>
<div align="center">
<table border="1" cellpadding="5">
<tr>
<th>Select</th>
<th>Order ID</th>
<th>Name</th>
<th>Date</th>
<th>Status</th>
</tr>
<tr>
<form action = 'deleteorders' >
<td><input type="checkbox"/></td>
<td><%= rs.getString("order_id") %></td>
<td><%= rs.getString("prod_name") %></td>
<td><%= rs.getString("order_dt") %></td>
<td><%= rs.getString("order_status") %></td>
<%
}
}
%>
</form>
</tr>
</table>
<br>
<INPUT TYPE=SUBMIT VALUE="CANCEL">
</div>
<%
}
嘗試一下,因為這是推薦的方法
if (!(rs.getString("order_status")).equals(ordstat))
請注意, ==比較對象的引用,如果要比較字符串的實際內容,請使用equals()函數
在Java中,使用相等運算符(“ ==”)檢查String意味着您正在將內存中該String對象的地址與另一個String對象進行有效比較。 它從不比較字符串的內容。
要比較2個字符串,請始終使用.equals方法
String x = "Hello";
String y = "World";
if (x.equals(y)) //if the strings are equals
System.out.println("Equals");
if (!x.equals(y)) //if the strings are not equal
System.out.println("Not Equal");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.