SqlUtils.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.Level;
  20. import java.util.logging.Logger;
  21. import com.l2jserver.L2DatabaseFactory;
  22. public class SqlUtils
  23. {
  24. private static Logger _log = Logger.getLogger(SqlUtils.class.getName());
  25. private SqlUtils()
  26. {
  27. }
  28. // =========================================================
  29. // Property - Public
  30. public static SqlUtils getInstance()
  31. {
  32. return SingletonHolder._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. Connection con = null;
  41. try
  42. {
  43. con = L2DatabaseFactory.getInstance().getConnection();
  44. query = L2DatabaseFactory.getInstance().prepQuerySelect(new String[] { resultField }, tableName, whereClause, true);
  45. PreparedStatement statement = con.prepareStatement(query);
  46. ResultSet rset = statement.executeQuery();
  47. if (rset.next())
  48. res = rset.getInt(1);
  49. rset.close();
  50. statement.close();
  51. }
  52. catch (Exception e)
  53. {
  54. _log.log(Level.WARNING, "Error in query '" + query + "':", e);
  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.log(Level.WARNING, "mSGI: Error in query '" + query + "':", e);
  97. }
  98. finally
  99. {
  100. try
  101. {
  102. con.close();
  103. }
  104. catch (Exception e)
  105. {
  106. }
  107. }
  108. return res;
  109. }
  110. public static Integer[][] get2DIntArray(String[] resultFields, String usedTables, String whereClause)
  111. {
  112. long start = System.currentTimeMillis();
  113. String query = "";
  114. Connection con = null;
  115. Integer res[][] = null;
  116. try
  117. {
  118. con = L2DatabaseFactory.getInstance().getConnection();
  119. query = L2DatabaseFactory.getInstance().prepQuerySelect(resultFields, usedTables, whereClause, false);
  120. PreparedStatement statement = con.prepareStatement(query);
  121. ResultSet rset = statement.executeQuery();
  122. int rows = 0;
  123. while (rset.next())
  124. rows++;
  125. res = new Integer[rows - 1][resultFields.length];
  126. rset.first();
  127. int row = 0;
  128. while (rset.next())
  129. {
  130. for (int i = 0; i < resultFields.length; i++)
  131. res[row][i] = rset.getInt(i + 1);
  132. row++;
  133. }
  134. rset.close();
  135. statement.close();
  136. }
  137. catch (Exception e)
  138. {
  139. _log.log(Level.WARNING, "Error in query '" + query + "':", e);
  140. }
  141. finally
  142. {
  143. try
  144. {
  145. con.close();
  146. }
  147. catch (Exception e)
  148. {
  149. }
  150. }
  151. _log.fine("Get all rows in query '" + query + "' in " + (System.currentTimeMillis() - start) + "ms");
  152. return res;
  153. }
  154. @SuppressWarnings("synthetic-access")
  155. private static class SingletonHolder
  156. {
  157. protected static final SqlUtils _instance = new SqlUtils();
  158. }
  159. }