gpt4 book ai didi

java - GWT:尝试通过 GWT AsyncCallback 检索对象列表,我得到一个仅包含最后一个值的列表

转载 作者:行者123 更新时间:2023-12-01 04:54:17 28 4
gpt4 key购买 nike

我正在尝试通过 AsyncCallback 调用检索对象列表。一切看起来都很好,直到我查看 onSuccess 过程中的列表数据。事实上,我收到了一个具有正确行数的列表,但每一行都与 SQL 语句最后检索到的行相同

这是 EntryPoint 客户端模块:

package com.fantaprica.client;

import java.util.List;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.fantaprica.shared.GameDay;


/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class Fantaprica implements EntryPoint {

private final ODBConnectionAsync odbconnectionSvc = GWT
.create(ODBConnection.class);

/**
* Entry point classes define <code>onModuleLoad()</code>.
*/

public void onModuleLoad() {

odbconnectionSvc
.getGameDayList(new AsyncCallback<List<GameDay>>() {
public void onFailure(Throwable caught) {
}

public void onSuccess(List<GameDay> result) {

System.out.println("#########################################");
System.out.println("OnModuleLoad");
for (int i = 0; i < result.size(); i++) {
System.out.println("index="+i);
System.out.println("result.get(i).getGameDayCompetition()="+result.get(i).getGameDayCompetition());
System.out.println("result.get(i).getGameDayCupFl()"+result.get(i).getGameDayCupFl());
System.out.println("result.get(i).getGameDayId()="+result.get(i).getGameDayId());
System.out.println("result.get(i).getGameDayOrder()="+result.get(i).getGameDayOrder());
System.out.println("result.get(i).getGameDaySeason()="+result.get(i).getGameDaySeason());
}
}
});
}
}

这是服务器端的实现:

package com.fantaprica.server;

import java.text.ParseException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Connection2DB {

public static ResultSet resultset(String p_sql_statement) throws ParseException {

//String db_file_name_prefix = "/home/dario/workspace/fantaprica/src/com/fantaprica/server/database/database";
//String db_file_name_prefix = "/home/dario/workspace/fantaprica/war/database/database";
String db_file_name_prefix = "/home/dario/Workspace/Fantaprica/war/fantaprica/database/database";
Connection con = null;

// connect to the database. This will load the db files and start the
// database if it is not alread running.
// db_file_name_prefix is used to open or create files that hold the
// state
// of the db.
// It can contain directory names relative to the
// current working directory
try {
Class.forName("org.hsqldb.jdbcDriver");
con = DriverManager.getConnection("jdbc:hsqldb:file:"
+ db_file_name_prefix, // filenames prefix
"sa", // user
""); // pass
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery(p_sql_statement);
System.out.println("query "+p_sql_statement+" eseguita");
statement.close();
con.close();
return rs;
} catch (ClassNotFoundException ex) {
Logger.getLogger(TestConnection.class.getName()).log(Level.SEVERE,
null, ex);
System.out.println("errore "+ex.toString());
return null;
} catch (SQLException ex) {
Logger.getLogger(TestConnection.class.getName()).log(Level.SEVERE,
null, ex);
ex.printStackTrace();
System.out.println("errore "+ex.toString());
return null;
}

}

public static void execstmt(String p_sql_statement) throws ParseException {

String db_file_name_prefix = "/home/dario/Workspace/Fantaprica/war/fantaprica/database/database";
Connection con = null;

// connect to the database. This will load the db files and start the
// database if it is not alread running.
// db_file_name_prefix is used to open or create files that hold the
// state
// of the db.
// It can contain directory names relative to the
// current working directory
try {
Class.forName("org.hsqldb.jdbcDriver");
con = DriverManager.getConnection("jdbc:hsqldb:file:"
+ db_file_name_prefix, // filenames prefix
"sa", // user
""); // pass
Statement statement = con.createStatement();
statement.executeUpdate(p_sql_statement);

System.out.println("statement "+p_sql_statement+" eseguito");

statement.close();
con.close();

} catch (ClassNotFoundException ex) {
Logger.getLogger(TestConnection.class.getName()).log(Level.SEVERE,
null, ex);
System.out.println("errore "+ex.toString());

} catch (SQLException ex) {
Logger.getLogger(TestConnection.class.getName()).log(Level.SEVERE,
null, ex);
ex.printStackTrace();
System.out.println("errore "+ex.toString());
}

}

}

当我运行代码时,我得到以下结果:

