问题描述:

I'm loading all the players from a database into the class Player, this happens in the class DB.

// class DB

ArrayList<Player> player= new ArrayList();

public void loadPlayers() {

try {

stmt = conn.createStatement();

rs = stmt.executeQuery("SELECT * FROM players");

while (rs.next()) {

player.add(new Player(rs.getInt("id"), rs.getString("name"), rs.getInt("score"));

}

rs.close();

stmt.close();

} catch (SQLException ex) {

System.out.println("error selecting");

System.err.println(ex);

}

}

// class Player

int id, score;

String name;

public Customer(int id, String name, int score) {

this.id = id;

this.name = name;

this.score = score;

}

public String getName(int id) {

return name;

}

// View

// do i need here something like "ArrayList<Player> player"?

DB.INSTANCE.loadPlayers(); // I use the Singleton pattern

System.out.println(player.getName(5));

How can I access the class Player from other classes besides DB?

For example I want to call the method player.getName(5); from View and get its particular name.

Do I have to load all the players in something like an Array or an ArrayList?

网友答案:

Yes, you need a Collection of Player.

while (rs.next()) {
   player = new Player(rs.getInt("id"), rs.getString("name"), rs.getInt("score"));
}

will only hold the last retrieved player from the database, because there's only one member in your class.

If you want something like DB.getName(5), I'd suggest a HashMap<Integer,Player> member, that way lookup by id will be fast.

网友答案:

Your DB class needs to have not a Player variable, but a collection of Player -- i.e., List<Player>. Then, you can simply have a method that returns that collection, or a copy of the collection, so that other classes have access to it.

网友答案:

With the way you have it set up, yes, you need to save off the players list in view. It would make more sense to have the loadPlayers method return a list, and then use that value to set a list in View.

网友答案:

Right now you're loading each player into the same member variable in DB, which isn't very useful. You should create a method in DB which returns a List of Player. Construct an ArrayList in this method and do something like this inside your loop:

players.add(new Player(rs.getInt("id"), rs.getString("name"), rs.getInt("score")));

Then, from your view layer, you can do something like this:

DB db = new DB();
for (Player p : db.getPlayers())
    System.out.println(p.getName());

Updated

DB

public class DB {

    ...

    private static DB instance;

    public static DB getInstance() {
        if (instance == null)
            instance = new DB();
        return instance;
    }

    public List<Player> loadPlayers() {
         List<Player> players = new ArrayList<Player>();
         try {
             Statement stmt = conn.createStatement();
             rs = stmt.executeQuery("SELECT * FROM players");
             while (rs.next()) {
                 players.add(new Player(rs.getInt("id"), rs.getString("name"), rs.getInt("score"));
             }
             rs.close();
             stmt.close();
         } catch (SQLException ex) {
             System.out.println("error selecting");
             System.err.println(ex);
         }
         return players;
    }
}

View

public class View {

    public static void main(String[] args) {
        DB db = DB.getInstance();
        List<Player> players = db.loadPlayers();
        for (Player p : players)
            System.out.println(p.getName());
    }

}
相关阅读:
Top