如何在Java中执行while循环和for循环条件?

huangapple 未分类评论47阅读模式
英文:

How to execute while loop and for loop conditions in Java?

问题

以下是翻译好的内容:

我有一个情景,其中有5个员工ID。我想要检查这些员工的ID状态(状态也是从数据库中获取的)。员工的状态有:

已完成等待错误

首先,员工ID会在一定的时间内从等待状态移动到已完成状态。一旦所有员工ID都移动到已完成状态,我必须执行另一个查询(直到所有员工ID都移动到已完成状态,我才能执行下一个查询)。

如果任何员工ID转移到错误状态,我只需显示他们的员工ID。这是我尝试过的代码:

public void ValidateStatusQuery() throws Exception {
    int counter = 0;
    List<Integer> EmplyeeIDList = new ArrayList<Integer>(Arrays.asList(123, 345, 456, 567));
    Class.forName(dbdriver);
    con = DriverManager.getConnection(URL2, DB_UserName, DB_Password);
    log.info("已建立数据库连接");
    int i = 0;
    while (i++ < EmplyeeIDList.size()) {
        if (counter == 5)
            break;
        stmt = con.prepareStatement("select * from tb_Employee where EmployeeID=?");
        stmt.setInt(1, EmplyeeIDList.get(i));
        rs3 = stmt.executeQuery();
        while (rs3.next()) {
            getCurrentStatus = rs3.getString("CurrentStatus");
            if (getCurrentStatusID.equals("1")) {
                log.info(EmplyeeIDList.get(i) + " 处于已完成状态");
                counter++; // 每当员工ID位于已完成队列中时递增计数器变量。
                System.out.println("计数为" + counter);
            } else if (getCurrentStatusID.equals("7")) {
                log.info(EmplyeeIDList.get(i) + " 处于等待状态");
                Thread.sleep(10000);
            } else if (getCurrentStatusID.equals("3")) {
                log.info(EmplyeeIDList.get(i) + " 处于错误状态");
            }
        } // rs循环结束
    }
}

这是输出结果:

123 处于等待状态
345 处于等待状态
456 处于等待状态
567 处于等待状态

一旦EmplyeeIDList.size()完成,它就会退出while循环。

预期的输出是:我希望等待直到所有案例ID都移动到已完成状态。

英文:

I have a scenario where there are 5 employee ids. I want to check these IDs status (Status also I am getting from data base). The status of employees are:

Completed, Waiting, Error.

First, the employee id will move from Waiting to Complete in a certain amount of time. Once all the employee ids are moved to Complete status, I have to execute another query (until all employee ids move to completed status, I cannot execute the next query).

If any employee ids move to Error state, I have to just display their employee ids. This is what I have tried:

public void ValidateStatusQuery() throws Exception {
    int counter = 0;
    List&lt;Integer&gt; EmplyeeIDList= new ArrayList&lt;Integer&gt;(Arrays.asList(123,345,456,567))
    Class.forName(dbdriver);	
    con=DriverManager.getConnection(URL2,DB_UserName,DB_Password);
    log.info(&quot;Data base connection is established&quot;);
    int i=0;
    while(i++&lt;EmplyeeIDList.size()) {
	    if(counter==5)
            break;
	    stmt = con.prepareStatement(&quot;select * from tb_Employee where EmployeeID=?&quot;);					
	    stmt.setInt(1,EmplyeeIDList.get(i));			
	    rs3= stmt.executeQuery();
	    while(rs3.next()) {				
	        getCurrentStatus= rs3.getString(&quot;CurrentStatus&quot;);
	        if(getCurrentStatusID.equals(&quot;1&quot;)) {
	            log.info(+EmplyeeIDList.get(i) + &quot; is in Completed Status&quot;);
	            counter++;//Incrementing counter variable whenever employee id is in Completed Queue.
	            System.out.println(&quot;Count is&quot;+counter);
	        } else if(getCurrentStatusID.equals(&quot;7&quot;)) {
	            log.info(+EmplyeeIDList.get(i) + &quot; is in Waiting Status&quot;);
	            Thread.sleep(10000);
	        } else if(getCurrentStatusID.equals(&quot;3&quot;)) {
	            log.info(+EmplyeeIDList.get(i) + &quot; is in Error Status&quot;);
	        }
        }	//End of While rs loop		
    }
}

This is the output:

123 is in Waiting status
345 is in Waiting status
456 is in Waiting status
567 is in Waiting status

It's coming out of the while loop once the EmplyeeIDList.size() is completed.

The expected output is: I want to wait till all the caseids move to Completed Status.

答案1

得分: 0

我可以在这里提供部分答案。如果您只想找出已完成状态的员工数量,您应该使用此查询:

SELECT COUNT(*) AS cnt
FROM tb_Employee
WHERE CurrentStatus = '1';

以下是执行此操作的Java代码:

String sql = "SELECT COUNT(*) AS cnt FROM tb_Employee WHERE CurrentStatus = '1'";
PreparedStatement stmt = con.prepareStatement(sql);          
ResultSet rs = stmt.executeQuery();
if (rs.next()) {             
    System.out.println("已完成员工数量:" + rs.getInt("cnt"));
}

您还可以运行类似的查询,以查找不在完成状态或处于其他状态的员工数量。此查询可以作为定期任务或线程的一部分运行。

编辑: 下面是将查询范围限制为仅特定员工列表的一种方法。

