【问题标题】:Error while using Apache Derby Embedded Database [duplicate]使用 Apache Derby 嵌入式数据库时出错 [重复]
【发布时间】:2021-10-11 21:31:51
【问题描述】:

我在执行我的 java 应用程序时收到以下错误:

Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:derby:flightdb;create=true
   at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
   at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251)
   at airline_reservation_system.Derby.<init>(Derby.java:19)

来自 Derby.java 的代码中出现错误

public Derby() throws SQLException {
    databaseURL = "jdbc:derby:flightdb;create=true";
    conn = DriverManager.getConnection(databaseURL);
    statement = conn.createStatement();
    createTables();
}

这可能是什么原因?我必须开始什么才能让 Derby 工作吗?由于我使用的是嵌入式数据库,我想这应该不是问题。

我也在 pom.xml 中包含以下内容:

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <version>10.15.2.0</version>
    <scope>test</scope>
</dependency>

当我使用 Maven 和嵌入式 Derby 时,我认为我不应该做任何额外的设置或单独安装任何 jar 文件。我不确定为什么我仍然收到此错误。

这是一个 MRE:

package mwe;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Derby {
    private String databaseURL;
    private Connection conn;
    private Statement statement;

    public static void main(String[] args) throws SQLException {
        new Derby();
    }

    public Derby() throws SQLException {
        databaseURL = "jdbc:derby:flightdb;create=true";
        conn = DriverManager.getConnection(databaseURL);
        statement = conn.createStatement();
    }

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>code</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.9</maven.compiler.source>
        <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
    </properties>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.derby/derbyclient -->
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derbyclient</artifactId>
            <version>10.15.2.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.derby/derby -->
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <version>10.15.2.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->


    </dependencies>
    <build>
        <plugins>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

mvn 依赖:树

mvn dependency:tree
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/usr/share/maven/lib/guice.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for groupId:code:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.(groupId:artifactId)' must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-compiler-plugin @ line 44, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ----------------------------< groupId:code >----------------------------
[INFO] Building code 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ code ---
[INFO] groupId:code:jar:1.0-SNAPSHOT
[INFO] +- org.apache.derby:derbyclient:jar:10.15.2.0:compile
[INFO] |  \- org.apache.derby:derbyshared:jar:10.15.2.0:compile
[INFO] \- org.apache.derby:derby:jar:10.15.2.0:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.003 s
[INFO] Finished at: 2021-08-07T17:14:20+05:30

【问题讨论】:

  • Derby 仅用于单元测试 (&lt;scope&gt;test&lt;/scope&gt;)。运行单元测试时是否发生错误?
  • 我删除了那个范围,但仍然是同样的错误。它在运行实际程序时发生。我没有任何单元测试
  • 当您说嵌入式时,您是指嵌入 derby 的某个 IDE 吗?我问这个问题是因为 NetBeans IDE 嵌入了 derby 数据库,所以想要清楚一点!
  • 不。不涉及 IDE。嵌入式是指可以以编程方式使用的普通嵌入式 Apache Derby 数据库,并且数据库存储为文件。不需要启动服务器。我正在使用没有嵌入式内置的 Intellij
  • 我已经更新了最小的可重现示例

标签: java derby


【解决方案1】:
<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derbyclient</artifactId>
    <version>10.1.2.1</version>
</dependency>

或许能帮到你

Class.forName("org.apache.derby.jdbc.ClientDriver"); // (for network)
Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); // (for embedded - afaicr)

就我个人而言,我在加载更高版本的驱动程序时遇到了问题。我很乐意接受指导;)我怀疑驱动程序加载方式已经改变 更新:最新的是启用 JDBC 4.0 并且是自动加载的。 DriverManager.getConnection 加载正常

【讨论】:

    猜你喜欢
    • 2017-08-31
    • 1970-01-01
    • 2023-03-11
    • 2015-10-19
    • 2020-12-25
    • 2020-06-24
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    相关资源
    最近更新 更多