JDBC Derby driver not found

匿名 (未验证) 提交于 2019-12-03 01:06:02

问题:

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();     } } 

回答1:

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:///;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



回答2:

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 


回答3:

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"



回答4:

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.



回答5:

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 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!