zoukankan      html  css  js  c++  java
  • JDBC——jdbcUtils加载配置文件赋值

    加载配置文件:Properties对象

    对应properties文件处理,开发中也使用Properties(唯一与流有关系的集合(是map),可以读取对象变为集合中Key/Value格式)对象进行。我们将采用加载properties文件获得流,然后使用Properties对象进行处理。

    l  JDBCUtils.java中编写代码

    public class JDBCUtils {
    
     
    
        private static String driver;
    
        private static String url;
    
        private static String user;
    
        private static String password;
    
        // 静态代码块
    
        static {
    
            try {
    
                // 1 使用Properties处理流
    
                // 使用load()方法加载指定的流
    
                Properties props = new Properties();
    
                Reader is = new FileReader("db.properties");
    
                props.load(is);
    
                // 2 使用getProperty(key),通过key获得需要的值,
    
                driver = props.getProperty("driver");
    
                url = props.getProperty("url");
    
                user = props.getProperty("user");
    
                password = props.getProperty("password");
    
            } catch (Exception e) {
    
                throw new RuntimeException(e);
    
            }
    
        }
    
     
    
        /**
    
         * 获得连接
    
         */
    
        public static Connection getConnection() {
    
            try {
    
                // 1 注册驱动
    
                Class.forName(driver);
    
                // 2 获得连接
    
                Connection conn = DriverManager.getConnection(url, user, password);
    
                return conn;
    
            } catch (Exception e) {
    
                throw new RuntimeException(e);
    
            }
    
        }
    
    }
  • 相关阅读:
    深度学习中的激活函数
    23.从上往下打印二叉树
    22.栈的压入、弹出序列
    使用TensorFlow实现DNN
    shell按行读取文件
    linux集群批量执行命令
    CDH升级
    自动微分方法(auto diff)
    快速了解负载均衡
    拼写纠错的利器,BK树算法
  • 原文地址:https://www.cnblogs.com/wy20110919/p/8093423.html
Copyright © 2011-2022 走看看