CharNameTable.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.datatables;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.util.logging.Logger;
  21. import net.sf.l2j.L2DatabaseFactory;
  22. /**
  23. * This class ...
  24. *
  25. * @version $Revision: 1.3.2.2.2.1 $ $Date: 2005/03/27 15:29:18 $
  26. */
  27. public class CharNameTable
  28. {
  29. private static Logger _log = Logger.getLogger(CharNameTable.class.getName());
  30. private CharNameTable()
  31. {
  32. }
  33. public static CharNameTable getInstance()
  34. {
  35. return SingletonHolder._instance;
  36. }
  37. public synchronized boolean doesCharNameExist(String name)
  38. {
  39. boolean result = true;
  40. Connection con = null;
  41. try
  42. {
  43. con = L2DatabaseFactory.getInstance().getConnection();
  44. PreparedStatement statement = con.prepareStatement("SELECT account_name FROM characters WHERE char_name=?");
  45. statement.setString(1, name);
  46. ResultSet rset = statement.executeQuery();
  47. result = rset.next();
  48. rset.close();
  49. statement.close();
  50. }
  51. catch (SQLException e)
  52. {
  53. _log.warning("could not check existing charname:" + e.getMessage());
  54. }
  55. finally
  56. {
  57. try
  58. {
  59. con.close();
  60. }
  61. catch (Exception e)
  62. {
  63. }
  64. }
  65. return result;
  66. }
  67. public int accountCharNumber(String account)
  68. {
  69. Connection con = null;
  70. int number = 0;
  71. try
  72. {
  73. con = L2DatabaseFactory.getInstance().getConnection();
  74. PreparedStatement statement = con.prepareStatement("SELECT COUNT(char_name) FROM characters WHERE account_name=?");
  75. statement.setString(1, account);
  76. ResultSet rset = statement.executeQuery();
  77. while (rset.next())
  78. {
  79. number = rset.getInt(1);
  80. }
  81. rset.close();
  82. statement.close();
  83. }
  84. catch (SQLException e)
  85. {
  86. _log.warning("could not check existing char number:" + e.getMessage());
  87. }
  88. finally
  89. {
  90. try
  91. {
  92. con.close();
  93. }
  94. catch (Exception e)
  95. {
  96. }
  97. }
  98. return number;
  99. }
  100. @SuppressWarnings("synthetic-access")
  101. private static class SingletonHolder
  102. {
  103. protected static final CharNameTable _instance = new CharNameTable();
  104. }
  105. }