优化应用程序和dbhost之间的速度

时间:2013-07-15 17:58:31

标签: mysql optimization netbeans connection

如何优化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);
    }

} 

在此完成之前需要花费一些时间!我该如何优化这样的代码?

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以从仅提取您需要的字段

开始
SELECT player_id, username, ip FROM player

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

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

    Statement userStat = userConn.createStatement();
    userStat.setFetchSize(0);
    String userquery = "SELECT player_id, username, ip  FROM player";
    ResultSet userResult = userStat.executeQuery(userquery);