`
cui09
  • 浏览: 113668 次
  • 性别: Icon_minigender_1
  • 来自: 吉林
最近访客 更多访客>>
社区版块
存档分类
最新评论

初涉设计模式小结

阅读更多

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

相关推荐

Global site tag (gtag.js) - Google Analytics