SqlUtils.java 4.0 KB

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