Hey new to JDBC SQL DERBY and Java so I don't know why but my Jtable only shows the columns.
Here are my codes for my LeaderboardDAO class.

public class LeaderboardDAO extends Dao {
private static final Logger LOG = LogManager.getLogger(PlayerDAO.class);
public static final String TABLE_NAME = "leaderboard";
private static LeaderboardDAO leaderboardDao;

private LeaderboardDAO() {
    super(TABLE_NAME);
}

public static LeaderboardDAO getLeaderboardDao() {
    if (leaderboardDao == null) {
        leaderboardDao = new LeaderboardDAO();
    }
    return leaderboardDao;
}

@Override
public void create() throws SQLException {
    LOG.debug("Creating database table " + TABLE_NAME);
    final String createStatement = "create table leaderboard(wins INTEGER(10), losses INTEGER(10), game_name VARCHAR(64), gamer_tag VARCHAR(64), platform VARCHAR(64))";
    super.create(createStatement);
    LOG.info("Executed statement: " + createStatement);
}

/**
 * Inserts singular persona into database
 * 
 * @param player
 * @throws SQLException
 */
public void addLeaderboardItem(final Leaderboard item) throws SQLException {

    final String addStatement = String.format("insert into %s values('%d', '%d', '%s', '%s', '%s')", TABLE_NAME, item.getWins(), item.getLosses(), item.getGameName(),
            item.getGamerTag(), item.getPlatform());
    super.add(addStatement);
}

/**
 * Retrieves a list of all leaderboard rows in the database
 * 
 * @return List<Leaderboard> rows
 * @throws SQLException
 * @throws Exception
 */
public List<Leaderboard> getLeaderboardRows(final String order, final boolean desc) throws SQLException, Exception {
    Connection connection;
    Statement statement = null;
    final List<Leaderboard> leaderboardRows = new ArrayList<Leaderboard>();
    String sqlString;
    try {
        if (order == null) {
            sqlString = String.format("SELECT * FROM " + TABLE_NAME);
        } else {

            if (order.equals("byGame")) {
                if (MainFrame.filterGamertag.equals("")) {
                    sqlString = String.format("SELECT * FROM " + TABLE_NAME + " ORDER BY game_name");
                } else {
                    sqlString = String.format("SELECT * FROM " + TABLE_NAME + " WHERE gamer_tag = '" + MainFrame.filterGamertag + "' ORDER BY game_name");
                }
            } else {
                if (MainFrame.filterGamertag.equals("")) {
                    sqlString = String.format("SELECT * FROM " + TABLE_NAME + " ORDER BY (wins||losses)");
                } else {
                    sqlString = String.format("SELECT * FROM " + TABLE_NAME + " WHERE gamer_tag= '" + MainFrame.filterGamertag + "' ORDER BY (wins||losses)");
                }
            }
            if (desc) {
                sqlString += " DESC";
            }
        }
        connection = database.connect();
        statement = connection.createStatement();
        final ResultSet resultSet = statement.executeQuery(sqlString);

        while (resultSet.next()) {
            leaderboardRows.add(new Leaderboard(resultSet.getInt("wins"), resultSet.getInt("losses"), resultSet.getString("game_name"), resultSet.getString("gamer_tag"),
                    resultSet.getString("platform")));
        }
    } finally {
        close(statement);
    }
    return leaderboardRows;
}

}

Here is my other code for LeaderboardDialog class

@SuppressWarnings("serial")
 public class LeaderboardDialog extends JDialog {

private static LeaderboardDAO dao;

private final JPanel contentPanel = new JPanel();
private final JTable table;
private final int numberOfColumns = 4;
private static final Logger LOG = LogManager.getLogger(LeaderboardDialog.class);

/**
 * Launch the application.
 */
public static void main(final String[] args) {
    try {
        Database.getDatabaseInstance();
        dao = LeaderboardDAO.getLeaderboardDao();
        final List<Leaderboard> rows = dao.getLeaderboardRows("byGame", false);
        final LeaderboardDialog dialog = new LeaderboardDialog(rows);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    } catch (final Exception e) {
        e.printStackTrace();
    }
}

/**
 * Create the dialog.
 */
public LeaderboardDialog(final List<Leaderboard> rows) {
    LOG.info("Creating leaderboard dialog.");
    Database.getDatabaseInstance();
    dao = LeaderboardDAO.getLeaderboardDao();
    this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    this.setVisible(true);
    this.setBounds(100, 100, 400, 400);
    this.setLayout(new FlowLayout());

    final String[] columnNames = { "Wins:Losses", "Game Name", "Gamer Tag", "Platform" };
    final String[][] data = new String[rows.size()][numberOfColumns];
    for (int i = 0; i < rows.size(); i++) {
        data[i] = new String[numberOfColumns];
        for (int j = 0; j < numberOfColumns; j++) {
            data[i][0] = rows.get(i).getWins() + ":" + rows.get(i).getLosses();
            data[i][1] = rows.get(i).getGameName();
            data[i][2] = rows.get(i).getGamerTag();
            data[i][3] = rows.get(i).getPlatform();

        }
    }
    table = new JTable(data, columnNames);
    contentPanel.add(new JScrollPane(table));
    this.add(contentPanel);
    this.pack();
}
}

what it is doing is just showing the columns only, have been trying to figure it out but have had no luck have also looked around in forums no luck as well.
Any help would be greatly appreciated because I've been stuck on it for the past few days maybe I am missing something or overlooked something.
If more code is need please let me know thank you.
Thanks in advance.

Recommended Answers

All 4 Replies

I suppose you have printed the size of the rows list to see whether the problem is in the SQL or the GUI?

JamesCherrill (operating under new ID while Dani's new code is finalised)

Hey yeah I did try that last night and looks like the row.size() or the rows is empty, have been trying to figure out why it is empty but no luck thus far. Any suggestion? That would be great!! Again thank you for your assistance on this matter, as I am quite new to derby sql swing and java.

Try printing sqlString to check its syntax. Run that exact string at an sql prompt and see what it does.

Hey thanks everyone I manage to figure it out. I appreciate the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.