Java:无法为EmbeddedDerby加载JDBC驱动程序

时间:2013-08-25 17:40:18

标签: java jdbc

为EmbeddedDerby加载JDBC驱动程序时遇到问题。以下是我编译和运行程序的案例

  • 案例1:

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

    运行:E:\ java \ WorkReminder> java -cp class MyDerbyProgram

    错误:

    无法加载JDBC驱动程序org.apache.derby.jdbc.EmbeddedDriver 请检查你的CLASSPATH。 java.lang.ClassNotFoundException:org.apache.derby.jdbc.EmbeddedDriver         在java.net.URLClassLoader $ 1.run(URLClassLoader.java:200)         at java.security.AccessController.doPrivileged(Native Method)         在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:没有合适的驱动程序 SimpleApp完成了

  • 案例2:如果我做的话,一切都很好

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

    更改目录:E:\ java \ WorkReminder> cd class

    运行:E:\ java \ WorkReminder \ class> java MyDerbyProgram

    输出:

    加载适当的驱动程序

    插入行。

    2 - - - -

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

    1975_的 _ __ _ __ _ _ + < / em> __ _ __ _ __ _ ___ _Sai Gon

    SimpleApp已完成

有人可以帮我解释为什么我在案例1中遇到错误,因为我正在编写一个更复杂的程序吗? 我的代码:

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

此致

1 个答案:

答案 0 :(得分:0)

-cp选项设置类路径,您不需要此

see here