ITeye博客

Singleton单例模式

crackit · 2011-05-12 22:28 · java学习笔记 · 企业架构 · Markdown

Singleton单例模式 -- java模式

  1. 单例模式

 ConnectionPool.java

package com.mark.util;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * Singleton get database connection 
 *
 */
public class ConnectionPool {
	private static ConnectionPool connectionPool = null;
	private static Connection connection = null;
	
	/*
	 * create ConnectionPool
	 */
	private ConnectionPool() {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * get ConnectionPool
	 */
	public static ConnectionPool getInstance() {
		if(connectionPool == null) {
			connectionPool = new ConnectionPool();
		}
		return connectionPool;
	}
	
	/*
	 * get connection
	 */
	public Connection getConnection() {
		if(connection != null) {
			return connection;
		}
		return null;
	}
}

评论(0)

暂无评论。

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