ITeye博客

初涉设计模式小结

cui09 · 2007-04-18 17:46 · Markdown

singleton(单态模式)主要作用是保证在java 应用程序中。一个类Class只有一个实例存在.

在实际操作中。建立目录。数据库连接都需要这样的单线程操作。

public class Singleton {

  private Singleton(){}

  //在自己内部定义自己一个实例,是不是很奇怪?
  //注意这是private 只供内部调用

  private static Singleton instance = new Singleton();

  //这里提供了一个供外部访问本class的静态方法,可以直接访问  
  public static Singleton getInstance() {
    return instance;   
   }
}

java 代码

  1. package com.skywing.utils.db;
  2. public class DbconnManager {
  3. private static final Logger logger = Logger.getLogger(DbconnManager.class);
  4. private static final String DataSourceName=Global.getDataSource();
  5. private javax.sql.DataSource ds = null;
  6. protected DbconnManager() {
  7. try {
  8. //initDs();
  9. }
  10. catch (Exception e) {
  11. System.err.println("your data base config may be error ,please ceck it .");
  12. e.printStackTrace();
  13. }
  14. }
  15. private synchronized void initDs() throws javax.naming.NamingException {
  16. if (ds == null) {
  17. Object dsLookup = new javax.naming.InitialContext().lookup(DataSourceName);
  18. ds = (DataSource) dsLookup;
  19. }
  20. }
  21. public static DbconnManager getInstance() {
  22. if (instance == null) {
  23. synchronized (com.skywing.utils.db.DbconnManager.class) {
  24. if (instance == null) {
  25. instance = new com.skywing.utils.db.DbconnManager();
  26. }
  27. }
  28. }
  29. return instance;
  30. }
  31. public Connection getConnection() throws java.sql.SQLException {
  32. return getConn();
  33. /*
  34. try {
  35. if (ds == null) {
  36. initDs();
  37. }
  38. return ds.getConnection();
  39. }
  40. catch (SQLException sqle) {
  41. throw sqle;
  42. }
  43. catch (Exception otherE) {
  44. throw new java.sql.SQLException("error geting database connection:" +
  45. otherE);
  46. }
  47. */
  48. }
  49. /**
  50. * 关闭连接语句
  51. * @return Connection
  52. */
  53. public static void closePreparedStatement(PreparedStatement pstmt) {
  54. try {
  55. if (pstmt != null) {
  56. pstmt.close();
  57. }
  58. }
  59. catch (SQLException e) {
  60. System.out.println(e);
  61. }
  62. }
  63. /**
  64. * 关闭连接语句
  65. * @return Connection
  66. */
  67. public static void closeStatement(Statement stmt) {
  68. try {
  69. if (stmt != null) {
  70. stmt.close();
  71. }
  72. }
  73. catch (SQLException e) {
  74. System.out.println(e);
  75. }
  76. }
  77. /**
  78. * 关闭查询结果集
  79. * @return Connection
  80. */
  81. public static void closeResultSet(ResultSet rst) {
  82. try {
  83. if (rst != null) {
  84. rst.close();
  85. }
  86. }
  87. catch (SQLException e) {
  88. System.out.println(e);
  89. }
  90. }
  91. /**
  92. * 关闭所有对象(普通的数据库连接)
  93. * @return Connection
  94. */
  95. public static void closeResource(Connection conn,PreparedStatement pstmt,ResultSet rst) {
  96. closeResultSet(rst);
  97. closePreparedStatement(pstmt);
  98. closeConnection(conn);
  99. }
  100. /**
  101. * 关闭所有对象(普通的数据库连接)
  102. * @return Connection
  103. */
  104. public static void closeResource(Connection conn,Statement stmt,ResultSet rst) {
  105. closeResultSet(rst);
  106. closeStatement(stmt);
  107. closeConnection(conn);
  108. }
  109. /**
  110. * 关闭所有对象(普通的数据库连接)
  111. * @return Connection
  112. */
  113. public static void closeResource(Connection conn,PreparedStatement pstmt) {
  114. closePreparedStatement(pstmt);
  115. closeConnection(conn);
  116. }
  117. final static String sDBDriver =Global.getJdbcDriver();
  118. final static String sConnStr = Global.getJdbcUrl();
  119. /**
  120. * 通过thin方式获得Oracle数据库的连接.
  121. */
  122. private Connection getConn() {
  123. Connection conn = null;
  124. try {
  125. Class.forName(sDBDriver);
  126. conn = DriverManager.getConnection(sConnStr, Global.getJdbcUser(), Global.getJdbcPassword());
  127. }
  128. catch (Exception e) {
  129. e.printStackTrace();
  130. }
  131. return conn;
  132. }
  133. private static DbconnManager instance = null;
  134. public static void main(String args[]) throws SQLException {
  135. Connection conn = DbconnManager.getInstance().getConnection();
  136. }
  137. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。