带有if-then语句的jdbc结果集



我的目标:我正在尝试查询我的消防/救援数据库,以确定救护车的当前状态。一旦我有了这个当前的倾向,我就会基于这个倾向生成一个可缩放的矢量图。例如,一辆正在工作的救护车在信息周围会有一个红色矩形。一辆可用的救护车会有一个带绿色边框的白色矩形,等等。问题是:我能够访问我的数据库并打印出一个结果集。然而,我的IF THEN语句不能正常工作。所有打印出来的输出都符合ELSE的最终声明。换句话说,尽管我正在检查处置=="Working",但程序无法识别它,并直接跳到最后的ELSE语句。下面显示的是我的代码,它被简化为删除SVG并只打印出一行文本。

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");
            }
        }
    }
}

我在控制台中的输出如下:

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

请注意,尽管M01正在工作,但第二行表示"不确定"哪一行将是最终ELSE语句的输出。为什么我的If THEN语句没有正确处理ResultSet?

您应该使用equals()方法来比较字符串。不能使用==比较运算符来比较字符串。

例如:

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

另请参阅此处的equalsIgnoreCase()方法

非常感谢Nabin回答这个问题。最后的代码是这样的:

 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.equals("Working"))
            {
                System.out.println("This truck is working");
            } else if (disposition.equals("Free"))
            {
                System.out.println("This truck is free");
            } else if (disposition.equals("OutofService"))
            {
                System.out.println("Out of Service temporarily");
            } else if (disposition.equals("Standby"))
            {
                System.out.println("Standing by");
            } else
            {
                System.out.println("not sure");
            }
        }

相关内容

  • 没有找到相关文章

最新更新