gpt4 book ai didi

java - 加载 Java derby 驱动程序时出错?

转载 作者:行者123 更新时间:2023-12-01 15:03:58 25 4
gpt4 key购买 nike

我想知道为什么我不能在java数据库程序中完成最简单的一步!我以前已经做过很多次了,这次我正在做接缝例程,但它不起作用。

我正在尝试以嵌入模式创建数据库。我已在 seam 目录中找到 derby.jar 文件。这是代码,之后是错误:

public static void main(String[] args) throws SQLException {

String driver ="org.apache.derby.jdbc.EmbeddedDriver";
String url= "jdbc:derby:MyDatabase";

try {

Class.forName(driver).newInstance();

} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}

Connection conn = DriverManager.getConnection(url+";create=true");
}

这是错误:

java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at Data.main(Data.java:16)
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:derby:MyDatabase;create=true
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at Data.main(Data.java:27)

最佳答案

问题是因为类路径未配置:

要在嵌入模式下使用 Derby,请将 CLASSPATH 设置为包含下面列出的 jar 文件:

Windows: C:> set CLASSPATH=%DERBY_INSTALL%\lib\derby.jar;%DERBY_INSTALL%\lib\derbytools.jar;.

UNIX: $ export CLASSPATH=$DERBY_INSTALL/lib/derby.jar:$DERBY_INSTALL/lib/derbytools.jar:.

http://db.apache.org/derby/papers/DerbyTut/install_software.html#derby_configure

关于java - 加载 Java derby 驱动程序时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13234560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com