简体   繁体   中英

Java: Unable to load the JDBC driver for EmbeddedDerby

I have a problem when loading JDBC driver for EmbeddedDerby. Below are the cases that I compile and run my program

  • Case 1:

    Compile: E:\\java\\WorkReminder>javac -d class source/MyDerbyProgram.java

    Run: E:\\java\\WorkReminder>java -cp class MyDerbyProgram

    Error:

    Unable to load the JDBC driver org.apache.derby.jdbc.EmbeddedDriver Please check your CLASSPATH. java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268) at java.lang.ClassLoader.loadClass(ClassLoader.java:251) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:164) at MyDerbyProgram.loadDriver(MyDerbyProgram.java:143) at MyDerbyProgram.go(MyDerbyProgram.java:38) at MyDerbyProgram.main(MyDerbyProgram.java:31) java.sql.SQLException: No suitable driver SimpleApp finished

  • Case 2: Everything is ok if I do

    Compile: E:\\java\\WorkReminder>javac -d class source/MyDerbyProgram.java

    Change directory: E:\\java\\WorkReminder>cd class

    Run: E:\\java\\WorkReminder\\class>java MyDerbyProgram

    Output:

    Loaded the appropriate driver

    Row inserted.

    2-----

    1956_ _ __ _ __ _ + __ _ __ _ __ _ ___ _Ha Noi

    1975_ _ __ _ __ _ _ + __ _ __ _ __ _ ___ _Sai Gon

    SimpleApp finished

Could someone help me explain why I get error in case 1 because I am writing a more complex program ? My code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Properties;

public class MyDerbyProgram
{
    /* the default framework is embedded*/
    private String framework = "embedded";
    private String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    private String protocol = "jdbc:derby:";

    public static void main(String[] args)
    {
        new MyDerbyProgram().go(args);
        System.out.println("SimpleApp finished");
    }

    void go(String[] args)
    {
        /* load the desired JDBC driver */
        loadDriver();
        try
        {
            Connection connection = DriverManager.getConnection(protocol + "testDB; create=true");
            connection.setAutoCommit(false);


            //Create table
            Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            statement.execute("create table location(num int, address varchar(40))");

            //Insert
            PreparedStatement psInsertStatement = connection.prepareStatement("insert into location values(?, ?)");
            psInsertStatement.setInt(1, 1956);
            psInsertStatement.setString(2, "Ha Noi");
            psInsertStatement.executeUpdate();
            psInsertStatement.setInt(1, 1975);
            psInsertStatement.setString(2, "Sai Gon");
            psInsertStatement.executeUpdate();
            System.out.println("Row inserted.");

            //Select
            ResultSet resultSet = statement.executeQuery("select * from location");

            int totalRows = 0;

            resultSet.last();
            totalRows = resultSet.getRow();
            resultSet.beforeFirst();
            System.out.println(totalRows + "-----");

            String strPrintResult = "";
            while(resultSet.next())
            {
                strPrintResult += resultSet.getString("num") + "___________________+_________________________" + resultSet.getString("address") + "\n";
            }           
            System.out.println(strPrintResult);

            //Drop table
            statement.execute("drop table location");

            //Commit transaction
            connection.commit();

            //Close
            statement.close();
            statement = null;

            psInsertStatement.close();
            psInsertStatement = null;

            resultSet.close();
            resultSet = null;

            connection.close();
            connection = null;
        }

        catch(SQLException se)
        {
            System.out.println(se.toString());
        }
    }

    private void loadDriver() {
    try {
        Class.forName(driver).newInstance();
        System.out.println("Loaded the appropriate driver");
    } catch (ClassNotFoundException cnfe) {
        System.err.println("\nUnable to load the JDBC driver " + driver);
        System.err.println("Please check your CLASSPATH.");
        cnfe.printStackTrace(System.err);
    } catch (InstantiationException ie) {
        System.err.println(
                    "\nUnable to instantiate the JDBC driver " + driver);
        ie.printStackTrace(System.err);
    } catch (IllegalAccessException iae) {
        System.err.println(
                    "\nNot allowed to access the JDBC driver " + driver);
        iae.printStackTrace(System.err);
    }
    }
    private void parseArguments(String[] args)
    {
        if (args.length > 0) {
            if (args[0].equalsIgnoreCase("derbyclient"))
            {
                framework = "derbyclient";
                driver = "org.apache.derby.jdbc.ClientDriver";
                protocol = "jdbc:derby://localhost:1527/";
            }
        }
    }
}

Regards

the -cp option set the classpath and you don't need this

see here

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