如何优化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);
}
}
它需要aaaggeeess才完成!如何优化这样的代码?
谢谢!
您可以通过只获取所需的字段开始
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);