}
}
private static void DeleteAccount(String account) throws SQLException
{
ThreadConnection con = null;
FiltredPreparedStatement statement = null;
FiltredPreparedStatement subStatement = null;
ResultSet rset = null, subRset = null;
try
{
con = L2DatabaseFactory.getInstance().getConnection();
// Check Account Exist
statement = con.prepareStatement("SELECT COUNT(*) FROM accounts WHERE login=?;");
statement.setString(1, account);
rset = statement.executeQuery();
if(rset.getInt(1) == 0)
{
System.out.println("Account \"" + account + "\" Not Exist\n");
return;
}
// Account exist
DatabaseUtils.closeDatabaseSR(statement, rset);
// Get Accounts ID
statement = con.prepareStatement("SELECT obj_Id, char_name, clanid FROM characters WHERE account_name=?;");
statement.setEscapeProcessing(true);
statement.setString(1, account);
rset = statement.executeQuery();
while(rset.next())
{
System.out.println("Deleting character \"" + rset.getString("char_name") + "\"\n");
// Check If clan leader Remove Clan and remove all from it
subStatement = con.prepareStatement("SELECT COUNT(*) FROM clan_data WHERE leader_id=?;");
subStatement.setString(1, rset.getString("clanid"));
subRset = subStatement.executeQuery();
boolean isClanLeader = false;
if(subRset.next() && subRset.getInt(1) > 0)
{
isClanLeader = true;
}
DatabaseUtils.closeDatabaseSR(subStatement, subRset);
if(isClanLeader)
{
// Clan Leader
// Get Clan Name
subStatement = con.prepareStatement("SELECT clan_name FROM clan_data WHERE leader_id=?;");
subStatement.setString(1, rset.getString("clanid"));
subRset = subStatement.executeQuery();
String clanName = null;
if(subRset.next())
{
clanName = subRset.getString("clan_name");
}
System.out.println("Deleting clan \"" + clanName + "\"\n");
DatabaseUtils.closeDatabaseSR(subStatement, subRset);
// Delete Clan Wars
subStatement = con.prepareStatement("DELETE FROM clan_wars WHERE clan1=? OR clan2=?;");
subStatement.setEscapeProcessing(true);
subStatement.setString(1, clanName);
subStatement.setString(2, clanName);
subStatement.executeUpdate();
DatabaseUtils.closeStatement(subStatement);
// Remove All From clan
subStatement = con.prepareStatement("UPDATE characters SET clanid=0 WHERE clanid=?;");
subStatement.setString(1, rset.getString("clanid"));
subStatement.executeUpdate();
DatabaseUtils.closeStatement(subStatement);
// Delete Clan
subStatement = con.prepareStatement("DELETE FROM clan_data WHERE clan_id=?;");
subStatement.setString(1, rset.getString("clanid"));
subStatement.executeUpdate();
DatabaseUtils.closeStatement(subStatement);
}
// skills
subStatement = con.prepareStatement("DELETE FROM character_skills WHERE char_obj_id=?;");
subStatement.setString(1, rset.getString("obj_Id"));
subStatement.executeUpdate();
DatabaseUtils.closeStatement(subStatement);
// shortcuts
subStatement = con.prepareStatement("DELETE FROM character_shortcuts WHERE char_obj_id=?;");
subStatement.setString(1, rset.getString("obj_Id"));
subStatement.executeUpdate();
DatabaseUtils.closeStatement(subStatement);
// items
subStatement = con.prepareStatement("DELETE FROM items WHERE owner_id=?;");
subStatement.setString(1, rset.getString("obj_Id"));
subStatement.executeUpdate();
DatabaseUtils.closeStatement(subStatement);
// recipebook
subStatement = con.prepareStatement("DELETE FROM character_recipebook WHERE char_id=?;");
subStatement.setString(1, rset.getString("obj_Id"));
subStatement.executeUpdate();
DatabaseUtils.closeStatement(subStatement);
// quests
subStatement = con.prepareStatement("DELETE FROM character_quests WHERE char_id=?;");
subStatement.setString(1, rset.getString("obj_Id"));
subStatement.executeUpdate();
DatabaseUtils.closeStatement(subStatement);
// macroses
subStatement = con.prepareStatement("DELETE FROM character_macroses WHERE char_obj_id=?;");
subStatement.setString(1, rset.getString("obj_Id"));
subStatement.executeUpdate();
DatabaseUtils.closeStatement(subStatement);
// friends
subStatement = con.prepareStatement("DELETE FROM character_friends WHERE char_id=? or friend_id = ?;");
subStatement.setString(1, rset.getString("obj_Id"));
subStatement.setString(2, rset.getString("obj_Id"));
subStatement.executeUpdate();
DatabaseUtils.closeStatement(subStatement);
// boxaccess
subStatement = con.prepareStatement("DELETE FROM boxaccess WHERE charname=?;");
subStatement.setString(1, rset.getString("char_name"));
subStatement.executeUpdate();
DatabaseUtils.closeStatement(subStatement);
// characters
subStatement = con.prepareStatement("DELETE FROM characters WHERE obj_Id=?;");
subStatement.setString(1, rset.getString("obj_Id"));
subStatement.executeUpdate();
DatabaseUtils.closeStatement(subStatement);
}
DatabaseUtils.closeDatabaseSR(statement, rset);
// Delete Account
statement = con.prepareStatement("DELETE FROM accounts WHERE login=?;");
statement.setEscapeProcessing(true);
statement.setString(1, account);
statement.executeUpdate();
System.out.println("Account \"" + account + "\" Deleted\n\n");
}