简体   繁体   English

JDBC 未找到 Derby 驱动程序

[英]JDBC Derby driver not found

I've followed the JDBC tutorial at: http://docs.oracle.com/javase/tutorial/jdbc/basics/gettingstarted.html , and managed to build and create my own JDBC database without too much fuss.我遵循了 JDBC 教程: http://docs.oracle.com/javase/tutorial/jdbc/basics/gettingstarted.html ,并设法构建和创建了我自己的 JDBC 数据库。 However now when trying to connect to the database from a java application I'm receiving the exception:但是现在当尝试从 java 应用程序连接到数据库时,我收到了异常:

java.sql.SQLException: No suitable driver found for jdbc:derby: db directory java.sql.SQLException:找不到适合 jdbc 的驱动程序:derby: db 目录

Then when trying to manually specify the JDBC driver using:然后在尝试使用以下命令手动指定 JDBC 驱动程序时:

Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

I get the following exception error:我收到以下异常错误:

java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver

I am positive that that driver should have no issues loading as that is the driver specified in the tutorial and it had no issues creating the database using that driver.我确信该驱动程序应该没有加载问题,因为它是教程中指定的驱动程序,并且使用该驱动程序创建数据库没有问题。 I've event tried adding the property ";create=true" at the end of the connection statement to try and create a brand new database but I still receive the same exception error.我曾尝试在连接语句的末尾添加属性“;create=true”以尝试创建一个全新的数据库,但我仍然收到相同的异常错误。

Please see my application code below.请在下面查看我的应用程序代码。 Any help at all would be fantastic:).任何帮助都会很棒:)。

package com.ddg;

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


public class SQLConnect
{
    Connection Conn = null;
    String URL;
    String Username;
    String Password;

    public SQLConnect()
    {
        try
        {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        }
        catch (ClassNotFoundException e)
        {
            System.out.println(e.toString());
        }
        URL = "jdbc:derby:*directory name*";

        System.out.println("Created SQL Connect");
    }

    public void CreateConnection()
    {
        try
        {
            Conn = DriverManager.getConnection(URL);
            System.out.println("Successfully Connected");
        }
        catch (SQLException e)
        {
            System.out.println(e.toString());
        }
    }

    public void CloseConnection()
    {
        try
        {
            this.Conn.close();
            System.out.println("Connection successfully closed");
        }
        catch (SQLException e)
        {
            System.out.println(e.toString());
        }
    }

    public static void main(String args[])
    {
        SQLConnect sql = new SQLConnect();
        sql.CreateConnection();
        sql.CloseConnection();
    }
}

java.sql.SQLException: No suitable driver found for jdbc:derby:db directory java.sql.SQLException: 找不到适合 jdbc:derby:db 目录的驱动程序

So your error can be caused by:所以你的错误可能是由以下原因引起的:

Driver is not loaded correctly or your URL is malformed.驱动程序未正确加载或您的URL格式错误。 So at first you need to ensure that your *.jar is in classpath.所以首先你需要确保你的*.jar在类路径中。 Check it out.一探究竟。

Also try to change your URL to:还尝试将您的URL更改为:

jdbc:derby://<path>/<databasename>;create=true

create=true will ensure that db will be created if does not exist. create=true将确保 db 如果不存在则创建。

Update:更新:

Look at this thead also: SQLException: No suitable driver found for jdbc:derby://localhost:1527也看看这个标题: SQLException:没有为 jdbc:derby://localhost:1527 找到合适的驱动程序

If you have this type of error如果你有这种类型的错误

java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver

and you are using netbeans then you have to follow these steps:并且您正在使用netbeans那么您必须按照以下步骤操作:

  1. right click on library右键单击库
  2. choose add library option and from the list of libraries choose "Java DB Driver"选择添加库选项,然后从库列表中选择“Java DB 驱动程序”

在此处输入图片说明

Java JDK comes with both Java JDK 都带有

org.apache.derby.jdbc.EmbeddedDriver
org.apache.derby.jdbc.ClientDriver

