Search code examples
javaif-statementjdbcresultset

jdbc result set with if then statements


My goal: I am trying to query my fire/rescue database to determine the current status of ambulances. Once I have this current disposition I generate a scalable vector graphic based on that disposition. For example, a working ambulance would have a red rectangle around the information. An available ambulance would have a white rectangle with a green border, etc. The problem: I am able to reach into my database and print out a result set. However my IF THEN statement does not work properly. All of the printed out put conforms to the final ELSE statement. In other words, although I am checking for a disposition == "Working" the program does not recognize it and skips right over to the final ELSE statement. Shown below is my code which has been simplified to remove the SVG and just print out a line of text.

package javaDatabase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class CurrentDisposition3
{

    public static void main(String[] args) throws SQLException,
            ClassNotFoundException
    {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        Connection conn = DriverManager
                .getConnection("jdbc:sqlserver://999.99.99.99;database=myDatabase;integratedsecurity=false;user=myUser;password=myPassword");

        Statement sta = conn.createStatement();
        String qryCurrentDisposition = "SELECT Ambulance, FinalTimeStamp, UnitStatus, Disposition FROM EMSDatabase";
        ResultSet rs = sta.executeQuery(qryCurrentDisposition);

        while (rs.next())
        {
            String truck = rs.getString("Ambulance");
            String timeStamp = rs.getString("FinalTimeStamp");
            String unitStatus = rs.getString("UnitStatus");
            String disposition = rs.getString("Disposition");
            System.out.println(truck + ", " + timeStamp + ", " + unitStatus
                    + ", " + disposition);
            if (disposition == "Working")
            {
                System.out.println("This truck is working");
            } else if (disposition == "Free")
            {
                System.out.println("This truck is free");
            } else if (disposition == "OutofService")
            {
                System.out.println("Out of Service temporarily");
            } else if (disposition == "Standby")
            {
                System.out.println("Standing by");
            } else
            {
                System.out.println("not sure");
            }

        }
    }
}

My output in the console looks like this:

M01, 09:00:17, USTA, Working

not sure

M02, 08:52:21, USLC, Standby

not sure

M03, 08:54:27, USTA, Working

not sure

M04, 08:59:59, USAR, Working

not sure

M05, 08:02:18, USLC, Standby

not sure

Please notice that although M01 is Working, the second line says "not sure" which would be the output of the final ELSE statement. Why is my If-THEN statement not processing the ResultSet correctly?


Solution

  • You should use equals() method to compare strings. You cannot use == comparison operator for comparing strings.

    For example:

    if(yourString.equals("sometext")//correct way
    if(yourString == "sometext")//incorrect way
    

    Also see equalsIgnoreCase() method here