SqlUtils.java 4.4 KB

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