Within eclipse add the following jars to the used JRE(JDK) or explicitly to your project.在 eclipse 中,将以下 jars 添加到使用的 JRE(JDK) 或显式添加到您的项目中。

[JDK]db/lib/derby.jar (EmbeddedDriver)
[JDK]db/lib/derbyclient.jar (ClientDriver)

For runtine you needed to made the appropriates jar available for your java application.对于runtine,您需要为您的java 应用程序提供适当的jar。

You said you have followed the tutorial.您说您已按照教程进行操作。 In the tutorial you had to install JDBC driver.在本教程中,您必须安装 JDBC 驱动程序。

Installing a JDBC driver generally consists of copying the driver to your computer, then adding the location of it to your class path.安装 JDBC 驱动程序通常包括将驱动程序复制到您的计算机,然后将其位置添加到您的类路径中。

After installing the driver you run安装驱动后运行

java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver

That is only possible if you messed the correct diver.只有当你弄乱了正确的潜水员时,这才有可能。

You have used你用过

org.apache.derby.jdbc.EmbeddedDriver

to load the driver加载驱动程序

but should use但应该使用

org.apache.derby.jdbc.ClientDriver

See the "Set DERBY_INSTALL" and "Configure Embedded Derby" section at https://db.apache.org/derby/papers/DerbyTut/install_software.html#derby_configure for details.有关详细信息, 参阅https://db.apache.org/derby/papers/DerbyTut/install_software.html#derby_configure 上的“设置 DERBY_INSTALL”和“配置嵌入式 Derby”部分。

Derby is part of the JavaSE installation and I had setup environment variable DERBY_HOME instead of DERBY_INSTALL shown in the link. Derby 是 JavaSE 安装的一部分,我设置了环境变量DERBY_HOME而不是链接中显示的DERBY_INSTALL

C:\> set DERBY_HOME=c:\Program Files\Java\jdk1.8.0_60\db
C:\> set CLASSPATH=%DERBY_INSTALL%\lib\derby.jar;%DERBY_INSTALL%\lib\derbytools.jar;.
C:\> cd %DERBY_INSTALL%\bin
c:\Program Files\Java\jdk1.8.0_60\db\bin> setEmbeddedCP.bat

I was getting the java.lang.ClassNotFoundException upon using the ClientDriver .我在使用ClientDriver时得到java.lang.ClassNotFoundException I used the latest Driver binaries, and that was the mistake.我使用了最新的驱动程序二进制文件,这是错误的。

At that time, the latest Driver binary was 10.15.1.3, right here: Apache Site当时,最新的驱动程序二进制文件是 10.15.1.3,就在这里: Apache 站点

I'm on Java 8, and I use the Hibernate 5.4.2.Final.我使用的是 Java 8,并且使用 Hibernate 5.4.2.Final。 Yet, the driver is compiled against the Java 9!然而,驱动程序是针对 Java 9 编译的!

I have been putting any needed jdbc driver at for example in the jre\\lib\\ext directory.例如,我已经将任何需要的 jdbc 驱动程序放在 jre\\lib\\ext 目录中。 On my system that would be: X:\\Java\\jre1.8.0_181\\lib\\ext Hope that helps.在我的系统上,这将是: X:\\Java\\jre1.8.0_181\\lib\\ext 希望有帮助。

I found recently that if you are using jlink to create a runtime, you may need to include additional jdk modules to allow the driver to instantiate.我最近发现如果您使用jlink创建运行时,您可能需要包含额外的 jdk 模块以允许驱动程序实例化。

In my case I needed to include the java.naming and java.management modules within the image.就我而言,我需要在图像中包含java.namingjava.management模块。

So I encountered this error and it was quite an irritating and hectic task to resolve this.所以我遇到了这个错误,解决这个问题是一项非常令人恼火和忙碌的任务。 But in the end, I managed to find a perfect video that made me install derby from the start and guided me perfectly on how to install it.但最终,我设法找到了一个完美的视频,它让我从一开始就安装了 derby,并完美地指导了我如何安装它。 However, there is one more step after the video.但是,视频之后还有一步。

