【问题标题】:ClassNotFoundException while using javaDB(apache.derby)使用 javaDB(apache.derby) 时出现 ClassNotFoundException
【发布时间】:2013-10-04 07:35:05
【问题描述】:

我正在尝试运行一个示例项目来运行 derby。 我运行从互联网上找到的代码。看起来不错,但打印 ClassNotFoundException。 代码如下;

package org.owls.mem.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class Main {

    public String framework = "embedded";
    public String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    public String protocol = "jdbc:derby:";

    public static void main(String[] args)
    {
        new Main().go(args);
    }

    void go(String[] args)
    {
        parseArguments(args);

        System.out.println("SimpleApp starting in " + framework + " mode.");

        try
        {

            Class.forName(driver).newInstance();
            System.out.println("Loaded the appropriate driver.");

            Connection conn = null;
            Properties props = new Properties();
            props.put("user", "user1");
            props.put("password", "user1");

            conn = DriverManager.getConnection(protocol +
                    "derbyDB;create=true", props);

            System.out.println("Connected to and created database derbyDB");

            conn.setAutoCommit(false);

            Statement s = conn.createStatement();

            s.execute("create table derbyDB(num int, addr varchar(40))");
            System.out.println("Created table derbyDB");
            s.execute("insert into derbyDB values (1956,'Webster St.')");
            System.out.println("Inserted 1956 Webster");
            s.execute("insert into derbyDB values (1910,'Union St.')");
            System.out.println("Inserted 1910 Union");
            s.execute(
                "update derbyDB set num=180, addr='Grand Ave.' where num=1956");
            System.out.println("Updated 1956 Webster to 180 Grand");

            s.execute(
                "update derbyDB set num=300, addr='Lakeshore Ave.' where num=180");
            System.out.println("Updated 180 Grand to 300 Lakeshore");

            ResultSet rs = s.executeQuery(
                    "SELECT num, addr FROM derbyDB ORDER BY num");

            if (!rs.next())
            {
                throw new Exception("Wrong number of rows");
            }

            if (rs.getInt(1) != 300)
            {
                throw new Exception("Wrong row returned");
            }

            if (!rs.next())
            {
                throw new Exception("Wrong number of rows");
            }

            if (rs.getInt(1) != 1910)
            {
                throw new Exception("Wrong row returned");
            }

            if (rs.next())
            {
                throw new Exception("Wrong number of rows");
            }

            System.out.println("Verified the rows");

            s.execute("drop table derbyDB");
            System.out.println("Dropped table derbyDB");

            rs.close();
            s.close();
            System.out.println("Closed result set and statement");

            conn.commit();
            conn.close();
            System.out.println("Committed transaction and closed connection");

            boolean gotSQLExc = false;

            if (framework.equals("embedded"))
            {
                try
                {
                    DriverManager.getConnection("jdbc:derby:;shutdown=true");
                }
                catch (SQLException se)
                {
                    gotSQLExc = true;
                }

                if (!gotSQLExc)
                {
                    System.out.println("Database did not shut down normally");
                }
                else
                {
                    System.out.println("Database shut down normally");
                }
            }
        }
        catch (Throwable e)
        {
            System.out.println("exception thrown:");

            if (e instanceof SQLException)
            {
                printSQLError((SQLException) e);
            }
            else
            {
                e.printStackTrace();
            }
        }

        System.out.println("SimpleApp finished");
    }

    static void printSQLError(SQLException e)
    {
        while (e != null)
        {
            System.out.println(e.toString());
            e = e.getNextException();
        }
    }

    private void parseArguments(String[] args)
    {
        int length = args.length;

        for (int index = 0; index < length; index++)
        {
            if (args[index].equalsIgnoreCase("jccjdbcclient"))
            {
                framework = "jccjdbc";
                driver = "com.ibm.db2.jcc.DB2Driver";
                protocol = "jdbc:derby:net://localhost:1527/";
            }
            if (args[index].equalsIgnoreCase("derbyclient"))
            {
                framework = "derbyclient";
                driver = "org.apache.derby.jdbc.ClientDriver";
                protocol = "jdbc:derby://localhost:1527/";
            }
        }
    }
};

它说 EmbeddedDriver 不在类路径中。我能做些什么? 我听说javaDB包含在jdk中(ver.1.6之后,我正在使用1.7),我已经为java设置了PATH。我需要额外的设置才能使用 derby 吗? 谢谢。

【问题讨论】:

标签: java database derby classnotfoundexception


【解决方案1】:

你的 jar 文件不在你的类路径中

查看此链接REFERENCE

【讨论】:

  • 是的。我认为包含'derby.jar'是很自然的,因为它是jdk的基本元素。但是,它需要额外的设置。喜欢您的链接或将“derby.jar”复制到项目内部并添加到构建路径(这是我的方式,但我认为您的建议更好。)。非常感谢:D
【解决方案2】:

你说得对,1.6之后应该包含驱动http://db.apache.org/derby/docs/10.5/devguide/cdevdvlp40653.html

现在检查您的 JAVA_HOME 值的路径是否在 JDK 而不是 JRE7 上。

【讨论】:

  • 我的 JAVA_HOME 是“C:\Program Files\Java\jdk1.7.0_25”。如您所见,我使用的是 jdk 而不是 jre。谢谢
猜你喜欢
  • 2020-05-17
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多