java从XML文件读取JDBC连接

时间:2021-01-19 09:13:02

Anyone have idea how can i write XMl file that i will have JDBC connection (username, passwd, driver, connection) in it and then read that xml for connecting to db?

有谁知道我如何写XMl文件,我将在其中有JDBC连接(用户名、密码、驱动程序、连接),然后读取XMl以连接到db?

4 个解决方案

#1


8  

Here's how you could compose the XML:

以下是如何组合XML:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <jdbc>
        <url>jdbc:mysql://localhost:3306/javabase</url>
        <driver>com.mysql.jdbc.Driver</driver>
        <username>java</username>
        <password>d$7hF_r!9Y</password>
    </jdbc>
</config>

Assuming that it's called config.xml and is been placed in the root of the classpath, here's an example how you could load it with help of JAXP and Xpath:

假设它叫config。xml并被放置在类路径的根中,这里有一个例子,您可以使用JAXP和Xpath来加载它:

InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.xml");
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(input));
XPath xpath = XPathFactory.newInstance().newXPath();
String url = (String) xpath.compile("//config//jdbc//url").evaluate(document, XPathConstants.STRING);
String driver = (String) xpath.compile("//config//jdbc//driver").evaluate(document, XPathConstants.STRING);
String username = (String) xpath.compile("//config//jdbc//username").evaluate(document, XPathConstants.STRING);
String password = (String) xpath.compile("//config//jdbc//password").evaluate(document, XPathConstants.STRING);
// ...

It's only pretty verbose as opposed to properties files. Here's an example of such a properties file:

与属性文件相比,它非常冗长。下面是这样一个属性文件的示例:

jdbc.url = jdbc:mysql://localhost:3306/javabase
jdbc.driver = com.mysql.jdbc.Driver
jdbc.username = java
jdbc.password = d$7hF_r!9Y

Assuming that it's named config.properties and is been placed in the root of the classpath (or its root path is been added to the classpath), here's how you could load it from the classpath:

假设它被命名为config。属性并被放置在类路径的根目录中(或者其根路径被添加到类路径中),以下是如何从类路径中加载它的方法:

Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
String url = properties.getProperty("jdbc.url");
String driver = properties.getProperty("jdbc.driver");
String username = properties.getProperty("jdbc.username");
String password = properties.getProperty("jdbc.password");
// ...

#2


1  

Take a look at commons-configuration. You can read multiple configuration formats with it.

看看常见的配置。您可以使用它读取多个配置格式。

That said, for database connection properties the a simple key-value .properties file is better, I think.

也就是说,对于数据库连接属性,我认为简单的键值.properties文件更好。

#3


1  

You could define your own XML schema, bind it to a Java bean, and parse it through JAXB. Then, you just have to invoke the getters of your bean to build your connection.

您可以定义自己的XML模式,将它绑定到Java bean,并通过JAXB解析它。然后,您只需调用bean的getter来构建连接。

#4


1  

I often use the Spring Framework to externalize the configuration of a connection pool and setup of the jdbc URL.

我经常使用Spring框架外化连接池的配置和jdbc URL的设置。

#1


8  

Here's how you could compose the XML:

以下是如何组合XML:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <jdbc>
        <url>jdbc:mysql://localhost:3306/javabase</url>
        <driver>com.mysql.jdbc.Driver</driver>
        <username>java</username>
        <password>d$7hF_r!9Y</password>
    </jdbc>
</config>

Assuming that it's called config.xml and is been placed in the root of the classpath, here's an example how you could load it with help of JAXP and Xpath:

假设它叫config。xml并被放置在类路径的根中,这里有一个例子,您可以使用JAXP和Xpath来加载它:

InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.xml");
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(input));
XPath xpath = XPathFactory.newInstance().newXPath();
String url = (String) xpath.compile("//config//jdbc//url").evaluate(document, XPathConstants.STRING);
String driver = (String) xpath.compile("//config//jdbc//driver").evaluate(document, XPathConstants.STRING);
String username = (String) xpath.compile("//config//jdbc//username").evaluate(document, XPathConstants.STRING);
String password = (String) xpath.compile("//config//jdbc//password").evaluate(document, XPathConstants.STRING);
// ...

It's only pretty verbose as opposed to properties files. Here's an example of such a properties file:

与属性文件相比,它非常冗长。下面是这样一个属性文件的示例:

jdbc.url = jdbc:mysql://localhost:3306/javabase
jdbc.driver = com.mysql.jdbc.Driver
jdbc.username = java
jdbc.password = d$7hF_r!9Y

Assuming that it's named config.properties and is been placed in the root of the classpath (or its root path is been added to the classpath), here's how you could load it from the classpath:

假设它被命名为config。属性并被放置在类路径的根目录中(或者其根路径被添加到类路径中),以下是如何从类路径中加载它的方法:

Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
String url = properties.getProperty("jdbc.url");
String driver = properties.getProperty("jdbc.driver");
String username = properties.getProperty("jdbc.username");
String password = properties.getProperty("jdbc.password");
// ...

#2


1  

Take a look at commons-configuration. You can read multiple configuration formats with it.

看看常见的配置。您可以使用它读取多个配置格式。

That said, for database connection properties the a simple key-value .properties file is better, I think.

也就是说,对于数据库连接属性,我认为简单的键值.properties文件更好。

#3


1  

You could define your own XML schema, bind it to a Java bean, and parse it through JAXB. Then, you just have to invoke the getters of your bean to build your connection.

您可以定义自己的XML模式,将它绑定到Java bean,并通过JAXB解析它。然后,您只需调用bean的getter来构建连接。

#4


1  

I often use the Spring Framework to externalize the configuration of a connection pool and setup of the jdbc URL.

我经常使用Spring框架外化连接池的配置和jdbc URL的设置。