---
title: "初涉设计模式小结"
author: "cui09"
date: "2007-04-18T17:46:25Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2007-04-18T17:46:25Z"
id: 72249
url: "https://cui.iteye.com/blog/72249"
---

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

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

public class Singleton {

　　private Singleton(){}

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

　　private static Singleton instance = new Singleton();

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

java 代码

001. package com.skywing.utils.db;
002. public class DbconnManager {
003. private static final Logger logger = Logger.getLogger(DbconnManager.class);
004. private static final String DataSourceName=Global.getDataSource();
005. private javax.sql.DataSource ds = null;
006. protected DbconnManager() {
007. try {
008. //initDs();
009. }
010. catch (Exception e) {
011. System.err.println("your data base config may be error ,please ceck it .");
012. e.printStackTrace();
013. }
014. }
015. private synchronized void initDs() throws javax.naming.NamingException {
016. if (ds == null) {
017. Object dsLookup = new javax.naming.InitialContext().lookup(DataSourceName);
018. ds = (DataSource) dsLookup;
019. }
020. }
021. public static DbconnManager getInstance() {
022. if (instance == null) {
023. synchronized (com.skywing.utils.db.DbconnManager.class) {
024. if (instance == null) {
025. instance = new com.skywing.utils.db.DbconnManager();
026. }
027. }
028. }
029. return instance;
030. }
031. public Connection getConnection() throws java.sql.SQLException {
032. return getConn();
033. /*
034. try {
035. if (ds == null) {
036. initDs();
037. }
038. return ds.getConnection();
039. }
040. catch (SQLException sqle) {
041. throw sqle;
042. }
043. catch (Exception otherE) {
044. throw new java.sql.SQLException("error geting database connection:" +
045. otherE);
046. }
047. \*/
048. }
049. /\**
050. * 关闭连接语句
051. * @return Connection
052. \*/
053. public static void closePreparedStatement(PreparedStatement pstmt) {
054. try {
055. if (pstmt != null) {
056. pstmt.close();
057. }
058. }
059. catch (SQLException e) {
060. System.out.println(e);
061. }
062. }
063. /\**
064. * 关闭连接语句
065. * @return Connection
066. \*/
067. public static void closeStatement(Statement stmt) {
068. try {
069. if (stmt != null) {
070. stmt.close();
071. }
072. }
073. catch (SQLException e) {
074. System.out.println(e);
075. }
076. }
077. /\**
078. * 关闭查询结果集
079. * @return Connection
080. \*/
081. public static void closeResultSet(ResultSet rst) {
082. try {
083. if (rst != null) {
084. rst.close();
085. }
086. }
087. catch (SQLException e) {
088. System.out.println(e);
089. }
090. }
091. /\**
092. * 关闭所有对象（普通的数据库连接）
093. * @return Connection
094. \*/
095. public static void closeResource(Connection conn,PreparedStatement pstmt,ResultSet rst) {
096. closeResultSet(rst);
097. closePreparedStatement(pstmt);
098. closeConnection(conn);
099. }
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. }