我无法连接到数据库 - 程序不知道 JAR 文件在哪里

I can't connect to a database - program doesn't know where JAR file is

我正在尝试复制 Cay S. Horstmann 的 Big Java 第 23 章中的示例,您可以在线找到 PDF 文件以备不时之需作为参考:http://bcs.wiley.com/he-bcs/Books?action=chapter&bcsId=7872&itemId=1118431111&chapterId=87075

我的程序应该使用 shell window 中的 java 实用工具连接到 Apache Derby 数据库,但它不起作用。我应该输入什么 shell window 来解决这个问题?

我的文件夹结构在 IntelliJ 中如下所示:

这是代码:

package Chapter23RelationalDatabases.database;

import java.io.*;
import java.sql.*;

public class TestDB {
    public static void main(String[] args) throws Exception {
        if (args.length == 0) {
            System.out.println(
                    "Usage: java -classpath driver_class_path"
                    + File.pathSeparator
                    + ". TestDB propertiesFile");
            return;
        }

        SimpleDataSource.init(args[0]);

        Connection conn = SimpleDataSource.getConnection();
        try {
            Statement stat = conn.createStatement();

            stat.execute("CREATE TABLE Test (Name VARCHAR(20))");
            stat.execute("INSERT INTO Test VALUES ('Romeo')");

            ResultSet result = stat.executeQuery("SELECT * FROM Test");
            result.next();
            System.out.println(result.getString("Name"));

            stat.execute("DROP TABLE Test");
        } finally {
            conn.close();
        }
    }
}

package Chapter23RelationalDatabases.database;

import java.io.*;
import java.sql.*;
import java.util.Properties;

public class SimpleDataSource {
    private static String url;
    private static String username;
    private static String password;

    /**
     * Initializes the data source.
     * @param fileName the name of the property file that contains
     *                 the database driver, URL, username, and password
     */
    public static void init(String fileName) throws IOException, ClassNotFoundException {
        Properties props = new Properties();
        FileInputStream in = new FileInputStream(fileName);
        props.load(in);

        String driver = props.getProperty("jdbc.driver");
        url = props.getProperty("jdbc.url");
        username = props.getProperty("jdbc.username");
        if (username == null)
            username = "";
        password = props.getProperty("jdbc.password");
        if (password == null)
            password = "";
        if (driver != null)
            Class.forName(driver);
    }

    /**
     * Gets a connection to the database.
     * @return the database connection
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, username, password);
    }
}

这是 database.properties 文件的内容:

jdbc.url=jdbc:derby:BigJavaDB;create=true

这是我尝试 运行 我的程序时的错误堆栈:

 Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:derby:BigJavaDB;create=true
 at java.sql.DriverManager.getConnection(DriverManager.java:689)
 at java.sql.DriverManager.getConnection(DriverManager.java:247)
 at Chapter23RelationalDatabases.database.SimpleDataSource.getConnection(SimpleDataSource.java:42)
 at Chapter23RelationalDatabases.database.TestDB.main(TestDB.java:20)

为什么在使用 IntelliJ 时要在 "Shell Window" 中输入任何内容?你需要将 derby jars 添加到你的 class 路径,如果你使用 Maven,你可以简单地添加:

    <!-- APACHE DERBY - for an embedded database -->
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.10.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derbyclient</artifactId>
        <version>10.10.1.1</version>
    </dependency>

    <!-- MYSQL Connector -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.28</version>
    </dependency>

否则你可以在这里自己搜索这些 http://mvnrepository.com/artifact/org.apache.derby/derby 下载 jar 并手动将它们添加到你的 class 路径,这是大多数初学者所做的。您可以使用 -cp 标志将 jar 添加到命令行上的 class 路径,但仅使用带有 java 的命令行来学习基本的编程概念。其他明智的使用像maven这样的构建工具。我认为您应该查看一些教程视频,将罐子添加到 class 带有 IntelliJ/Eclipse/Netbeans 的路径。然后学习如何使用maven,我强烈推荐作为初学者。