1

如何优化 JAVA 应用程序(用 Netbeans 编写)和我的 MYSQL 数据库(托管在外部数据库提供商)之间的速度?

假设我有以下代码;

private void fillPlayersList()
{
    try
    {
        PlayerListModel.clear();
        Connection userConn = SimpleDataSource.getConnection();
        Statement userStat = userConn.createStatement();
        String userquery = "SELECT * FROM player";
        ResultSet userResult = userStat.executeQuery(userquery);
        if(userResult.next())
        {
            PlayerList.setEnabled(true);
            userResult.previous();
            while (userResult.next()) {
                int id = userResult.getInt("player_id");
                String username = userResult.getString("username");
                String ip = userResult.getString("ip");
                PlayerListModel.addElement(new PlayerItem(id, username, ip));
            }
        }
        userConn.close();
        PlayerList.setModel(PlayerListModel);
    }
    catch(SQLException e)
    {
        System.out.println(e);
    }

} 

这需要aaaggeeesss才能完成!如何优化这样的代码?

谢谢!

4

1 回答 1

0

您可以从只获取您需要的字段开始

SELECT player_id, username, ip FROM player

并且可能添加一个 where 条件也会很好,你真的需要所有玩家吗?

如果你也禁用光标,并继续做你正在做的事情......

    Statement userStat = userConn.createStatement();
    userStat.setFetchSize(0);
    String userquery = "SELECT player_id, username, ip  FROM player";
    ResultSet userResult = userStat.executeQuery(userquery);
于 2013-07-15T18:07:29.113 回答