我们最近开始使用 Maven 构建项目。之前,所有 JAR 都添加到 libs 文件夹中并添加到类路径中。在转向 Maven 构建时,我错过了添加对“sqlite-jdbc”的依赖项。该库需要从本地 .db3 文件读取数据。
由于“org.sqlite.JBDC”类是通过调用代码 Class.forName("org.sqlite.JDBC")
加载的,因此没有编译错误,我部署了 WAR 文件,servlet 失败在服务器中。我正在考虑一种在编译时本身发现问题的方法,以避免将来出现任何此类错误。我可以简单地调用 JDBC.PREFIX 来加载 JDBC,这样,如果我忘记将依赖项添加到 pom.xml 文件,我可以在编译时本身找到问题吗?
Class.forName("org.sqlite.JDBC")
与 JDBC.PREFIX
加载 JDBC 类有区别吗?
We had recently moved to building projects using Maven. Earlier, all the JARs were added in libs folder and added to classpath. While moving to the Maven build, I missed to add a dependency to the 'sqlite-jdbc'. This library is required to read data from a local .db3 file.
As the 'org.sqlite.JBDC' class was loaded by calling the code Class.forName("org.sqlite.JDBC")
, there was no compilation error and I deployed the WAR file and servlet failed in the server. I was thinking of a way to find the issue at the compilation time itself to avoid any such mistakes in the future. Can I simply call the JDBC.PREFIX to load the JDBC, so that, If I forget to add the dependency to the pom.xml file, I can find the issue at the compile time, itself?
Is there was difference between Class.forName("org.sqlite.JDBC")
vs JDBC.PREFIX
to load JDBC class?
发布评论
评论(1)
不需要
Class.forName
一般不需要调用
Class.forName
。早些年就使用过此类调用。现代 Java 已进行更改,以便通过 Java 服务提供者接口 (SPI ) 设施。
如果您正在使用建议
Class.forName
的书籍或教程,您可能希望获得更多最新的学习材料。DataSource
此外,在 Servlet 工作中,您通常不应显式访问 JDBC 驱动程序。
设置数据库服务器地址、用户名和密码需要硬编码文本。当部署系统管理员更改 IP 地址或轮换密码时,您的代码就会崩溃。然后,您必须修改源代码、重新编译并重新部署。
相反,您应该外部化此类配置详细信息。
对于 SQLite,请参阅通过 (Xerial) sqlite-jdbc 驱动程序使用 DataSource 连接到 SQLite。
JNDI
您可以使用
DataSource
接口。在运行时获取DataSource
对象后,通过调用其getConnection
方法。该 DataSource 对象保存数据库服务器地址、用户名、密码以及连接数据库所需的所有其他设置。使用 JNDI 在运行时获取
DataSource
对象。如果您的系统管理员如此配置,您的 Servlet 容器可以充当命名/目录服务器来提供DataSource
对象。或者可以通过 JNDI 从外部服务器获取DataSource
,例如 LDAP< /a> 服务器。再次强调,使用 DataSource 和 JNDI 的优点在于,当部署细节发生变化时,您作为 Servlet 程序员不需要参与,甚至不需要被通知。
JDBC 驱动程序位置
对于 Servlet 工作,您通常不会将 JDBC 驱动程序与您的应用程序捆绑在一起。
相反,JDBC 驱动程序会进入由 Servlet 容器管理的文件夹。研究特定 Servlet 容器的文档。对于 Apache Tomcat,请参阅这个答案。
在开发过程中,您的 IDE 可能需要访问 JDBC 驱动程序才能进行编译。如果是这样,请在 Maven POM 中使用
provided
元素标记依赖项。该标签告诉 Maven 在最终构建中忽略该依赖项,因为该依赖项在运行时已经存在(提供)。如果您坚持将 JDBC 驱动程序捆绑在 Web 应用程序的 WAR 文件中,请参阅重要的 Mark Rotteveel 的评论。
这个话题在 Stack Overflow 上已经被讨论过很多次了。 搜索 了解更多信息。
No need for
Class.forName
There is no generally no need to call
Class.forName
.Such calls were used in the early years. Modern Java was changed so that JDBC drivers are automatically loaded and registered with the JVM via the Java Service Provider Interface (SPI) facility.
If you are using books or tutorials advising
Class.forName
, you may want to obtain more up-to-date learning materials.DataSource
Furthermore, in Servlet work you generally should not be explicitly accessing the JDBC driver.
Setting the database server address, username, and password would require hard-coding text. When the deployment sysadmins change the IP address, or rotate passwords, your code breaks. You would then have to modify your source code, re-compile, and re-deploy.
Instead, you should externalize such configuration details.
For SQLite, see Using DataSource to connect to SQLite with (Xerial) sqlite-jdbc driver.
JNDI
You can externalize database configuration by using the the
DataSource
interface. After obtaining aDataSource
object at runtime, make database connections by calling itsgetConnection
method. ThatDataSource
object holds the database server address, username, password, and all other settings needed to make a connection to the database.Obtain a
DataSource
object at runtime by using JNDI. Your Servlet container may act as the naming/directory server to provide theDataSource
object, if your sysadmin so configures it. Or theDataSource
can be obtained via JNDI from an external server such as an LDAP server.Again, the beauty of using
DataSource
and JNDI is that you as the Servlet programmer need not be involved, or even informed, when the deployment details change.JDBC driver location
For Servlet work, you generally do not bundle the JDBC driver with your app.
Instead, the JDBC driver goes into a folder managed by your Servlet container. Study the documentation for your particular Servlet container. For Apache Tomcat, see this Answer.
In development, your IDE may need access to the JDBC driver to compile. If so, in your Maven POM, mark the dependency with a
<scope>provided</scope>
element. This tag tells Maven to omit that dependency from the final build because the dependency will already be present (provided) at runtime.If you insist on bundling your JDBC driver within the WAR file of your web app, then see the important Comment by Mark Rotteveel.
This topic has been addressed many times on Stack Overflow. Search to learn more.