• properties配置文件实现DBManager


    package som.gu.core;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    import com.gu.bean.configuration;
    
    /**
     * 获得配置信息,维持连接管理
     * @author 谷
     *
     */
    public class DBManager {
    
        private static configuration conf;
        static{
            Properties pro=new Properties();
            try {
                pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             conf=new configuration();
             conf.setDriver(pro.getProperty("driver"));
             conf.setUrl(pro.getProperty("url"));
             conf.setUsingDB(pro.getProperty("usingDB"));
             conf.setAdmin(pro.getProperty("admin"));
             conf.setAd_pass(pro.getProperty("ad_pass"));
             conf.setPoPackage(pro.getProperty("poPackage"));
             conf.setSrcPath(pro.getProperty("srcPath"));
        }
        
        /**
         * 封装数据库的链接,资源关闭等方法
         */
        public static  Connection getConnect(){
            Connection con=null;
            try {
            //    Class.forName("com.mysql.jdbc.Driver");
                Class.forName(conf.getDriver());
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                con=DriverManager.getConnection(conf.getUrl(), conf.getAdmin(), conf.getAd_pass());
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return con;
        } 
    
    
    
        public static  void closeRes(ResultSet res,Statement ps,Connection con){
            
                try {
                    if(res!=null){
                        res.close();
                    }
                    if(ps!=null){
                    ps.close();
                    }
                    if(con!=null){
                        con.close();
                    }
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        
    }
  • 相关阅读:
    python_linux系统相关配置
    python_字典dict相关操作
    python_传参
    mapreduce 学习笔记
    linux 常用命令
    C++ stringstream介绍,使用方法与例子
    C++/C++11中std::numeric_limits的使用
    C++中string erase函数的使用
    C++中accumulate的用法
    malloc的用法和意义
  • 原文地址:https://www.cnblogs.com/helloMyworld0001/p/5972959.html
Copyright © 2020-2023  润新知