我想知道为什么我不能在java数据库程序中做最简单的步骤!我之前已经多次这样做了,而这次我正在做缝纫程序,但它不起作用。
我正在尝试以嵌入模式创建数据库。我已将derby.jar文件放在seam目录中。这是代码,然后是错误:
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)
答案 0 :(得分:1)
问题是因为未配置类路径:
要在嵌入模式下使用Derby,请将CLASSPATH设置为包含下面列出的jar文件:
Windows:C:>组 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