arrays - Java: Access class -
i'm loading players database class player
, happens in class db
.
// class db arraylist<player> player= new arraylist(); public void loadplayers() { try { stmt = conn.createstatement(); rs = stmt.executequery("select * 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 // need here "arraylist<player> player"? db.instance.loadplayers(); // use singleton pattern system.out.println(player.getname(5));
how can access class player
other classes besides db? example want call method player.getname(5);
view
, particular name.
have load players in array or arraylist?
yes, need collection
of player
.
while (rs.next()) { player = new player(rs.getint("id"), rs.getstring("name"), rs.getint("score")); }
will hold last retrieved player database, because there's 1 member in class.
if want db.getname(5)
, i'd suggest hashmap<integer,player>
member, way lookup id fast.
Comments
Post a Comment