简体   繁体   中英

Error for loading driver for Java derby?

I'm wondering why I can't do the simplest step in java database program! I have done this many times before and this time I'm doing seam routine but it doesn't work.

I'm trying to create a database in embedded mode. I have located the derby.jar file in the seam directory. Here is the code and after that is the error:

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");   
}

Here is the error:

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)

The problem is because class path is not configured :

To use Derby in its embedded mode set your CLASSPATH to include the jar files listed below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM