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

12 篇文章 0 订阅
5 篇文章 0 订阅

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

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;
   }

 }










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值