我有一个代码告诉我Firebase数据库中添加了一些内容。当一个孩子被添加或更改时,我的Java代码被执行并且在for循环中我正在执行Mysql查询。现在的问题是for循环执行频繁而不等待查询完成。我如何等待查询完成。
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String arg1) {
// TODO Auto-generated method stub
String chatToken = dataSnapshot.getKey();
String message = "";
String messageId = "";
String bg_img_url = "";
String domain = "";
String end_user = "";
String mobile = "";
String time = "";
String date = "";
String sender = "";
String status = "0";
int lastCount = 1;
for(DataSnapshot ds : dataSnapshot.getChildren()) {
try {
System.out.println(lastCount+"getChildrenCount() -------------- "+ dataSnapshot.getChildrenCount());
if(lastCount == dataSnapshot.getChildrenCount()){
messageId = ds.getKey();
System.out.println("onChildChanged ds.getValue().toString() -------------- "+ ds.getValue().toString());
HashMap msgJsn = (HashMap) ds.getValue();
message = msgJsn.get("message")+"";
time = msgJsn.get("time")+"";
date = msgJsn.get("date")+"";
sender = msgJsn.get("sender")+"";
if(msgJsn.get("status") != null){
status = msgJsn.get("status")+"";
}
if(msgJsn.get("bg_img_url") != null){
bg_img_url = msgJsn.get("bg_img_url")+"";
}
if(msgJsn.get("domain") != null){
domain = msgJsn.get("domain")+"";
}
if(msgJsn.get("end_user") != null){
end_user = msgJsn.get("end_user")+"";
}
if(msgJsn.get("mobile") != null){
mobile = msgJsn.get("mobile")+"";
}
if(sender.equals("web"))
date = msgJsn.get("Date")+"";
System.out.println("onChildChanged chatToken -------------- "+ chatToken);
System.out.println("onChildChanged messageId -------------- "+ messageId);
System.out.println("onChildChanged message -------------- "+ message);
System.out.println("onChildChanged time -------------- "+ time);
System.out.println("onChildChanged sender -------------- "+ sender);
System.out.println("onChildChanged date -------------- "+ date);
System.out.println("onChildChanged bg_img_url -------------- "+ bg_img_url);
System.out.println("onChildChanged domain -------------- "+ domain);
System.out.println("onChildChanged end_user -------------- "+ end_user);
System.out.println("onChildChanged mobile -------------- "+ mobile);
System.out.println("onChildChanged status -------------- "+ status);
String result = dbcs.saveChatMessage(chatToken, message, messageId, bg_img_url, domain, end_user, mobile, time, date, sender, status);
System.out.println("onChildChanged result ------------------- "+ result);
}
lastCount++;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
数据库代码是:
public String saveChatMessage(String chatToken, String message, String messageId, String bg_img_url, String domain, String end_user, String mobile, String time, String date, String sender, String status) throws JSONException{ //NEW
String result = "";
try{
String sql = "SELECT * FROM app_chat_message_list where message_id = '"+messageId+"'";
System.out.println("sql : "+sql);
ResultSet rs = stmt.executeQuery(sql);
if (!rs.next()){
sql = "INSERT INTO app_chat_message_list(chat_token, message, message_id, mobile, domain, bg_img_url, end_user, sender, date, time, status) VALUES ('"+chatToken+"', '"+message+"', '"+messageId+"', '"+mobile+"', '"+domain+"', '"+bg_img_url+"', '"+end_user+"', '"+sender+"', '"+date+"', '"+time+"', '"+status+"')";
result =stmt.executeUpdate(sql)+"";
}
// stmt.close();
// conn.close();
}
catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
result = "error";
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
result = "error";
}finally{
//finally block used to close resources
/*try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
result = "error";
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
result = "error";
se.printStackTrace();
}//end finally try
*/ }//end try
return result;
}
答案 0 :(得分:0)
使用sleep()方法放置您想要等待的位置