Watch this video if you have set up JavaFX packages and are able to run the program normally, but facing如果您已经设置了 JavaFX 包并且能够正常运行程序,请观看此视频,但面临
"java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/DBNAME;create=true" issue when trying to run with the database.尝试使用数据库运行时出现“java.sql.SQLException:找不到适用于 jdbc:derby://localhost:1527/DBNAME;create=true”的驱动程序问题。

Link to the tutorial链接到教程

Now After this is set up you will now be able to start/stop the database (via the services tab) and will be able to connect with the DB.现在设置完成后,您现在将能够启动/停止数据库(通过服务选项卡)并且能够与数据库连接。 But the issue will still persist in trying to edit the DB.但是在尝试编辑数据库时问题仍然存在。

TO rectify this, follow the steps ->要纠正此问题,请按照以下步骤操作 ->

Right click on project ---> Properties ---> Libraries ---> Click on '+' in Classpath ---> Add jar/folder ---> Go to the lib folder inside the derby and select derbyclient.jar右键单击项目--->属性--->--->在类路径中单击“+” --->添加 jar/文件夹--->转到 derby 中的 lib 文件夹并选择 derbyclient.jar

VERSIONS版本

JAVA - 17.0.1, Netbeans - 12.6 JAVA - 17.0.1,Netbeans - 12.6

HOPE IT HELPED, THANKS :)希望有所帮助,谢谢:)

This seems to be an age old problem and caused me and others in this thread significant frustration.这似乎是一个古老的问题,并在此线程中引起了我和其他人的极大挫败感。 Despite being successfully running tests in the Eclipse IDE test harness the application would fail to load the embedded drivers if called through a network connection.尽管在 Eclipse IDE 测试工具中成功运行测试,但如果通过网络连接调用,应用程序将无法加载嵌入式驱动程序。

With little evidence, I am going to suggest that the problem lies with Derby and the way it searches the classpath for the drivers and the delay causes a concurrency issue in that it can't find the drivers in time when searching outside its workspace.在几乎没有证据的情况下,我将建议问题出在 Derby 以及它在classpath搜索驱动程序的方式和延迟导致并发问题,因为它在其工作空间外搜索时无法及时找到驱动程序。 I have repeated this test many time and can only achieve consistency if I create a SharedLibraries project within the programme's workspace and then adding this shared library to the libraries classpath as shown in the screen captures.我已多次重复此测试,只有在程序工作区中创建SharedLibraries项目,然后将此共享库添加到libraries classpath才能实现一致性,如屏幕截图所示。

在此处输入图片说明

You do this by using Project->Properties->Java Build Path->Libraries .您可以通过使用Project->Properties->Java Build Path->Libraries The only downside with this approach is that you don't know the source of the libaries and their versions, however this can be overcome by just adding the version number to the library files if this is an issue.这种方法的唯一缺点是您不知道库的来源及其版本,但是如果这是一个问题,只需将版本号添加到库文件中就可以解决这个问题。

Tried many other options, but finally managed to fix this by simply removing the following line from my code:尝试了许多其他选项,但最终通过简单地从我的代码中删除以下行来解决此问题:

Class.forName(driverClass);

I read somewhere that Derby uses internal jdbc driver which comes by default with the jdk.我在某处读到 Derby 使用内部 jdbc 驱动程序,默认情况下它随 jdk 一起提供。
I went through many jars which came along with the Derby installation, and looked for org.apache.derby.jdbc.EmbeddedDriver class. Did not find it any of the jars.我浏览了许多 jars 随 Derby 安装而来,并寻找org.apache.derby.jdbc.EmbeddedDriver class。没有找到任何 jars。
Bit surprising, but it is true as it's working.有点令人惊讶,但它确实有效。
Note: You have to keep the Derby.jar on your classpath, and this answer is specifically for Embedded mode of derby.注意:您必须将 Derby.jar 保留在您的类路径中,这个答案专门针对德比的嵌入式模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM