ORM框架分析之DB到Entity的映射(三)

来源:互联网 发布:淘宝同款排除原理 编辑:程序博客网 时间:2024/06/04 17:51

//根据配置信息,维持连接对象的管理(增加连接外对象)

public class DBManager{

      //连接池对象

     private static DBConnPool pool;

     private static Logger logger = Logger.getLogger(DBManager.class);

     //配置信息

     private static Configuration conf;

     static {

        logger.info("开始加载数据库资源....................");

        Properties pros = new Properties();

        pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));

        conf = new Configuration();

        conf.setDriver(pros.getProperty("driver"));
        conf.setPoPackage(pros.getProperty("poPackage"));
        conf.setPwd(pros.getProperty("pwd"));
        conf.setSrcPath(pros.getProperty("srcPath"));
        conf.setUrl(pros.getProperty("url"));
        conf.setUser(pros.getProperty("user"));
        conf.setUserDB(pros.getProperty("userDB"));
        conf.setQueryClass(pros.getProperty("queryClass"));
        conf.setPoolMaxSize(Integer.parseInt(pros.getProperty("poolMaxSize")));
        conf.setPoolMinSize(Integer.parseInt(pros.getProperty("poolMinSize")));

     }

     //获得Connection对象

     public static Connection getConn(){

         if(pool == null){

             pool = new DBConnPool();

         }

         return pool.getConnection();

     }

     //创建新的Connection对象

     public static Connection createConn(){

           Class.forName(conf.getDriver());

           return DriverManager.getConnection(conf.getUrl(),conf.getUser(),conf.getPwd());

     }

     //关闭传入的ResultSet、PreparedStatement、Connection对象

     public static void close(ResultSet rs,Statement ps,Connection conn){

        if(rs!=null){

             rs.close();

        }

        if(ps!=null){

             ps.close();

        }

        if(conn!=null){

             conn.close();

        }

     }

      /**
    * 关闭传入的PreparedStatement、Connection对象   
    * @param ps
    * @param conn
    */
   public static void close(Statement ps,Connection conn){
       try {
        if(ps!=null){
               ps.close();
           }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        pool.close(conn);
   }

   /**
    * 返回Configuration对象
    * @return
    */
   public static Configuration getConf(){
       return conf;
   }

 }










0 0
原创粉丝点击