#########################################
ODBConnectionImpl
Start creating the list
scanning resultset
v_game_day_tmp.getGameDayCompetition()=campionato
v_game_day_tmp.getGameDayCupFl()=false
v_game_day_tmp.getGameDayId()=0
v_game_day_tmp.getGameDayOrder()15
v_game_day_tmp.getGameDaySeason()2012/2013
0
index=0
v_game_day_list.get(i).getGameDayCompetition()=campionato
v_game_day_list.get(i).getGameDayCupFl()=false
v_game_day_list.get(i).getGameDayId()=0
v_game_day_list.get(i).getGameDayOrder()=15
v_game_day_list.get(i).getGameDaySeason()=2012/2013
scanning resultset
v_game_day_tmp.getGameDayCompetition()=campionato
v_game_day_tmp.getGameDayCupFl()=false
v_game_day_tmp.getGameDayId()=1
v_game_day_tmp.getGameDayOrder()14
v_game_day_tmp.getGameDaySeason()2012/2013
1
index=1
v_game_day_list.get(i).getGameDayCompetition()=campionato
v_game_day_list.get(i).getGameDayCupFl()=false
v_game_day_list.get(i).getGameDayId()=1
v_game_day_list.get(i).getGameDayOrder()=14
v_game_day_list.get(i).getGameDaySeason()=2012/2013
scanning resultset
v_game_day_tmp.getGameDayCompetition()=campionato
v_game_day_tmp.getGameDayCupFl()=false
v_game_day_tmp.getGameDayId()=2
v_game_day_tmp.getGameDayOrder()13
v_game_day_tmp.getGameDaySeason()2012/2013
2
index=2
v_game_day_list.get(i).getGameDayCompetition()=campionato
v_game_day_list.get(i).getGameDayCupFl()=false
v_game_day_list.get(i).getGameDayId()=2
v_game_day_list.get(i).getGameDayOrder()=13
v_game_day_list.get(i).getGameDaySeason()=2012/2013
scanning resultset
v_game_day_tmp.getGameDayCompetition()=mundialito
v_game_day_tmp.getGameDayCupFl()=false
v_game_day_tmp.getGameDayId()=3
v_game_day_tmp.getGameDayOrder()12
v_game_day_tmp.getGameDaySeason()2012/2013
3
index=3
v_game_day_list.get(i).getGameDayCompetition()=mundialito
v_game_day_list.get(i).getGameDayCupFl()=false
v_game_day_list.get(i).getGameDayId()=3
v_game_day_list.get(i).getGameDayOrder()=12
v_game_day_list.get(i).getGameDaySeason()=2012/2013
list created
#########################################
OnModuleLoad
index=0
result.get(i).getGameDayCompetition()=mundialito
result.get(i).getGameDayCupFl()false
result.get(i).getGameDayId()=3
result.get(i).getGameDayOrder()=12
result.get(i).getGameDaySeason()=2012/2013
index=1
result.get(i).getGameDayCompetition()=mundialito
result.get(i).getGameDayCupFl()false
result.get(i).getGameDayId()=3
result.get(i).getGameDayOrder()=12
result.get(i).getGameDaySeason()=2012/2013
index=2
result.get(i).getGameDayCompetition()=mundialito
result.get(i).getGameDayCupFl()false
result.get(i).getGameDayId()=3
result.get(i).getGameDayOrder()=12
result.get(i).getGameDaySeason()=2012/2013
index=3
result.get(i).getGameDayCompetition()=mundialito
result.get(i).getGameDayCupFl()false
result.get(i).getGameDayId()=3
result.get(i).getGameDayOrder()=12
result.get(i).getGameDaySeason()=2012/2013

如您所见,列表在 ODBConnectionImpl 中正确创建,并且由 4 个不同的行组成。但是,当我扫描返回到 AsyncCallback 调用的列表时,我有四个相等的行,全部等于 ResultSet 中的最后一行。

感谢您的帮助和建议,达里奥

最佳答案

我相信问题出在 getGameDayList 中的代码:

List<GameDay> v_game_day_list = new ArrayList<GameDay>();
GameDay v_game_day_tmp = new GameDay();
String v_sql_statement;

从此代码块中删除 GameDay v_game_day_tmp = new GameDay()。

您正在做的是在列表中使用同一个对象 (v_game_day_tmp) 4 次。当 while 循环重新填充 v_game_day_tmp 并添加到列表中时,列表中的先前条目仍然引用同一对象 (v_game_day_tmp),因此必然具有相同的值。

不要使用 1 个临时对象。

该对象必须使用 new() 重新创建、填充,然后添加到列表中。

简单移动GameDay v_game_day_tmp = new GameDay();成为 while(rs.next()) 的第一行应该可以解决您的问题。

问候。

关于java - GWT:尝试通过 GWT AsyncCallback 检索对象列表,我得到一个仅包含最后一个值的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14428958/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com