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

时间:2013-01-20 20:32:29

标签: java gwt arraylist asynccallback

我正在尝试使用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的最后一行。

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

2 个答案:

答案 0 :(得分:0)

newnoise,SHiv16,我道歉,我做了一个错误的复制粘贴。这是getGameDayList过程的代码

public List<GameDay> getGameDayList() {
    // TODO Auto-generated method stub

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

    try {

        v_sql_statement = "SELECT T1.\"game_day_id\",T1.\"game_day_date\",T1.\"game_day_competition\","
                + " T1.\"game_day_season\",T1.\"game_day_cup_fl\",T1.\"game_day_order\""
                + " FROM \"game_days\" T1 ORDER BY T1.\"game_day_order\" DESC";
        System.out.println("   -------   -------   -------");
        System.out.println("classe ODBConnectionImpl");
        System.out.println("getGameDayList");
        System.out.println("esecuzione statement " + v_sql_statement);
        ResultSet rs = Connection2DB.resultset(v_sql_statement);
        System.out.println("query eseguita");

        int i = 0;

        System.out.println("#########################################");
        System.out.println("ODBConnectionImpl");
        System.out.println("Start creating the list");

        while (rs.next()) {
            v_game_day_tmp.getGameDay(rs.getDate("game_day_date"),
                    rs.getString("game_day_competition"),
                    rs.getString("game_day_season"),
                    rs.getBoolean("game_day_cup_fl"),
                    rs.getInt("game_day_id"), rs.getInt("game_day_order"));

            System.out.println("scanning resultset");
            System.out.println("v_game_day_tmp.getGameDayCompetition()="+v_game_day_tmp.getGameDayCompetition());
            System.out.println("v_game_day_tmp.getGameDayCupFl()="+v_game_day_tmp.getGameDayCupFl());
            System.out.println("v_game_day_tmp.getGameDayId()="+v_game_day_tmp.getGameDayId());
            System.out.println("v_game_day_tmp.getGameDayOrder()"+v_game_day_tmp.getGameDayOrder());
            System.out.println("v_game_day_tmp.getGameDaySeason()"+v_game_day_tmp.getGameDaySeason());
            System.out.println(i);

            v_game_day_list.add(v_game_day_tmp);

            System.out.println("index="+i);
            System.out.println("v_game_day_list.get(i).getGameDayCompetition()="+v_game_day_list.get(i).getGameDayCompetition());
            System.out.println("v_game_day_list.get(i).getGameDayCupFl()="+v_game_day_list.get(i).getGameDayCupFl());
            System.out.println("v_game_day_list.get(i).getGameDayId()="+v_game_day_list.get(i).getGameDayId());
            System.out.println("v_game_day_list.get(i).getGameDayOrder()="+v_game_day_list.get(i).getGameDayOrder());
            System.out.println("v_game_day_list.get(i).getGameDaySeason()="+v_game_day_list.get(i).getGameDaySeason());

            i++;

        }

        for(i=0;i<v_game_day_list.size();i++)
            System.out.println("secondo loop v_game_day_list.get(i).getGameDayId()="+v_game_day_list.get(i).getGameDayId());


        System.out.println("list created");
        return v_game_day_list;

    } catch (SQLException e) {
        System.out.println("ODBConnectionImpl SQLException: "
                + e.toString());
        return null;
    } catch (ParseException e) {
        System.out.println("ODBConnectionImpl ParseException: "
                + e.toString());
        return null;
    }

}

这是共享类GameDay的代码:

package com.fantaprica.shared;

import java.util.Date;
import java.io.Serializable;

@SuppressWarnings("serial")
public class GameDay implements Serializable {

private int v_game_day_id;
private Date v_game_day_date;
private String v_game_day_competition;
private String v_game_day_season;
private boolean v_game_day_cup_fl;
private int v_game_day_order;

public GameDay() {
    // TODO Auto-generated constructor stub

}

// questo metodo viene uitilizzato per l'UPDATE
public void setGameDay(int p_game_day_id,Date p_game_day_date, String p_game_day_competition,
        String p_game_day_season, boolean p_game_day_cup_fl) {

    v_game_day_id = p_game_day_id;
    v_game_day_date = p_game_day_date;
    v_game_day_competition = p_game_day_competition;
    v_game_day_season = p_game_day_season;
    v_game_day_cup_fl = p_game_day_cup_fl;

}

// questo metodo viene utilizzato per l'INSERT
public void setGameDay(Date p_game_day_date, String p_game_day_competition,
        String p_game_day_season, boolean p_game_day_cup_fl) {

    v_game_day_date = p_game_day_date;
    v_game_day_competition = p_game_day_competition;
    v_game_day_season = p_game_day_season;
    v_game_day_cup_fl = p_game_day_cup_fl;

}

public void getGameDay(Date p_game_day_date, String p_game_day_competition,
        String p_game_day_season, boolean p_game_day_cup_fl,
        int p_game_day_id, int p_game_day_order) {

    v_game_day_date = p_game_day_date;
    v_game_day_competition = p_game_day_competition;
    v_game_day_season = p_game_day_season;
    v_game_day_cup_fl = p_game_day_cup_fl;
    v_game_day_id = p_game_day_id;
    v_game_day_order = p_game_day_order;
}

public Date getGameDayDate() {
    return v_game_day_date;
}

public String getGameDayCompetition() {
    return v_game_day_competition;
}

public String getGameDaySeason() {
    return v_game_day_season;
}

public boolean getGameDayCupFl() {
    return v_game_day_cup_fl;
}

public int getGameDayId() {
    return v_game_day_id;
}

public int getGameDayOrder() {
    return v_game_day_order;
}
}

我读到这个问题可能是thr类属性的“静态”实现,但不是这种情况,因为GameDay类没有静态属性

的Dario

答案 1 :(得分:0)

我认为问题在于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())应该解决你的问题。

问候。