1. /**
  2. * Copyright: Copyright (c) 2005-2005
  3. * Company: JavaResearch(http://www.javaresearch.org)
  4. */
  5. package org.javaresearch.jerch;
  6. import java.sql.Connection;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ResourceBundle;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import javax.sql.DataSource;
  14. /**
  15. * 工具类。
  16. * 最后更新日期:2005年5月7日
  17. * @author cherami
  18. */
  19. public class Utils {
  20. /**
  21. * 是否是调试模式。
  22. */
  23. private static boolean debuggable = false;
  24. /**
  25. * 系统使用的国际化资源。
  26. */
  27. private static ResourceBundle resources=ResourceBundle.getBundle("org.javaresearch.jerch.jerch");
  28. /**
  29. * 日志记录。
  30. */
  31. private static final Logger logger = Logger.getLogger("jerch");
  32. private static long openConnectionCount=0;
  33. private static long closeConnectionCount=0;
  34. /**
  35. * 根据主键得到对应的国际化消息。
  36. * @param key 消息的主键
  37. * @return 国际化的消息
  38. */
  39. public static String getMessage(String key) {
  40. return resources.getString(key);
  41. }
  42. /**
  43. * 向日志中写入警告信息。
  44. * @param message 信息主键
  45. * @param th 相关的异常
  46. */
  47. public static void warn(String message, Throwable th) {
  48. logger.log(Level.WARNING, getMessage(message), th);
  49. }
  50. /**
  51. * 向日志中写入错误信息。
  52. * @param message 信息主键
  53. * @param th 相关的异常
  54. */
  55. public static void error(String message, Throwable th) {
  56. logger.log(Level.SEVERE, getMessage(message), th);
  57. }
  58. /**
  59. * 向日志中写入正常信息。
  60. * @param message 信息主键
  61. * @param th 相关的异常
  62. */
  63. public static void info(String message, Throwable th) {
  64. logger.log(Level.INFO, getMessage(message), th);
  65. }
  66. /**
  67. * 向日志中写入调试信息。
  68. * @param message 信息主键
  69. * @param th 相关的异常
  70. */
  71. public static void debug(String message, Throwable th) {
  72. if (debuggable) {
  73. if (th!=null) {
  74. logger.log(Level.INFO, getMessage(message),th);
  75. } else {
  76. logger.log(Level.INFO, getMessage(message));
  77. }
  78. }
  79. }
  80. /**
  81. * 设置是否处于调试状态。
  82. * @param debuggable 是否是调试状态
  83. */
  84. public static void setDebuggable(boolean debuggable) {
  85. Utils.debuggable = debuggable;
  86. }
  87. /**
  88. * 关闭JDBC的Statement并忽略任何异常。
  89. * @param stmt 要关闭的Statement
  90. */
  91. public static void closeStatement(Statement stmt) {
  92. if (stmt != null) {
  93. try {
  94. stmt.close();
  95. }
  96. catch (SQLException ex) {
  97. warn("Utils.CAN_NOT_CLOSE_STATEMENT", ex);
  98. }
  99. }
  100. }
  101. /**
  102. * 关闭JDBC的ResultSet并忽略任何异常。
  103. * @param rs 要关闭的ResultSet
  104. */
  105. public static void closeResultSet(ResultSet rs) {
  106. if (rs != null) {
  107. try {
  108. rs.close();
  109. }
  110. catch (SQLException ex) {
  111. warn("Utils.CAN_NOT_CLOSE_RESULTSET", ex);
  112. }
  113. }
  114. }
  115. /**
  116. * 取得JDBC的Connection。
  117. * @param datasource 数据源
  118. * @return JDBC的Connection
  119. */
  120. public static synchronized Connection getConnection(DataSource datasource) {
  121. try {
  122. Connection connection = datasource.getConnection();
  123. return connection;
  124. }
  125. catch (SQLException ex) {
  126. warn("Utils.CAN_NOT_GET_CONNECTION", ex);
  127. throw new JerchException("Utils.CAN_NOT_GET_CONNECTION",ex);
  128. }
  129. }
  130. /**
  131. * 关闭JDBC的Connection并忽略任何异常。
  132. * @param con 要关闭的Connection
  133. */
  134. public static void closeConnection(Connection con) {
  135. if (con != null) {
  136. try {
  137. con.close();
  138. closeConnectionCount++;
  139. }
  140. catch (SQLException ex) {
  141. warn("Utils.CAN_NOT_CLOSE_CONNECTION", ex);
  142. }
  143. }
  144. }
  145. /**
  146. * 得到Connection关闭的次数。
  147. * @return Connection关闭的次数。
  148. */
  149. public static long getCloseConnectionCount() {
  150. return closeConnectionCount;
  151. }
  152. /**
  153. * 得到Connection打开的次数。
  154. * @return Connection打开的次数。
  155. */
  156. public static long getOpenConnectionCount() {
  157. return openConnectionCount;
  158. }
  159. }