简体   繁体   中英

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. However now when trying to connect to the database from a java application I'm receiving the exception:

java.sql.SQLException: No suitable driver found for jdbc:derby: db directory

Then when trying to manually specify the JDBC driver using:

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

I get the following exception error:

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.

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

So your error can be caused by:

Driver is not loaded correctly or your URL is malformed. So at first you need to ensure that your *.jar is in classpath. Check it out.

Also try to change your URL to:

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

create=true will ensure that db will be created if does not exist.

Update:

Look at this thead also: SQLException: No suitable driver found for 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:

  1. right click on library
  2. choose add library option and from the list of libraries choose "Java DB Driver"

在此处输入图片说明

Java JDK comes with both

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.

[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.

You said you have followed the tutorial. In the tutorial you had to install JDBC driver.

Installing a JDBC driver generally consists of copying the driver to your computer, then adding the location of it to your class path.

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.

Derby is part of the JavaSE installation and I had setup environment variable DERBY_HOME instead of DERBY_INSTALL shown in the link.

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 . 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

I'm on Java 8, and I use the Hibernate 5.4.2.Final. Yet, the driver is compiled against the Java 9!

I have been putting any needed jdbc driver at for example in the jre\\lib\\ext directory. On my system that would be: X:\\Java\\jre1.8.0_181\\lib\\ext Hope that helps.

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.

In my case I needed to include the java.naming and java.management modules within the image.

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. 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
"java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/DBNAME;create=true" issue when trying to run with the database.

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

VERSIONS

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.

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. 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.

在此处输入图片说明

You do this by using 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.
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.
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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