我正在创建一个用于投注程序的Elo系统,以比较两个竞争对手并确定下注金额。我已经收集了一些超时数据,需要将所有这些数据进行计算以收集Elo分数。

我大约有1150条记录可以做到这一点。我最初的方法是查询所有记录,然后使用while(rs.next())将每个记录放入计算中,然后通过另一个查询用新值更新表。但是程序挂在我的更新语句上。它的挂起位置在下面的代码中带有注释“ // HANGS RIGHT HERE”。有趣的是,它不会给出任何错误或异常。它只是坐在那里,不会有任何进一步的进展。我已经尝试了几种解决方案,但无法提出/研究解决方案。下面的代码完整地表示了该程序:

package elomaker;

import java.awt.Component;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;

public class EloMaker {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws SQLException {

    //initial variables
    Connection con;
    Statement stmt = null;
    Component frame = null;
    String sqlPullFights = "select  f.fid,\n"
            + "        f.p1id,\n"
            + "        f.p2id,\n"
            + "        f.winner,\n"
            + "        (select p.elo from players p where f.p1id = p.pid) p1elo,\n"
            + "        (select pl.elo from players pl where f.p2id = pl.pid) p2elo\n"
            + "from    fights f";
    ResultSet rs = null;
    int x = 0;

    //initializing the database to make sure the datbase is ready.
    String dbURL = "jdbc:oracle:thin:@localhost:1521:orcl";
    String userName = "NEGGLY";
    String password = "Yellow23";
    try {
        Class.forName("oracle.jdbc.OracleDriver");
        con = DriverManager.getConnection(dbURL, userName, password);
        stmt = con.createStatement();
    } catch (ClassNotFoundException e) {
        System.out.println("Couldn't register JDBC driver,");
        System.out.println("Applicaiton Ending.");
        System.exit(-1);
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(frame, "Could not establish a connection to the database", "Database Error", +JOptionPane.ERROR_MESSAGE);
        System.exit(-1);
    }

    //pull the fight values for each player
    rs = stmt.executeQuery(sqlPullFights);
    while (rs.next()) {
        int intP1ID = rs.getInt("P1ID");
        int intP2ID = rs.getInt("P2ID");
        int intWinner = rs.getInt("WINNER");
        int intP1Elo = rs.getInt("P1ELO");
        int intP2Elo = rs.getInt("P2ELO");
        int intP1NewRating, intP2NewRating;
        double dblP1Outcome, dblP2Outcome;

        dblP1Outcome = 1 / (1 + (Math.pow(10, (intP2Elo - intP1Elo) / 400)));
        dblP2Outcome = 1 - dblP1Outcome;

        //determine the winner
        if (intP1ID == intWinner) {
            intP1NewRating = (int) (intP1Elo + 32 * (1-dblP1Outcome));
            intP2NewRating = (int) (intP2Elo + 32 * (0-dblP2Outcome));
        } else {
            intP2NewRating = (int) (intP2Elo + 32 * (1-dblP2Outcome));
            intP1NewRating = (int) (intP1Elo + 32 * (0-dblP1Outcome));
        }

        String sqlUpdP1Rate = "update players set elo = "+intP1NewRating+" where pid = " + intP1ID;
        String sqlUpdP2Rate = "update players set elo = "+intP2NewRating+" where pid = " + intP2ID;

        stmt.executeQuery(sqlUpdP1Rate); //HANGS RIGHT HERE.
        stmt.executeQuery(sqlUpdP2Rate);
        stmt.executeQuery("commit");

        x++;
        System.out.println(x);
    }

}

}


任何帮助,将不胜感激。

最佳答案

我想您这里有一个经典的僵局:


您获取了比赛列表和比赛中每个参与者的ELO评分
对于每个这些比赛

2.1计算每个参与者的结果

2.2您更新每个参与者的评分


但是Oracle在2.2中应该做什么?它是否应该自动更改您在1中获取的结果集(以及您当前正在迭代的结果集)以反映更新的评分?还是应该保留旧的评分标准,如果一名球员参加了一场以上的比赛,这将导致虚假结果?

因此,建议您将方法修改为:


将查询结果提取到数组或类似数组中
遍历此列表
保持计算+更新逻辑不变


这应该摆脱僵局(对不起,我不是JDBC专家,所以我无法为您提供实现此目标的示例)。

10-04 18:11