zoukankan      html  css  js  c++  java
  • 读取JDBC配置文件的二种方式

    一 读取jdbc.properties文件

    1  新建jdbc.properties文件,如下:

    jdbc.user=root
    jdbc.password=123456
    jdbc.driver=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql://xxx.xxx.xxx.xxx:3306/database?useUnicode=true&characterEncoding=utf8

    2 编写readPro()方法如下:

    private static void readPro() {
            Properties pro=new Properties();
            InputStream in = PropertiesTest.class.getClassLoader().getResourceAsStream("JDBC.properties");
            try {
                pro.load(in);//加载properties配置文件
                String user = pro.getProperty("jdbc.user");
                String password = pro.getProperty("jdbc.password");
                String url = pro.getProperty("jdbc.url");
                String driver = pro.getProperty("jdbc.driver");
                System.out.println(user);
                System.out.println(password);
                System.out.println(url);
                System.out.println(driver);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    3 编写测试类PropertiesTest,调用readPro方法,如下:

    public class PropertiesTest {
        public static void main(String[] args) {
            readPro();//读取jdbc.properties配置文件
            
        }
    }

    二 读取jdbc.xml文件

    1  新建jdbc.xml文件,如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
    <properties>
        <comment>jdbc</comment>
        <entry key="jdbc.user">root</entry>
        <entry key="jdbc.password">123456</entry>
        <entry key="jdbc.driver">com.mysql.cj.jdbc.Driver</entry>
        <entry key="jdbc.url">jdbc:mysql://xxx.xxx.xxx.xxx:3306/database?useUnicode=true&amp;characterEncoding=utf8</entry>
    </properties>

    2 编写readXml()方法如下:

    private static void readPro() {
            Properties pro=new Properties();
            InputStream in = PropertiesTest.class.getClassLoader().getResourceAsStream("JDBC.properties");
            try {
                pro.load(in);//加载properties配置文件
                String user = pro.getProperty("jdbc.user");
                String password = pro.getProperty("jdbc.password");
                String url = pro.getProperty("jdbc.url");
                String driver = pro.getProperty("jdbc.driver");
                System.out.println(user);
                System.out.println(password);
                System.out.println(url);
                System.out.println(driver);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    3 编写测试类PropertiesTest,调用readXml方法,如下:

    public class PropertiesTest {
        public static void main(String[] args) {
            readPro();//读取jdbc.properties配置文件
            
        }
    }
  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    椭圆曲线加解密
    执行计划多版本查看
    椭圆曲线算法:入门(1)
    “戏精少女”的pandas学习之路,你该这么学!No.5
    用Fabric构建应收账款融资系统的方法
    区块链的去中心化创新
    搜集统计信息
    去中心化计算
  • 原文地址:https://www.cnblogs.com/aikutao/p/12218407.html
Copyright © 2011-2022 走看看