// 给定 EmplyeeIDList
StringBuilder whereIn = new StringBuilder("(");
for (int i = 1; i < EmplyeeIDList.size(); ++i) {
    whereIn.append(", ?");
}
whereIn.append(")");
String sql = "SELECT COUNT(*) AS cnt FROM tb_Employee ";
sql += "WHERE CurrentStatus = '1' AND EmployeeID IN " + whereIn;
for (int i = 1; i <= EmplyeeIDList.size(); ++i) {
    stmt.setInt(i, EmplyeeIDList.get(i - 1));
}
PreparedStatement stmt = con.prepareStatement(sql);          
ResultSet rs = stmt.executeQuery();
if (rs.next()) {             
    System.out.println("已完成员工数量:" + rs.getInt("cnt"));
}
英文:

I can offer a partial answer here. If you just want to find out how many employees are in the completed status, you should use this query:

SELECT COUNT(*) AS cnt
FROM tb_Employee
WHERE CurrentStatus = &#39;1&#39;;

Here is Java code to do this:

String sql = &quot;SELECT COUNT(*) AS cnt FROM tb_Employee WHERE CurrentStatus = &#39;1&#39;&quot;;
PreparedStatement stmt = con.prepareStatement(sql);          
ResultSet rs = stmt.executeQuery();
if (rs3.next()) {             
    System.out.println(&quot;Number of completed employees: &quot; + rs.getInt(&quot;cnt&quot;));
}

You could also run a similar query to find the count of employees not in completed status, or in some other status. This query could be called as part of a scheduled task or thread.

Edit: Here is one way to limit the scope of the query to only a certain list of employees.

// given EmplyeeIDList
StringBuilder whereIn = new StringBuilder(&quot;(?&quot;);
for (int i=1; i &lt; EmplyeeIDList.size(); ++i) {
    whereIn.append(&quot;, ?&quot;);
}
whereIn.append(&quot;)&quot;);
String sql = &quot;SELECT COUNT(*) AS cnt FROM tb_Employee &quot;;
sql += &quot;WHERE CurrentStatus = &#39;1&#39; AND EmployeeID IN &quot; + whereIn;
for (int i=1; i &lt;= EmplyeeIDList.size(); ++i) {
    stmt.setInt(i, EmplyeeIDList.get(i-1); ++i);
}
PreparedStatement stmt = con.prepareStatement(sql);          
ResultSet rs = stmt.executeQuery();
if (rs3.next()) {             
    System.out.println(&quot;Number of completed employees: &quot; + rs.getInt(&quot;cnt&quot;));
}

答案2

得分: 0

public void ValidateStatusQuery() throws Exception {
int counter = 0;
List EmplyeeIDList = new ArrayList(Arrays.asList(123, 345, 456, 567));
Class.forName(dbdriver);
con = DriverManager.getConnection(URL2, DB_UserName, DB_Password);
log.info("数据库连接已建立");
int i = 0;
while (i++ < EmplyeeIDList.size()) {
if (counter == 5)
break;
stmt = con.prepareStatement("select * from tb_Employee where EmployeeID=?");
stmt.setInt(1, EmplyeeIDList.get(i));
rs3 = stmt.executeQuery();
while (rs3.next()) {
getCurrentStatus = rs3.getString("CurrentStatus");
if (getCurrentStatusID.equals("1")) {
log.info(+EmplyeeIDList.get(i) + " 处于已完成状态");
counter++; // 每当员工 ID 处于已完成队列中时,递增计数器变量。
System.out.println("计数为" + counter);
} else if (getCurrentStatusID.equals("7")) {
log.info(+EmplyeeIDList.get(i) + " 处于等待状态");
Thread.sleep(10000);
--i; // 将值减少 1
} else if (getCurrentStatusID.equals("3")) {
log.info(+EmplyeeIDList.get(i) + " 处于错误状态");
}
} // rs 循环结束
}
}

英文:
public void ValidateStatusQuery() throws Exception {
int counter = 0;
List&lt;Integer&gt; EmplyeeIDList= new ArrayList&lt;Integer&gt;(Arrays.asList(123,345,456,567))
Class.forName(dbdriver);    
con=DriverManager.getConnection(URL2,DB_UserName,DB_Password);
log.info(&quot;Data base connection is established&quot;);
int i=0;
while(i++&lt;EmplyeeIDList.size()) {
    if(counter==5)
        break;
    stmt = con.prepareStatement(&quot;select * from tb_Employee where EmployeeID=?&quot;);                    
    stmt.setInt(1,EmplyeeIDList.get(i));            
    rs3= stmt.executeQuery();
    while(rs3.next()) {             
        getCurrentStatus= rs3.getString(&quot;CurrentStatus&quot;);
        if(getCurrentStatusID.equals(&quot;1&quot;)) {
            log.info(+EmplyeeIDList.get(i) + &quot; is in Completed Status&quot;);
            counter++;//Incrementing counter variable whenever employee id is in  
            Completed Queue. 
            System.out.println(&quot;Count is&quot;+counter);
        } else if(getCurrentStatusID.equals(&quot;7&quot;)) {
            log.info(+EmplyeeIDList.get(i) + &quot; is in Waiting Status&quot;);
            Thread.sleep(10000);
            --i;//Decreasing the value by 1
        } else if(getCurrentStatusID.equals(&quot;3&quot;)) {
            log.info(+EmplyeeIDList.get(i) + &quot; is in Error Status&quot;);
        }
    }   //End of While rs loop      
}

}

huangapple
  • 本文由 发表于 2020年4月9日 01:02:40
  • 转载请务必保留本文链接:https://java.coder-hub.com/61106070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定