SqlUtils.java 4.1 KB

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