private void loadEntries() { try { entries.clear(); Statement stmt = getConnection().createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM results"); while (rs.next()) { ResultEntry e = new ResultEntry(rs); entries.add(e); } } catch (SQLException e) { ChessCraftLogger.warning("SQL query failed: " + e.getMessage()); } }
/** * Get the number of losses for a player * * @param playerName The player to check * @return The number of games this player has lost */ public int getLosses(String playerName) { try { PreparedStatement stmtW = getConnection() .prepareStatement( "SELECT COUNT(playerWhite) FROM results WHERE " + "pgnResult = '0-1' AND playerWhite = ?"); PreparedStatement stmtB = getConnection() .prepareStatement( "SELECT COUNT(playerBlack) FROM results WHERE " + "pgnResult = '1-0' AND playerBlack = ?"); return doSearch(playerName, stmtW, stmtB); } catch (SQLException e) { ChessCraftLogger.warning("SQL query failed: " + e.getMessage()); return 0; } }
/** * Generate some random test data and put it in the results table. This is just for testing * purposes. */ public void addTestData() { final int N_PLAYERS = 10; String[] pgnResults = {"1-0", "0-1", "1/2-1/2"}; try { getConnection().setAutoCommit(false); Statement clear = getConnection().createStatement(); clear.executeUpdate( "DELETE FROM results WHERE playerWhite LIKE 'testplayer%' OR playerBlack LIKE 'testplayer%'"); Random rnd = new Random(); for (int i = 0; i < N_PLAYERS; i++) { for (int j = 0; j < N_PLAYERS; j++) { if (i == j) { continue; } String plw = "testplayer" + i; String plb = "testplayer" + j; String gn = "testgame-" + i + "-" + j; long start = System.currentTimeMillis() - 5000; long end = System.currentTimeMillis() - 4000; String pgnRes = pgnResults[rnd.nextInt(pgnResults.length)]; GameResult rt; if (pgnRes.equals("1-0") || pgnRes.equals("0-1")) { rt = GameResult.Checkmate; } else { rt = GameResult.DrawAgreed; } ResultEntry re = new ResultEntry(plw, plb, gn, start, end, pgnRes, rt); entries.add(re); re.save(getConnection()); } } getConnection().setAutoCommit(true); for (ResultViewBase view : views.values()) { view.rebuild(); } System.out.println("test data added & committed"); } catch (SQLException e) { ChessCraftLogger.warning("can't put test data into DB: " + e.getMessage()); } }