Eclipse存储我用Java创建的Derby数据库文件在哪里?

时间:2016-09-19 03:02:59

标签: java eclipse derby

我已经搜索了一会儿,无法找到Eclipse Neon存储我使用Derby和Java创建的数据库文件的位置。较旧的文档显示了一个" Apache Derby"上下文菜单中的选项,但这对我来说并不适用。

在较旧的文档中,我还看到了一些关于Derby的IJ SQL接口的内容。我还想知道这个IJ接口是否仍然可用于Eclipse Neon,以及如何打开它。

谢谢!

1 个答案:

答案 0 :(得分:1)

我不认为eclipse可以存储数据库,但您可以在eclipse中编写Java代码,创建Derby数据库,表等,以及您可以使用Derby数据库的ij工具创建一个数据库。

如何创建Derby数据库以及创建的数据库保存位置的两种方法。

**默认情况下,自动将数据库保存在运行startNetworkServer.bat的位置。

从java代码(eclipse)创建数据库的示例:

public class CreateDatabase {
    public static void main(String[] args) {

        String driverName = "org.apache.derby.jdbc.ClientDriver";

        // Creating all tables for database DDL for TABLES
        try {
        Class.forName(driverName);

        System.out.println("driver loaded");

        String url = "jdbc:derby://localhost:1527/MyDB;create=true";

        // ARM technology
        try (Connection con = DriverManager.getConnection(url);) {

            // get a connection to a specific database (inside ARM)
            System.out.println("connected to: " + con);
            Statement stmt = con.createStatement();

            // create a Statement for all tables

            String companyTable = "CREATE TABLE Company"
                    + "("
                    + "COMPANY_ID BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"
                    + "COMPANY_NAME VARCHAR(20),"
                    + "COMPANY_PASSWORD VARCHAR(16),"
                    + "EMAIL VARCHAR(30),"
                    + "CONSTRAINT PRIME_COMPANY_ID PRIMARY KEY (COMPANY_ID)"
                    + ")";

            stmt.executeUpdate(companyTable);
            System.out.println("success: " + companyTable);

            String customerTable = "CREATE TABLE Customer"
                    + "("
                    + "CUSTOMER_ID BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"
                    + "CUSTOMER_NAME VARCHAR(25),"
                    + "CUSTOMER_PASSWORD VARCHAR(16),"
                    + "CONSTRAINT PRIME_CUSTOMER_ID PRIMARY KEY (CUSTOMER_ID)"
                    + ")";
            stmt.executeUpdate(customerTable);
            System.out.println("success: " + customerTable);


            System.out.println("The database MyDB created successfully");

            }

            System.out.println("connection closed");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

    }
}

从(Derby)的ij-tool创建数据库的示例: 转到Derby数据库的bin目录和 -
1.运行startNetworkServer.bat
2.运行ij.bat
3.在ij-tool中写connect 'jdbc:derby://localhost:1527/MyDB;create=true';
在第3步中,我们创建了一个名为MyDB的数据库,它出现在这个 - bin目录中。

创建数据库后,您可以创建表... https://db.apache.org/derby/docs/10.7/ref/rrefsqlj24513.html#rrefsqlj24513