Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javaDB(apache.derby)时发生ClassNotFoundException_Java_Database_Derby_Classnotfoundexception - Fatal编程技术网

使用javaDB(apache.derby)时发生ClassNotFoundException

使用javaDB(apache.derby)时发生ClassNotFoundException,java,database,derby,classnotfoundexception,Java,Database,Derby,Classnotfoundexception,我正在尝试运行一个示例项目来运行derby。 我运行了一个从互联网上找到的代码。看起来不错,但打印类没有发现例外。 代码如下 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.P

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

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/";
            }
        }
    }
};
package org.owls.mem.db;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.sql.Statement;
导入java.util.Properties;
公共班机{
publicstringframework=“embedded”;
公共字符串driver=“org.apache.derby.jdbc.EmbeddedDriver”;
公共字符串协议=“jdbc:derby:”;
公共静态void main(字符串[]args)
{
新建Main().go(args);
}
void go(字符串[]args)
{
解析参数(args);
System.out.println(“SimpleApp以“+framework+”模式启动”);
尝试
{
Class.forName(driver.newInstance();
System.out.println(“加载了相应的驱动程序”);
连接conn=null;
Properties props=新属性();
道具放置(“用户”、“用户1”);
道具放置(“密码”、“用户1”);
conn=DriverManager.getConnection(协议+
“derbyDB;create=true”,道具);
System.out.println(“连接并创建了数据库derbyDB”);
连接设置自动提交(错误);
语句s=conn.createStatement();
s、 执行(“创建表derbyDB(numint,addr varchar(40))”;
System.out.println(“创建的表derbyDB”);
s、 执行(“插入derbyDB值(1956年,'Webster St.)”;
System.out.println(“插入1956年韦伯斯特”);
s、 执行(“插入derbyDB值(1910,'Union St.)”;
System.out.println(“插入1910联合”);
s、 执行(
“更新derbyDB set num=180,addr='Grand Ave',其中num=1956”);
System.out.println(“1956年韦伯斯特更新为18万”);
s、 执行(
“更新derbyDB set num=300,addr='Lakeshore Ave.'其中num=180”);
System.out.println(“更新18万至300万湖岸”);
结果集rs=s.executeQuery(
“从derbyDB中选择num,addr ORDER BY num”);
如果(!rs.next())
{
抛出新异常(“错误的行数”);
}
如果(rs.getInt(1)!=300)
{
抛出新异常(“返回了错误的行”);
}
如果(!rs.next())
{
抛出新异常(“错误的行数”);
}
如果(rs.getInt(1)!=1910)
{
抛出新异常(“返回了错误的行”);
}
如果(rs.next())
{
抛出新异常(“错误的行数”);
}
System.out.println(“已验证行”);
s、 执行(“drop table derbyDB”);
System.out.println(“删除表derbyDB”);
rs.close();
s、 close();
System.out.println(“封闭结果集和语句”);
conn.commit();
康涅狄格州关闭();
System.out.println(“提交的事务和关闭的连接”);
布尔值gotsqlex=false;
if(framework.equals(“嵌入式”))
{
尝试
{
getConnection(“jdbc:derby:;shutdown=true”);
}
捕获(SQLSE异常)
{
gotsqlex=true;
}
如果(!gotsqlex)
{
System.out.println(“数据库未正常关闭”);
}
其他的
{
System.out.println(“数据库正常关闭”);
}
}
}
捕获(可丢弃的e)
{
System.out.println(“抛出异常:”);
if(e instanceof SQLException)
{
printSQLError((SQLException)e);
}
其他的
{
e、 printStackTrace();
}
}
System.out.println(“SimpleApp完成”);
}
静态无效printSQLError(SQLException e)
{
while(e!=null)
{
System.out.println(例如toString());
e=e.getNextException();
}
}
私有void参数(字符串[]参数)
{
int length=args.length;
for(int index=0;index
它说EmbeddedDriver不在类路径上。我能做什么? 我听说javaDB包含在jdk中(在1.6版之后,我使用的是1.7版),我已经为java设置了路径。我需要额外的设置才能使用derby吗?
谢谢。

你说得对,1.6之后应该包括驱动程序


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

您的jar文件不在类路径中


检查此链接

看看我的JAVA\u主页是“C:\Program Files\JAVA\jdk1.7.0\u 25”。正如您所看到的,我使用的是jdk而不是jre。谢谢你。我认为包含“derby.jar”是很自然的,因为它是jdk的基本元素。然而,它需要额外的设置。就像你的链接一样,或者将“derby.jar”复制到项目内部并添加到buildpath(这是我的方式,但我认为你的建议更好)。非常感谢:D