SqlUtils.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.lib;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.util.logging.Logger;
  19. import net.sf.l2j.L2DatabaseFactory;
  20. public class SqlUtils
  21. {
  22. private static Logger _log = Logger.getLogger(SqlUtils.class.getName());
  23. // =========================================================
  24. // Data Field
  25. private static SqlUtils _instance;
  26. // =========================================================
  27. // Property - Public
  28. public static SqlUtils getInstance()
  29. {
  30. if (_instance == null)
  31. _instance = new SqlUtils();
  32. return _instance;
  33. }
  34. // =========================================================
  35. // Method - Public
  36. public static Integer getIntValue(String resultField, String tableName, String whereClause)
  37. {
  38. String query = "";
  39. Integer res = null;
  40. PreparedStatement statement = null;
  41. ResultSet rset = null;
  42. try
  43. {
  44. query = L2DatabaseFactory.getInstance().prepQuerySelect(new String[]
  45. {
  46. resultField
  47. }, tableName, whereClause, true);
  48. statement = L2DatabaseFactory.getInstance().getConnection().prepareStatement(query);
  49. rset = statement.executeQuery();
  50. if (rset.next())
  51. res = rset.getInt(1);
  52. }
  53. catch (Exception e)
  54. {
  55. _log.warning("Error in query '" + query + "':" + e);
  56. e.printStackTrace();
  57. }
  58. finally
  59. {
  60. try
  61. {
  62. rset.close();
  63. }
  64. catch (Exception e)
  65. {
  66. }
  67. try
  68. {
  69. statement.close();
  70. }
  71. catch (Exception e)
  72. {
  73. }
  74. }
  75. return res;
  76. }
  77. public static Integer[] getIntArray(String resultField, String tableName, String whereClause)
  78. {
  79. String query = "";
  80. Integer[] res = null;
  81. PreparedStatement statement = null;
  82. ResultSet rset = null;
  83. try
  84. {
  85. query = L2DatabaseFactory.getInstance().prepQuerySelect(new String[]
  86. {
  87. resultField
  88. }, tableName, whereClause, false);
  89. statement = L2DatabaseFactory.getInstance().getConnection().prepareStatement(query);
  90. rset = statement.executeQuery();
  91. int rows = 0;
  92. while (rset.next())
  93. rows++;
  94. if (rows == 0)
  95. return new Integer[0];
  96. res = new Integer[rows - 1];
  97. rset.first();
  98. int row = 0;
  99. while (rset.next())
  100. {
  101. res[row] = rset.getInt(1);
  102. }
  103. }
  104. catch (Exception e)
  105. {
  106. _log.warning("mSGI: Error in query '" + query + "':" + e);
  107. e.printStackTrace();
  108. }
  109. finally
  110. {
  111. try
  112. {
  113. rset.close();
  114. }
  115. catch (Exception e)
  116. {
  117. }
  118. try
  119. {
  120. statement.close();
  121. }
  122. catch (Exception e)
  123. {
  124. }
  125. }
  126. return res;
  127. }
  128. public static Integer[][] get2DIntArray(String[] resultFields, String usedTables, String whereClause)
  129. {
  130. long start = System.currentTimeMillis();
  131. String query = "";
  132. PreparedStatement statement = null;
  133. ResultSet rset = null;
  134. Integer res[][] = null;
  135. try
  136. {
  137. query = L2DatabaseFactory.getInstance().prepQuerySelect(resultFields, usedTables, whereClause, false);
  138. statement = L2DatabaseFactory.getInstance().getConnection().prepareStatement(query);
  139. rset = statement.executeQuery();
  140. int rows = 0;
  141. while (rset.next())
  142. rows++;
  143. res = new Integer[rows - 1][resultFields.length];
  144. rset.first();
  145. int row = 0;
  146. while (rset.next())
  147. {
  148. for (int i = 0; i < resultFields.length; i++)
  149. res[row][i] = rset.getInt(i + 1);
  150. row++;
  151. }
  152. }
  153. catch (Exception e)
  154. {
  155. _log.warning("Error in query '" + query + "':" + e);
  156. e.printStackTrace();
  157. }
  158. finally
  159. {
  160. try
  161. {
  162. rset.close();
  163. }
  164. catch (Exception e)
  165. {
  166. }
  167. try
  168. {
  169. statement.close();
  170. }
  171. catch (Exception e)
  172. {
  173. }
  174. }
  175. _log.fine("Get all rows in query '" + query + "' in " + (System.currentTimeMillis() - start) + "ms");
  176. return res;
  177. }
  178. }