SqlUtils.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.util.lib;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import com.l2jserver.L2DatabaseFactory;
  26. public class SqlUtils
  27. {
  28. private static Logger _log = Logger.getLogger(SqlUtils.class.getName());
  29. public static SqlUtils getInstance()
  30. {
  31. return SingletonHolder._instance;
  32. }
  33. public static Integer getIntValue(String resultField, String tableName, String whereClause)
  34. {
  35. String query = "";
  36. Integer res = null;
  37. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  38. {
  39. query = L2DatabaseFactory.getInstance().prepQuerySelect(new String[]
  40. {
  41. resultField
  42. }, tableName, whereClause, true);
  43. try (PreparedStatement ps = con.prepareStatement(query);
  44. ResultSet rs = ps.executeQuery())
  45. {
  46. if (rs.next())
  47. {
  48. res = rs.getInt(1);
  49. }
  50. }
  51. }
  52. catch (Exception e)
  53. {
  54. _log.log(Level.WARNING, "Error in query '" + query + "':", e);
  55. }
  56. return res;
  57. }
  58. public static Integer[] getIntArray(String resultField, String tableName, String whereClause)
  59. {
  60. String query = "";
  61. Integer[] res = null;
  62. try
  63. {
  64. query = L2DatabaseFactory.getInstance().prepQuerySelect(new String[]
  65. {
  66. resultField
  67. }, tableName, whereClause, false);
  68. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  69. PreparedStatement ps = con.prepareStatement(query);
  70. ResultSet rs = ps.executeQuery())
  71. {
  72. int rows = 0;
  73. while (rs.next())
  74. {
  75. rows++;
  76. }
  77. if (rows == 0)
  78. {
  79. return new Integer[0];
  80. }
  81. res = new Integer[rows - 1];
  82. rs.first();
  83. int row = 0;
  84. while (rs.next())
  85. {
  86. res[row] = rs.getInt(1);
  87. }
  88. }
  89. }
  90. catch (Exception e)
  91. {
  92. _log.log(Level.WARNING, "mSGI: Error in query '" + query + "':", e);
  93. }
  94. return res;
  95. }
  96. public static Integer[][] get2DIntArray(String[] resultFields, String usedTables, String whereClause)
  97. {
  98. long start = System.currentTimeMillis();
  99. String query = "";
  100. Integer res[][] = null;
  101. try
  102. {
  103. query = L2DatabaseFactory.getInstance().prepQuerySelect(resultFields, usedTables, whereClause, false);
  104. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  105. PreparedStatement ps = con.prepareStatement(query);
  106. ResultSet rs = ps.executeQuery())
  107. {
  108. int rows = 0;
  109. while (rs.next())
  110. {
  111. rows++;
  112. }
  113. res = new Integer[rows - 1][resultFields.length];
  114. rs.first();
  115. int row = 0;
  116. while (rs.next())
  117. {
  118. for (int i = 0; i < resultFields.length; i++)
  119. {
  120. res[row][i] = rs.getInt(i + 1);
  121. }
  122. row++;
  123. }
  124. }
  125. }
  126. catch (Exception e)
  127. {
  128. _log.log(Level.WARNING, "Error in query '" + query + "':", e);
  129. }
  130. _log.fine("Get all rows in query '" + query + "' in " + (System.currentTimeMillis() - start) + "ms");
  131. return res;
  132. }
  133. private static class SingletonHolder
  134. {
  135. protected static final SqlUtils _instance = new SqlUtils();
  136. }
  137. }