本文介绍了找不到 JDBC Derby 驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了 JDBC 教程:http://docs.oracle.com/javase/tutorial/jdbc/basics/gettingstarted.html,并设法轻松地构建和创建了我自己的 JDBC 数据库.但是现在当我尝试从 Java 应用程序连接到数据库时,我收到了异常:

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: 找不到适合 jdbc:derby:db 的驱动程序目录

然后尝试手动指定 JDBC 驱动程序时使用:

Then when trying to manually specify the JDBC driver using:

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

我收到以下异常错误:

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

我确信该驱动程序应该没有加载问题,因为这是教程中指定的驱动程序,并且使用该驱动程序创建数据库没有问题.我曾尝试在连接语句的末尾添加属性;create=true"以尝试创建一个全新的数据库,但我仍然收到相同的异常错误.

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

推荐答案

所以你的错误可能是由以下原因引起的:

So your error can be caused by:

驱动程序未正确加载或您的 URL 格式错误.所以首先你需要确保你的 *.jar 在类路径中.看看吧.

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.

还尝试将您的 URL 更改为:

Also try to change your URL to:

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

create=true 将确保数据库不存在时创建.

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

也看看这个标题:SQLException:找不到合适的驱动程序jdbc:derby://localhost:1527

这篇关于找不到 JDBC Derby 驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 04:35