java从XML文件读取JDBC连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3674422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java read JDBC connection from XML file
提问by user437630
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?
采纳答案by BalusC
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>dhF_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 = dhF_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 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");
// ...
回答by stacker
I often use the Spring Frameworkto externalize the configuration of a connection pool and setup of the jdbc URL.
我经常使用Spring Framework来外部化连接池的配置和 jdbc URL 的设置。
回答by Bozho
Take a look at commons-configuration. You can read multiple configuration formats with it.
看看commons-configuration。您可以使用它读取多种配置格式。
That said, for database connection properties the a simple key-value .properties
file is better, I think.
也就是说,对于数据库连接属性.properties
,我认为简单的键值文件更好。
回答by Tomas Narros
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 来建立连接。