Use the configuration file to load the jdbc driver

First of all, we need to understand a collection class——Properties class

Properties class: he is the only collection combined with IO stream
                The Properties class provides two methods store and load for file input and output operations

                1. You can use the store in the properties collection to persistently write the temporary data in the collection to the hard disk for storage
                    void store (outputstream out, string comments): byte output stream, cannot write Chinese
                    void store (Write write, string comments): character output stream, can write Chinese
                    String comments: Comments, used to explain what the saved file is for, Chinese cannot be used, Unicode encoding is used by default, and an empty string is generally used

                    Steps for usage:
                    1. Create a properties collection object and add data
                    2. Create a byte output stream/character output stream object, and bind the destination to be output in the construction method
                    3. Use the method store in the properties collection to permanently write the temporary data in the collection to the hard disk for storage
                    4. Release resources


                2. You can use the method load in the properties collection to read and write the files (key-value pairs) saved in the hard disk to the collection for use
                    void load (InputStream inStream) byte input stream, cannot contain Chinese
                    void load (Reader reader) character input stream, can have Chinese
                    Steps for usage:
                    1. Create a properties collection object
                    2. Use the method load in the properties collection to read the file that saves the key-value pair
                    3. Traverse the properties collection



                The content in the configuration file ending in .properties
                    Notice:
                    1. The file that stores the key-value pair, the default connection symbol of the key-value pair can use = space (other symbols)
                    2. You can use # to comment
                    3. The default is a string without quotation marks

How to use the configuration file

        

//读取资源文件,获取值
        Properties pro = new Properties();
        //获取src路径下的文件的方式--->Classloader 类加载器:
        ClassLoader classLoader=JDBCTOOL.class.getClassLoader();//可以获取字节码文件
        URL res =classLoader.getSystemResource("jdbc.properties");//URL统一资源定位器,可以定位绝对路径
        String path=res.getPath();//使用URL获取绝对路径
        try {
            pro.load(new FileReader(path));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //获取数据赋值
        url=pro.getProperty("url");
        user=pro.getProperty("user");
        password=pro.getProperty("password");
        driver=pro.getProperty("driver");

Note: The configuration file must be placed in the src directory

Guess you like

Origin blog.csdn.net/qq_47701945/article/details/121113209