Skip to content

Commit 1373986

Browse files
committed
[Fix] ResultSet close timing to get userdata
1 parent 7437d7a commit 1373986

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

src/main/java/jp/azisaba/lgw/kdstatus/sql/KillDeathDataContainer.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -97,30 +97,7 @@ public KDUserData loadPlayerData(@NonNull Player p) {
9797
e.printStackTrace();
9898
}
9999
}else{
100-
101-
ResultSet set = KDStatusReloaded.getPlugin().getKDData().getRawData(p.getUniqueId());
102-
103-
try {
104-
if ( set.next() ) {
105-
int totalKills = set.getInt("kills");
106-
int deaths = set.getInt("deaths");
107-
int dailyKills = set.getInt("daily_kills");
108-
int monthlyKills = set.getInt("monthly_kills");
109-
int yearlyKills = set.getInt("yearly_kills");
110-
long lastUpdated = set.getLong("last_updated");
111-
112-
data = new KDUserData(p.getUniqueId(), p.getName(), totalKills, deaths, dailyKills, monthlyKills, yearlyKills, lastUpdated);
113-
} else {
114-
data = new KDUserData(p.getUniqueId(), p.getName(), 0, 0, 0, 0, 0, -1);
115-
KDStatusReloaded.getPlugin().getKDData().create(data);
116-
}
117-
118-
set.close();
119-
120-
} catch ( Exception e ) {
121-
e.printStackTrace();
122-
}
123-
100+
data = KDStatusReloaded.getPlugin().getKDData().getUserData(p.getUniqueId(), p.getName());
124101
}
125102

126103
// データがnullの場合はnullを返す

src/main/java/jp/azisaba/lgw/kdstatus/sql/PlayerDataMySQLController.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,38 @@ public ResultSet getRawData(@NonNull UUID uuid) {
214214
return null;
215215
}
216216

217+
/**
218+
* @param uuid UUID of target player
219+
* @param name Name of target player
220+
* @return returns userdata. If failed, returns null.
221+
*/
222+
public KDUserData getUserData(@NonNull UUID uuid, @NonNull String name) {
223+
try(Connection conn = plugin.sql.getConnection();
224+
PreparedStatement ps = conn.prepareStatement("SELECT * FROM kill_death_data WHERE uuid=?")) {
225+
ps.setString(1, uuid.toString());
226+
ResultSet rs = ps.executeQuery();
227+
if(rs.next()) {
228+
int totalKills = rs.getInt("kills");
229+
int deaths = rs.getInt("deaths");
230+
int dailyKills = rs.getInt("daily_kills");
231+
int monthlyKills = rs.getInt("monthly_kills");
232+
int yearlyKills = rs.getInt("yearly_kills");
233+
long lastUpdated = rs.getLong("last_updated");
234+
rs.close();
235+
236+
return new KDUserData(uuid, name, totalKills, deaths, dailyKills, monthlyKills, yearlyKills, lastUpdated);
237+
} else {
238+
rs.close();
239+
KDUserData data = new KDUserData(uuid, name, 0, 0, 0, 0, 0, -1);
240+
KDStatusReloaded.getPlugin().getKDData().create(data);
241+
return data;
242+
}
243+
} catch (SQLException throwables) {
244+
throwables.printStackTrace();
245+
}
246+
return null;
247+
}
248+
217249
public static final String RANK_QUERY = "SELECT * FROM (SELECT uuid, ?, last_updated, RANK() over (ORDER BY ? DESC) as 'rank' FROM kill_death_data WHERE last_updated > ?) s WHERE s.uuid=?";
218250

219251
public int getRank(UUID uuid,TimeUnit unit){

0 commit comments

Comments
 (0)