尝试运行创建Java数据库的程序时出错。我错过了什么?

时间:2013-12-08 10:58:39

标签: java database derby javadb

这是我书中的示例程序,我需要在运行自己的分配程序之前运行它来访问数据库并检索数据。我的问题是,这段代码不能正常运行,它给了我:

"ERROR: No suitable driver found for jdbc:derby:CityDB;create=true" 

at runtime.

我正在使用IntelliJ 13 - 社区版。

import java.sql.*;

/**
This program creates the CityDB database.                                    *
*/

public class CreateCityDB {

public static void main(String[] args) throws Exception {
    String sql;
    final String DB_URL = "jdbc:derby:CityDB;create=true";

    try {
        // Create a connection to the database.
        Connection conn = DriverManager.getConnection(DB_URL);

        // Create a Statement object.
        Statement stmt = conn.createStatement();

        // Create the Dvd table.
        System.out.println("Creating the City table...");
        stmt.execute("CREATE TABLE City ("    +
                    "CityName CHAR(25) NOT NULL PRIMARY KEY, "   +
                    "Population DOUBLE)");

        // Add some rows to the new table.
        sql = "INSERT INTO City VALUES" +
                "('Beijing', 12500000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('Buenos Aires', 13170000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('Cairo', 14450000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('Calcutta', 15100000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('Delhi', 18680000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('Jakarta', 18900000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('Karachi', 11800000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('Lagos', 13488000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('London', 12875000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('Los Angeles', 15250000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('Manila', 16300000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('Mexico City', 20450000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('Moscow', 15000000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('Mumbai', 19200000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('New York City', 19750000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('Osaka', 17350000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('Sao Paulo', 18850000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('Seoul', 20550000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('Shanghai', 16650000)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO City VALUES" +
        "('Tokyo', 32450000)";
        stmt.executeUpdate(sql);

        // Close Resources
        stmt.close();
        conn.close();
        System.out.println("Done");
    }
    catch(Exception ex) {
        System.out.println("ERROR: " + ex.getMessage());
    }

}

}

2 个答案:

答案 0 :(得分:2)

没有合适的驱动程序意味着您没有在类路径中添加所需的jar。 如果您正在使用eclipse,请按照

进行操作
  • 右键点击项目
  • 点击构建路径 - >配置构建路径
  • 然后单击库选项卡
  • 然后单击添加外部jar并提供jar文件的路径。
  • 重新运行项目

按照IntelliJ IDEA

的步骤操作
  1. 点击文件
  2. 点击项目结构(快捷键ctrl + shift + alt + s)
  3. 然后单击模块,然后单击依赖项
  4. 然后点击添加 - >项目库

答案 1 :(得分:2)

首先使用以下方法加载课程:

Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

修改

结果证明JDK中没有现成的JavaDB。见http://db.apache.org/derby/integrate/plugin_help/derby_app.html#Changing+the+application+to+use+the+Derby+Embedded+Driver

  

要在嵌入模式下使用Derby,请将CLASSPATH设置为包含下面列出的jar文件:   derby.jar:包含Derby引擎和Derby Embedded JDBC驱动程序

编辑2:

我尝试在类路径上仅使用derby.jar编译您的示例,但它失败了。添加derbyclient.jar之后也可以(从JDK7开始)。