Monday, January 13, 2014

Trouble populating a JList using a database ?




I am trying to populate a Jlist for information stored on a database. The database contains football club names, but instead of being populated with their names it just has a hexadecimal reference ( Club@183357c4 ) for each club object.




here is my club class leaving out the setters and getters






Java Code:






public class Club {

private String[] fixtureList;
private String clubName;
private int totalFixtures;
private int gamesPlayed;
private int remainingFixtures;
private int gamesWon;
private int gamesDrawn;
private int gamesLost;
private int goalsFor;
private int goalsAgainst;
private int goalDifference;
private int pointsTally;

public Club(){} // no args constructor

public Club( String name ){

setClubName( name );

} // end of Name args constructor

public Club(
String name, String[] fixList, int totalFix, int remainingFix,
int fixturesWon, int fixturesDrawn, int fixturesLost,
int playedFix, int goalsScored, int goalsCon ){

setClubName( name );
setFixtureList( fixList );
setTotalFixtures( totalFix );
setGamesWon( fixturesWon );
setGamesDrawn( fixturesDrawn );
setGamesLost( fixturesLost );
setGoalsFor( goalsScored );
setGoalsAgainst( goalsCon );

} // end of args constructor




and here is my query class



Java Code:






public class TourneyQueries {

private static final String URL = "jdbc:mysql://localhost/teams";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";

private Connection connection = null;
private PreparedStatement selectAllTeams = null;
private PreparedStatement selectPlayersByTeam = null;

public TourneyQueries(){

try{

connection =
DriverManager.getConnection( URL, USERNAME, PASSWORD );

// create a query that selects all clubs from the teams db
selectAllTeams =
connection.prepareStatement( "SELECT * FROM clubs" );

// create a query that selects players from their clubs
selectPlayersByTeam =
connection.prepareStatement( "SELECT * FROM players ");


} // end of try block
catch( SQLException sqle ){

sqle.printStackTrace();
System.exit( 1 );

} // end of catch block
} // end of TourneyQueries constructor

public List < Club > getAllClubs(){

List < Club > results = null;
ResultSet resultSet = null;

try{

resultSet = selectAllTeams.executeQuery();
results = new ArrayList < Club >();

while( resultSet.next() ){

results.add( new Club(
resultSet.getString( "Name" )));

} // end while loop
} // end of try block
catch( SQLException sqle ){

sqle.printStackTrace();;

} // end of catch block
finally{

try{

resultSet.close();

} // end try block
catch( SQLException sqle ){

sqle.printStackTrace();
close();

} // end catch block
} // end of finally block

return results;

} // end of getAllClubs method

public void close(){

try{

connection.close();

} // end of try block
catch( SQLException sqle ){

sqle.printStackTrace();

} // end of catch block
} // end of close method


} // end of TourneyQueries class




and finally some of my GUI class



Java Code:






public class TourneySetUpDisplay extends JFrame {

private JPanel centerPanel;
private JList listOfTeams;
private JScrollPane teamPane;
private JScrollBar teamsScrollBar;
private JLabel participantsLabel;
private JTextField participantsField;
private JRadioButton homeAndAwayRadio;
private ButtonGroup radioGroup;
private JButton getFixturesButton;
private TourneyQueries query;


public TourneySetUpDisplay(){
super( "Tournament" );

query = new TourneyQueries();

centerPanel = new JPanel();
centerPanel.setLayout( null );

listOfTeams = new JList( query.getAllClubs().toArray() );
teamPane = new JScrollPane( listOfTeams );
teamPane.setBounds( 20, 20, 180, 60 );
centerPanel.add( teamPane, BorderLayout.NORTH );



add( centerPanel );




} // emd of TourneySetUpDisplay constructor
} // end of TourneySetUpDisplay class




any help on this matter would be appreciated :)




No comments:

Post a Comment