文档告诉我们像这样加载 JDBC 驱动程序
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
https://db.apache.org/derby/papers/DerbyTut/embedded_intro.html
但是没有连接就可以正常工作
connection = DriverManager.getConnection("jdbc:derby:" + pathDerby + ";create=true");
这是为什么?
日志中的版本:启动 Derby 版本 Apache Software Foundation - Apache Derby - 10.13.1.1 - (1765088)
编辑:
实际上,如果您关闭 Derby 引擎并希望在同一个 JWM 进程中再次打开它(我在集成测试中一直这样做),则需要它
关闭后
DriverManager.getConnection("jdbc:derby:;shutdown=true");
您应该像这样重新打开
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
connection = DriverManager.getConnection("jdbc:derby:" + pathDerby + ";create=true");
最佳答案
The DriverManager methods getConnection and getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism. JDBC 4.0 Drivers must include the file META-INF/services/java.sql.Driver. This file contains the name of the JDBC drivers implementation of java.sql.Driver. For example, to load the my.sql.Driver class, the META-INF/services/java.sql.Driver file would contain the entry:
my.sql.Driver
Applications no longer need to explicitly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification.
关于java - Derby EmbeddedDriver 在没有 Class.forName 的情况下工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41519844/