-1

Here is my code : Why is the if ((rs.getString("order_status")) != ordstat) not working?

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>
<%

}
Ahmed
  • 2,176
  • 5
  • 26
  • 40
JavascriptL
  • 19
  • 1
  • 4
  • 1
    Define 'not working'. What's `ordstat`, and what's `rs.getString("order_status")`? And please show the rendered HTML output instead of this. – danronmoon Nov 14 '14 at 17:48
  • @danronmoon the code clearly defines what ordstat is. – Timmerz Nov 14 '14 at 17:50
  • @Timmerz yes you're correct, now it does. Touche – danronmoon Nov 14 '14 at 17:52
  • @JavascriptL this code isn't really enough information. For example, we don't even know what is coming back in the sql query. You really need to view this with a debugger. – Timmerz Nov 14 '14 at 17:53
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Luke Woodward Nov 16 '14 at 11:11

2 Answers2

4

Try this , as this is the recommended way of doing it

if (!(rs.getString("order_status")).equals(ordstat))

Note that == compares references of the objects and if you want to compare actual content of strings , use equals() function

Ahmed
  • 2,176
  • 5
  • 26
  • 40
0

In java checking for String using the equality operator ("==") means that you are effectively comparing the address of that String object in memory to another String object. It never compares the contents of the String.

To compare 2 Strings always use .equals method

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");
Shayan Jalil
  • 588
  • 5
  • 17