2007-04-18
初涉设计模式小结
singleton(单态模式)主要作用是保证在java 应用程序中。一个类Class只有一个实例存在.
在实际操作中。建立目录。数据库连接都需要这样的单线程操作。
public class Singleton {
private Singleton(){}
//在自己内部定义自己一个实例,是不是很奇怪?
//注意这是private 只供内部调用
private static Singleton instance = new Singleton();
//这里提供了一个供外部访问本class的静态方法,可以直接访问
public static Singleton getInstance() {
return instance;
}
}
java 代码
- package com.skywing.utils.db;
- public class DbconnManager {
- private static final Logger logger = Logger.getLogger(DbconnManager.class);
- private static final String DataSourceName=Global.getDataSource();
- private javax.sql.DataSource ds = null;
- protected DbconnManager() {
- try {
- //initDs();
- }
- catch (Exception e) {
- System.err.println("your data base config may be error ,please ceck it .");
- e.printStackTrace();
- }
- }
- private synchronized void initDs() throws javax.naming.NamingException {
- if (ds == null) {
- Object dsLookup = new javax.naming.InitialContext().lookup(DataSourceName);
- ds = (DataSource) dsLookup;
- }
- }
- public static DbconnManager getInstance() {
- if (instance == null) {
- synchronized (com.skywing.utils.db.DbconnManager.class) {
- if (instance == null) {
- instance = new com.skywing.utils.db.DbconnManager();
- }
- }
- }
- return instance;
- }
- public Connection getConnection() throws java.sql.SQLException {
- return getConn();
- /*
- try {
- if (ds == null) {
- initDs();
- }
- return ds.getConnection();
- }
- catch (SQLException sqle) {
- throw sqle;
- }
- catch (Exception otherE) {
- throw new java.sql.SQLException("error geting database connection:" +
- otherE);
- }
- */
- }
- /**
- * 关闭连接语句
- * @return Connection
- */
- public static void closePreparedStatement(PreparedStatement pstmt) {
- try {
- if (pstmt != null) {
- pstmt.close();
- }
- }
- catch (SQLException e) {
- System.out.println(e);
- }
- }
- /**
- * 关闭连接语句
- * @return Connection
- */
- public static void closeStatement(Statement stmt) {
- try {
- if (stmt != null) {
- stmt.close();
- }
- }
- catch (SQLException e) {
- System.out.println(e);
- }
- }
- /**
- * 关闭查询结果集
- * @return Connection
- */
- public static void closeResultSet(ResultSet rst) {
- try {
- if (rst != null) {
- rst.close();
- }
- }
- catch (SQLException e) {
- System.out.println(e);
- }
- }
- /**
- * 关闭所有对象(普通的数据库连接)
- * @return Connection
- */
- public static void closeResource(Connection conn,PreparedStatement pstmt,ResultSet rst) {
- closeResultSet(rst);
- closePreparedStatement(pstmt);
- closeConnection(conn);
- }
- /**
- * 关闭所有对象(普通的数据库连接)
- * @return Connection
- */
- public static void closeResource(Connection conn,Statement stmt,ResultSet rst) {
- closeResultSet(rst);
- closeStatement(stmt);
- closeConnection(conn);
- }
- /**
- * 关闭所有对象(普通的数据库连接)
- * @return Connection
- */
- public static void closeResource(Connection conn,PreparedStatement pstmt) {
- closePreparedStatement(pstmt);
- closeConnection(conn);
- }
- final static String sDBDriver =Global.getJdbcDriver();
- final static String sConnStr = Global.getJdbcUrl();
- /**
- * 通过thin方式获得Oracle数据库的连接.
- */
- private Connection getConn() {
- Connection conn = null;
- try {
- Class.forName(sDBDriver);
- conn = DriverManager.getConnection(sConnStr, Global.getJdbcUser(), Global.getJdbcPassword());
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- return conn;
- }
- private static DbconnManager instance = null;
- public static void main(String args[]) throws SQLException {
- Connection conn = DbconnManager.getInstance().getConnection();
- }
- }
发表评论
- 浏览: 36219 次
- 性别:

- 来自: 吉林

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
三大工作流引擎对比(转载 ...
收集了有关范玉顺的工作流文章 http://www.agilestep.cn/t ...
-- by plutoner -
J2EE 0.4—加入批量CRUD功 ...
想请教一下这个系统的设计与实现:)
-- by stamina -
J2EE 0.4—加入批量CRUD功 ...
你好,可以加你的msn么? springadele@hotmail.com
-- by stamina -
2007年最值得期待的40部北 ...
准备全部等高清,下着看. 以前读书条件不允许,只好下载, 现在上班时间不允许,只 ...
-- by dovecat -
2007年最值得期待的40部北 ...
争取去电影院看20%,也就是8部,半价场的,35-50元一场。
-- by swflora






评论排行榜