如何使用JDBC Java与.xml文件建立数据库连接 [英] how to establish a database connection with an .xml file using JDBC Java

查看:238
本文介绍了如何使用JDBC Java与.xml文件建立数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我在.xml文件中具有与数据库的连接信息.这里是该文件的完整代码:

As the title indicates, I have the connection information to my database located in a .xml file. Here the full code of this file :

<?xml version="1.0" encoding="UTF-8"?>

-<context>

<Resource url="jdbc:sqlserver://LAPTOP..;database=test" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" password="cgy55wH-..." username="sa" nomBDD="test" typeConn="SQL Server"/>

<Chemin fileSauve="truc\Export\" file="truc\Export\"/>

</context>

我不知道如何从我的Java代码中检索信息. (使用JDBC) 首先,我开始开发一个功能,使我可以确认文件的存在.

I can't figure out how to retrieve the information from my java code. (using JDBC) As a first step, I started to develop a function that allows me to confirm the existence of the file.

这是代码的一部分:

    public String searchFile() throws Exception {
        String retour = "ko";
        File fichier;
        for (File lment : File.listRoots()) {
            fichier = new File(lment + "\\XFiles\\Conf\\init.xml");
            if (fichier.exists()) {
                cheminFichier=lment+"\\XFiles\\Conf\\init.xml";
                retour = "ok";
            }
        }
        return retour;
    }

我愿意接受任何可以理解和解决此问题的建议或想法.

I'm open to any suggestion or idea which allows me to undertand and solve this problem.

推荐答案

Java命名和目录接口(JNDI)

您的XML看起来像是网络容器(例如 Apache Tomcat .

如果是这样,则应该使用 Java命名和目录接口(JNDI) API内置于Java中以间接访问信息.

If so, you should be using the Java Naming and Directory Interface (JNDI) API built into Java to access the information indirectly.

有人将数据库信息加载到 LDAP 服务器或类似的目录服务,可通过JNDI从Java代码访问.

Some people load the database info in an LDAP server or a similar directory service, reachable from your Java code via JNDI.

Tomcat和类似的网络容器捆绑了它们自己的目录服务,您的Java代码通过 JNDI .此处的关键思想是硬编码在部署时更改的详细信息.您的Web应用程序代码应该不了解有关特定Web容器的任何信息,也不应了解有关其配置文件的任何信息. JNDI是Web应用程序和运行时部署详细信息之间的中间人.因此,当数据库密码更改时,您无需重新编译Web应用程序.例如,当您或您的管理员从Tomcat更改为 Payara 时,无需重新编译您的网络应用.

Tomcat and similar web containers bundle their own directory service, reached by your Java code via JNDI. The key idea here is no hard-coding of details that change at deployment time. Your web app code should not know anything about your particular web container, nor should your web app know anything about its configuration files. JNDI is the middle-man between your web app and the runtime deployment details. So when the database password changes, you need not re-compile your web app. When you or you admins change from Tomcat to Payara, for example, you need not re-compile your web app.

因此,您不会在代码中直接打开XML文件.读取和解析XML文件是Web容器的工作.

So you would not in your code be directly opening an XML file. That reading and parsing of the XML file is the job of your web container.

您将要求JNDI服务为您提供 getConnection 连接到数据库.这将所有配置信息责任交到sysadmin手中.作为程序员,该配置对您而言是透明的.

You would be asking the JNDI service to provide you with a DataSource object. From that data source, you call getConnection to connect to database. This puts all of the configuration info responsibility in the hands of the sysadmin. The configuration is transparent to you as the programmer.

关于加密XML配置文件的Resource标记中的密码:您不需要.如果您在网上搜索,则会发现一些文章,这些文章解释了在某种程度上您对保护的追求必须结束.如果在此处对密码进行加密,则仍必须在某处具有解密密钥.因此,您必须通过加密来保护该解密密钥,这意味着您需要保护第二个密钥.依此类推,从头到尾都是乌龟.在某些时候,您必须拥有值得信赖的锁定系统.

As for encrypting the password in the Resource tag of your XML configuration file: You don‘t. If you search the Web you will find articles explaining that to some point your quest for protection must end. If you encrypt the password there, you must still have a decryption key somewhere. So you must protect that decryption key by encrypting it, which means you need to protect that second key. And so on, turtles all the way down. At some point you must have a locked-down system that is trustworthy.

搜索堆栈溢出以了解更多信息,因为已经讨论了很多次.姊妹站点Server Fault Stack Exchange可能也有信息.

Search Stack Overflow to learn more as this has been covered many times. The sister site Server Fault Stack Exchange might also have info.

这篇关于如何使用JDBC Java与.xml文件建立数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