SqlUtils.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 net.sf.l2j.gameserver.lib;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.util.logging.Logger;
  19. import net.sf.l2j.L2DatabaseFactory;
  20. public class SqlUtils
  21. {
  22. private static Logger _log = Logger.getLogger(SqlUtils.class.getName());
  23. // =========================================================
  24. // Data Field
  25. private static SqlUtils _instance;
  26. // =========================================================
  27. // Property - Public
  28. public static SqlUtils getInstance()
  29. {
  30. if (_instance == null) _instance = new SqlUtils();
  31. return _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. PreparedStatement statement = null;
  40. ResultSet rset = null;
  41. try
  42. {
  43. query = L2DatabaseFactory.getInstance().prepQuerySelect(new String[] {resultField}, tableName, whereClause, true);
  44. statement = L2DatabaseFactory.getInstance().getConnection().prepareStatement(query);
  45. rset = statement.executeQuery();
  46. if(rset.next()) res = rset.getInt(1);
  47. }
  48. catch(Exception e)
  49. {
  50. _log.warning("Error in query '" + query + "':"+e);
  51. e.printStackTrace();
  52. }
  53. finally
  54. {
  55. try{ rset.close(); } catch(Exception e) {}
  56. try{ statement.close(); } catch(Exception e) {}
  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. PreparedStatement statement = null;
  65. ResultSet rset = null;
  66. try
  67. {
  68. query = L2DatabaseFactory.getInstance().prepQuerySelect(new String[] {resultField}, tableName, whereClause, false);
  69. statement = L2DatabaseFactory.getInstance().getConnection().prepareStatement(query);
  70. rset = statement.executeQuery();
  71. int rows = 0;
  72. while (rset.next())
  73. rows++;
  74. if (rows == 0) return new Integer[0];
  75. res = new Integer[rows-1];
  76. rset.first();
  77. int row = 0;
  78. while (rset.next())
  79. {
  80. res[row] = rset.getInt(1);
  81. }
  82. }
  83. catch(Exception e)
  84. {
  85. _log.warning("mSGI: Error in query '" + query + "':"+e);
  86. e.printStackTrace();
  87. }
  88. finally
  89. {
  90. try{ rset.close(); } catch(Exception e) {}
  91. try{ statement.close(); } catch(Exception e) {}
  92. }
  93. return res;
  94. }
  95. public static Integer[][] get2DIntArray(String[] resultFields, String usedTables, String whereClause)
  96. {
  97. long start = System.currentTimeMillis();
  98. String query = "";
  99. PreparedStatement statement = null;
  100. ResultSet rset = null;
  101. Integer res[][] = null;
  102. try
  103. {
  104. query = L2DatabaseFactory.getInstance().prepQuerySelect(resultFields, usedTables, whereClause, false);
  105. statement = L2DatabaseFactory.getInstance().getConnection().prepareStatement(query);
  106. rset = statement.executeQuery();
  107. int rows = 0;
  108. while(rset.next())
  109. rows++;
  110. res = new Integer[rows-1][resultFields.length];
  111. rset.first();
  112. int row = 0;
  113. while(rset.next())
  114. {
  115. for(int i=0; i<resultFields.length; i++)
  116. res[row][i] = rset.getInt(i+1);
  117. row++;
  118. }
  119. }
  120. catch(Exception e)
  121. {
  122. _log.warning("Error in query '" + query + "':"+e);
  123. e.printStackTrace();
  124. }
  125. finally
  126. {
  127. try{ rset.close(); } catch(Exception e) {}
  128. try{ statement.close(); } catch(Exception e) {}
  129. }
  130. _log.fine("Get all rows in query '" + query + "' in " + (System.currentTimeMillis()-start) + "ms");
  131. return res;
  132. }
  133. }