cleanup MySQL code.

This commit is contained in:
DNx5 2015-09-15 15:40:19 +07:00
parent f975fefd4a
commit c82aaa303d

View File

@ -149,7 +149,7 @@ public class MySQL implements DataSource {
} }
private synchronized Connection getConnection() throws SQLException { private synchronized Connection getConnection() throws SQLException {
Connection con = null; Connection con;
con = ds.getConnection(); con = ds.getConnection();
return con; return con;
} }
@ -290,6 +290,7 @@ public class MySQL implements DataSource {
public synchronized boolean saveAuth(PlayerAuth auth) { public synchronized boolean saveAuth(PlayerAuth auth) {
Connection con = null; Connection con = null;
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null;
try { try {
con = getConnection(); con = getConnection();
if ((columnSalt == null || columnSalt.isEmpty()) || (auth.getSalt() == null || auth.getSalt().isEmpty())) { if ((columnSalt == null || columnSalt.isEmpty()) || (auth.getSalt() == null || auth.getSalt().isEmpty())) {
@ -322,13 +323,11 @@ public class MySQL implements DataSource {
} }
} }
if (Settings.getPasswordHash == HashAlgorithm.PHPBB) { if (Settings.getPasswordHash == HashAlgorithm.PHPBB) {
int id;
ResultSet rs = null;
PreparedStatement pst2 = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnName + "=?;"); PreparedStatement pst2 = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnName + "=?;");
pst2.setString(1, auth.getNickname()); pst2.setString(1, auth.getNickname());
rs = pst2.executeQuery(); rs = pst2.executeQuery();
if (rs.next()) { if (rs.next()) {
id = rs.getInt(columnID); int id = rs.getInt(columnID);
// Insert player in phpbb_user_group // Insert player in phpbb_user_group
pst = con.prepareStatement("INSERT INTO " + Settings.getPhpbbPrefix + "user_group (group_id, user_id, group_leader, user_pending) VALUES (?,?,?,?);"); pst = con.prepareStatement("INSERT INTO " + Settings.getPhpbbPrefix + "user_group (group_id, user_id, group_leader, user_pending) VALUES (?,?,?,?);");
pst.setInt(1, Settings.getPhpbbGroup); pst.setInt(1, Settings.getPhpbbGroup);
@ -368,16 +367,15 @@ public class MySQL implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
pst.close(); pst.close();
} }
rs.close();
pst2.close(); pst2.close();
} }
if (Settings.getPasswordHash == HashAlgorithm.WORDPRESS) { if (Settings.getPasswordHash == HashAlgorithm.WORDPRESS) {
int id;
ResultSet rs = null;
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnName + "=?;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnName + "=?;");
pst.setString(1, auth.getNickname()); pst.setString(1, auth.getNickname());
rs = pst.executeQuery(); rs = pst.executeQuery();
if (rs.next()) { if (rs.next()) {
id = rs.getInt(columnID); int id = rs.getInt(columnID);
// First Name // First Name
pst = con.prepareStatement("INSERT INTO " + Settings.getWordPressPrefix + "usermeta (user_id, meta_key, meta_value) VALUES (?,?,?);"); pst = con.prepareStatement("INSERT INTO " + Settings.getWordPressPrefix + "usermeta (user_id, meta_key, meta_value) VALUES (?,?,?);");
pst.setInt(1, id); pst.setInt(1, id);
@ -463,15 +461,14 @@ public class MySQL implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
pst.close(); pst.close();
} }
rs.close();
} }
if (Settings.getPasswordHash == HashAlgorithm.XENFORO) { if (Settings.getPasswordHash == HashAlgorithm.XENFORO) {
int id;
ResultSet rs = null;
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnName + "=?;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnName + "=?;");
pst.setString(1, auth.getNickname()); pst.setString(1, auth.getNickname());
rs = pst.executeQuery(); rs = pst.executeQuery();
if (rs.next()) { if (rs.next()) {
id = rs.getInt(columnID); int id = rs.getInt(columnID);
// Insert password in the correct table // Insert password in the correct table
pst = con.prepareStatement("INSERT INTO xf_user_authenticate (user_id, scheme_class, data) VALUES (?,?,?);"); pst = con.prepareStatement("INSERT INTO xf_user_authenticate (user_id, scheme_class, data) VALUES (?,?,?);");
pst.setInt(1, id); pst.setInt(1, id);
@ -482,13 +479,13 @@ public class MySQL implements DataSource {
pst.setBlob(3, blob); pst.setBlob(3, blob);
pst.executeUpdate(); pst.executeUpdate();
} }
if (rs != null && !rs.isClosed()) rs.close();
rs.close();
} }
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return false; return false;
} finally { } finally {
close(rs);
close(pst); close(pst);
close(con); close(con);
} }
@ -499,6 +496,7 @@ public class MySQL implements DataSource {
public synchronized boolean updatePassword(PlayerAuth auth) { public synchronized boolean updatePassword(PlayerAuth auth) {
Connection con = null; Connection con = null;
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null;
try { try {
con = getConnection(); con = getConnection();
pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnPassword + "=? WHERE LOWER(" + columnName + ")=?;"); pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnPassword + "=? WHERE LOWER(" + columnName + ")=?;");
@ -507,13 +505,11 @@ public class MySQL implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
pst.close(); pst.close();
if (Settings.getPasswordHash == HashAlgorithm.XENFORO) { if (Settings.getPasswordHash == HashAlgorithm.XENFORO) {
int id;
ResultSet rs = null;
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=?;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=?;");
pst.setString(1, auth.getNickname()); pst.setString(1, auth.getNickname());
rs = pst.executeQuery(); rs = pst.executeQuery();
if (rs.next()) { if (rs.next()) {
id = rs.getInt(columnID); int id = rs.getInt(columnID);
// Insert password in the correct table // Insert password in the correct table
pst = con.prepareStatement("UPDATE xf_user_authenticate SET data=? WHERE " + columnID + "=?;"); pst = con.prepareStatement("UPDATE xf_user_authenticate SET data=? WHERE " + columnID + "=?;");
byte[] bytes = auth.getHash().getBytes(); byte[] bytes = auth.getHash().getBytes();
@ -527,13 +523,13 @@ public class MySQL implements DataSource {
pst.setInt(2, id); pst.setInt(2, id);
pst.executeUpdate(); pst.executeUpdate();
} }
if (rs != null && !rs.isClosed()) rs.close();
rs.close();
} }
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return false; return false;
} finally { } finally {
close(rs);
close(pst); close(pst);
close(con); close(con);
} }
@ -585,7 +581,7 @@ public class MySQL implements DataSource {
Connection con = null; Connection con = null;
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs = null;
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<>();
try { try {
con = getConnection(); con = getConnection();
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLastLogin + "<?;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLastLogin + "<?;");
@ -601,7 +597,7 @@ public class MySQL implements DataSource {
return list; return list;
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return new ArrayList<String>(); return new ArrayList<>();
} finally { } finally {
close(rs); close(rs);
close(pst); close(pst);
@ -617,7 +613,7 @@ public class MySQL implements DataSource {
con = getConnection(); con = getConnection();
if (Settings.getPasswordHash == HashAlgorithm.XENFORO) { if (Settings.getPasswordHash == HashAlgorithm.XENFORO) {
int id; int id;
ResultSet rs = null; ResultSet rs;
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=?;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=?;");
pst.setString(1, user); pst.setString(1, user);
rs = pst.executeQuery(); rs = pst.executeQuery();
@ -753,37 +749,14 @@ public class MySQL implements DataSource {
@Override @Override
public synchronized void close() { public synchronized void close() {
try { if (ds != null)
if (ds != null) ds.close();
ds.close();
} catch (Exception e) {
}
} }
private void close(Statement st) { private void close(AutoCloseable o) {
if (st != null) { if (o != null) {
try { try {
st.close(); o.close();
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
}
}
}
private void close(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
}
}
}
private void close(Connection con) {
if (con != null) {
try {
con.close();
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
} }
@ -795,7 +768,7 @@ public class MySQL implements DataSource {
Connection con = null; Connection con = null;
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs = null;
List<String> countIp = new ArrayList<String>(); List<String> countIp = new ArrayList<>();
try { try {
con = getConnection(); con = getConnection();
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;");
@ -807,7 +780,7 @@ public class MySQL implements DataSource {
return countIp; return countIp;
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return new ArrayList<String>(); return new ArrayList<>();
} finally { } finally {
close(rs); close(rs);
close(pst); close(pst);
@ -820,7 +793,7 @@ public class MySQL implements DataSource {
Connection con = null; Connection con = null;
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs = null;
List<String> countIp = new ArrayList<String>(); List<String> countIp = new ArrayList<>();
try { try {
con = getConnection(); con = getConnection();
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;");
@ -832,7 +805,7 @@ public class MySQL implements DataSource {
return countIp; return countIp;
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return new ArrayList<String>(); return new ArrayList<>();
} finally { } finally {
close(rs); close(rs);
close(pst); close(pst);
@ -845,7 +818,7 @@ public class MySQL implements DataSource {
Connection con = null; Connection con = null;
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs = null;
List<String> countEmail = new ArrayList<String>(); List<String> countEmail = new ArrayList<>();
try { try {
con = getConnection(); con = getConnection();
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnEmail + "=?;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnEmail + "=?;");
@ -857,7 +830,7 @@ public class MySQL implements DataSource {
return countEmail; return countEmail;
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return new ArrayList<String>(); return new ArrayList<>();
} finally { } finally {
close(rs); close(rs);
close(pst); close(pst);
@ -924,12 +897,10 @@ public class MySQL implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return;
} finally { } finally {
close(pst); close(pst);
close(con); close(con);
} }
return;
} }
@Override @Override
@ -945,12 +916,10 @@ public class MySQL implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return;
} finally { } finally {
close(pst); close(pst);
close(con); close(con);
} }
return;
} }
@Override @Override
@ -965,12 +934,10 @@ public class MySQL implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return;
} finally { } finally {
close(pst); close(pst);
close(con); close(con);
} }
return;
} }
@Override @Override
@ -978,7 +945,7 @@ public class MySQL implements DataSource {
int result = 0; int result = 0;
Connection con = null; Connection con = null;
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs;
try { try {
con = getConnection(); con = getConnection();
pst = con.prepareStatement("SELECT COUNT(*) FROM " + tableName + ";"); pst = con.prepareStatement("SELECT COUNT(*) FROM " + tableName + ";");
@ -1008,12 +975,10 @@ public class MySQL implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return;
} finally { } finally {
close(pst); close(pst);
close(con); close(con);
} }
return;
} }
@Override @Override
@ -1042,7 +1007,7 @@ public class MySQL implements DataSource {
} }
} }
if (Settings.getPasswordHash == HashAlgorithm.XENFORO) { if (Settings.getPasswordHash == HashAlgorithm.XENFORO) {
ResultSet rsid = null; ResultSet rsid;
pst = con.prepareStatement("SELECT * FROM xf_user_authenticate WHERE " + columnID + "=?;"); pst = con.prepareStatement("SELECT * FROM xf_user_authenticate WHERE " + columnID + "=?;");
pst.setInt(1, id); pst.setInt(1, id);
rsid = pst.executeQuery(); rsid = pst.executeQuery();
@ -1068,7 +1033,7 @@ public class MySQL implements DataSource {
@Override @Override
public List<PlayerAuth> getLoggedPlayers() { public List<PlayerAuth> getLoggedPlayers() {
List<PlayerAuth> auths = new ArrayList<PlayerAuth>(); List<PlayerAuth> auths = new ArrayList<>();
Connection con = null; Connection con = null;
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs = null;
@ -1077,7 +1042,7 @@ public class MySQL implements DataSource {
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLogged + "=1;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLogged + "=1;");
rs = pst.executeQuery(); rs = pst.executeQuery();
while (rs.next()) { while (rs.next()) {
PlayerAuth pAuth = null; PlayerAuth pAuth;
int id = rs.getInt(columnID); int id = rs.getInt(columnID);
if (rs.getString(columnIp).isEmpty() && rs.getString(columnIp) != null) { if (rs.getString(columnIp).isEmpty() && rs.getString(columnIp) != null) {
pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), "192.168.0.1", rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), "192.168.0.1", rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName));
@ -1092,7 +1057,7 @@ public class MySQL implements DataSource {
} }
} }
if (Settings.getPasswordHash == HashAlgorithm.XENFORO) { if (Settings.getPasswordHash == HashAlgorithm.XENFORO) {
ResultSet rsid = null; ResultSet rsid;
pst = con.prepareStatement("SELECT * FROM xf_user_authenticate WHERE " + columnID + "=?;"); pst = con.prepareStatement("SELECT * FROM xf_user_authenticate WHERE " + columnID + "=?;");
pst.setInt(1, id); pst.setInt(1, id);
rsid = pst.executeQuery(); rsid = pst.executeQuery();
@ -1101,11 +1066,9 @@ public class MySQL implements DataSource {
byte[] bytes = blob.getBytes(1, (int) blob.length()); byte[] bytes = blob.getBytes(1, (int) blob.length());
pAuth.setHash(new String(bytes)); pAuth.setHash(new String(bytes));
} }
if (rsid != null) rsid.close();
rsid.close();
} }
if (pAuth != null) auths.add(pAuth);
auths.add(pAuth);
} }
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());