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);
    
            }
    
        }
    
    }
  • 相关阅读:
    Nginx配置中运行与启动的详细介绍
    php实现文件上传进度条
    C# 提取逗号分割的字符串
    【sas proc sql】out join
    【SAS NOTE】substr函数
    【sas proc sql】子查询
    【SAS NOTE】数字字符互换
    【SAS NOTE】数组
    【sas Notel】merge
    【sas sql proc】inner join or outer join
  • 原文地址:https://www.cnblogs.com/wy20110919/p/8093423.html
Copyright © 2011-2022 走看看