java.lang.NoClassDefFoundError 连接到嵌入式 Derby 数据库时
java.lang.NoClassDefFoundError while connecting to Embedded Derby Database
我在嵌入式模式下使用 Apache NetBeans IDE 11.3(使用 JDK 13)和 Derby。连接到数据库时出现以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/derby/iapi/jdbc/JDBCBoot
at org.apache.derby.jdbc.EmbeddedDriver.<clinit>(EmbeddedDriver.java:95)
at Testing.testConnection.main(testConnection.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.derby.iapi.jdbc.JDBCBoot
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 2 more
代码如下:
package Testing;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.derby.jdbc.EmbeddedDriver;
public class testConnection {
public static void main(String[] args) {
try {
DriverManager.registerDriver(new EmbeddedDriver());
Connection con = DriverManager.getConnection("jdbc:derby:testdb;create=true");
Statement stmt = con.createStatement();
stmt.execute("create table test(id int(2) primary key, name varchar not null);");
stmt.execute("insert into test values (01, 'Jeff'), (02, 'Clint'), (03, 'Mike');");
ResultSet rs = stmt.executeQuery("select * from test;");
while (rs.next()) {
System.out.println(rs.getString("id") + "\t" + rs.getString("name"));
}
DriverManager.getConnection("jdbc:derby:;shutdown=true");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
我不知道出了什么问题。我想我没有正确设置数据库,但我不确定。我是 Derby 和嵌入式数据库的新手。以前用过 MySQL 但它更容易。任何帮助将不胜感激。
更新:
我看到之前没有在依赖中添加'derby.jar'(我只是使用'derbytools.jar'),所以我添加了它,现在出现以下错误:
java.lang.NoClassDefFoundError: org/apache/derby/shared/common/security/SystemPermission
at org.apache.derby.iapi.jdbc.JDBCBoot.boot(JDBCBoot.java:93)
at org.apache.derby.iapi.jdbc.JDBCBoot.boot(JDBCBoot.java:68)
at org.apache.derby.jdbc.EmbeddedDriver.<clinit>(EmbeddedDriver.java:95)
at Testing.testConnection.main(testConnection.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.derby.shared.common.security.SystemPermission
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 4 more
确保将 Apache derby JDBC 驱动程序的 jar 添加到项目中(作为依赖项),可在此处下载:http://db.apache.org/derby/derby_downloads.html
正如 Mark Rotteveel 在评论中所建议的那样,将 derbyshared.jar
以及 derby.jar
和 derbytools.jar
添加到依赖项中是有效的,我不再收到该错误。我非常非常感谢他的帮助。谢谢。
我在嵌入式模式下使用 Apache NetBeans IDE 11.3(使用 JDK 13)和 Derby。连接到数据库时出现以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/derby/iapi/jdbc/JDBCBoot
at org.apache.derby.jdbc.EmbeddedDriver.<clinit>(EmbeddedDriver.java:95)
at Testing.testConnection.main(testConnection.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.derby.iapi.jdbc.JDBCBoot
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 2 more
代码如下:
package Testing;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.derby.jdbc.EmbeddedDriver;
public class testConnection {
public static void main(String[] args) {
try {
DriverManager.registerDriver(new EmbeddedDriver());
Connection con = DriverManager.getConnection("jdbc:derby:testdb;create=true");
Statement stmt = con.createStatement();
stmt.execute("create table test(id int(2) primary key, name varchar not null);");
stmt.execute("insert into test values (01, 'Jeff'), (02, 'Clint'), (03, 'Mike');");
ResultSet rs = stmt.executeQuery("select * from test;");
while (rs.next()) {
System.out.println(rs.getString("id") + "\t" + rs.getString("name"));
}
DriverManager.getConnection("jdbc:derby:;shutdown=true");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
我不知道出了什么问题。我想我没有正确设置数据库,但我不确定。我是 Derby 和嵌入式数据库的新手。以前用过 MySQL 但它更容易。任何帮助将不胜感激。
更新:
我看到之前没有在依赖中添加'derby.jar'(我只是使用'derbytools.jar'),所以我添加了它,现在出现以下错误:
java.lang.NoClassDefFoundError: org/apache/derby/shared/common/security/SystemPermission
at org.apache.derby.iapi.jdbc.JDBCBoot.boot(JDBCBoot.java:93)
at org.apache.derby.iapi.jdbc.JDBCBoot.boot(JDBCBoot.java:68)
at org.apache.derby.jdbc.EmbeddedDriver.<clinit>(EmbeddedDriver.java:95)
at Testing.testConnection.main(testConnection.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.derby.shared.common.security.SystemPermission
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 4 more
确保将 Apache derby JDBC 驱动程序的 jar 添加到项目中(作为依赖项),可在此处下载:http://db.apache.org/derby/derby_downloads.html
正如 Mark Rotteveel 在评论中所建议的那样,将 derbyshared.jar
以及 derby.jar
和 derbytools.jar
添加到依赖项中是有效的,我不再收到该错误。我非常非常感谢他的帮助。谢谢。