gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:59:42 25 4
gpt4 key购买 nike

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

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

}

}

最佳答案

首先使用以下方法加载类:

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

编辑:

事实证明,JavaDB 在 JDK 中并不可用。参见 http://db.apache.org/derby/integrate/plugin_help/derby_app.html#Changing+the+application+to+use+the+Derby+Embedded+Driver .

To use Derby in its embedded mode set your CLASSPATH to include the jar files listed below: derby.jar: contains the Derby engine and the Derby Embedded JDBC driver

编辑 2:

我试过在类路径中仅使用 derby.jar 来编译您的示例,但它失败了。添加 derbyclient.jar 后它也可以工作(来自 JDK7)。

关于java - 尝试运行创建 Java 数据库的程序时出错。我错过了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20452608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com