Java 在 IntelliJ Idea 13 中创建/配置 Derby JDBC 客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22922740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:02:45  来源:igfitidea点击:

Creating/Configuring Derby JDBC Client in IntelliJ Idea 13

javajdbcintellij-ideaderby

提问by user2151486

Sorry for this (maybe) stupid question. I need to create some local DB in my java project so I've decided for Apache Derby Client. I am working with IntelliJ IDEA 13 Ultimateand my problem is that I don't know, how to create local database. Tutorials at Jetbrains websites aren't useful because there are articles only about connecting to the remote DB, not to the local one (or at least I didn't find them yet).

抱歉这个(也许)愚蠢的问题。我需要在我的 java 项目中创建一些本地数据库,所以我决定使用 Apache Derby Client。我正在使用IntelliJ IDEA 13 Ultimate,我的问题是我不知道如何创建本地数据库。Jetbrains 网站上的教程没有用,因为只有关于连接到远程数据库的文章,而不是连接到本地数据库的文章(或者至少我还没有找到它们)。

What have I done so far:

到目前为止我做了什么:

  1. I've tried to set the DB up by creating new remote derby data source. Screenshot with the settings: DB Settings screen
  1. 我尝试通过创建新的远程 derby 数据源来设置数据库。带有设置的屏幕截图:DB 设置屏幕

Username and password are the same: admin

用户名和密码相同: admin

  1. After clicking test connection, this error is thrown: error
  2. When I click applyand ok, it says that it's connected, but exception is still there.
  1. 点击后test connection,抛出这个错误:error
  2. 当我点击apply和 时ok,它说它已连接,但异常仍然存在。

So do you have any idea where the problem can be? I've got small confiuration class called DatabaseSetting.java

那么您知道问题出在哪里吗?我有一个名为DatabaseSetting.java 的小型配置类

package issuetrackinglite;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseSetting {

private String dbURL = "jdbc:derby://localhost:1527/MallDB;create=true";
private String user = "admin";
private String password = "admin";
private Connection connection;



public static final String CREATE_ITEMS_DB = "CREATE TABLE items (item_id INTEGER NOT NULL, item_name VARCHAR(20) NOT NULL, item_price REAL NOT NULL, multiplicity_shop INTEGER NOT NULL, multiplicity_store INTEGER NOT NULL)";
public static final String INSERT_PRODUCT = "INSERT INTO items (item_id, item_name, item_price, multiplicity_shop, multiplicity_store) VALUES (?, ?, ?, ?, ?)";
public static final String CLEAR_ITEMS_DB = "DELETE FROM items";

// -------------------------------------------------------------

protected Connection connectToDB() {

    try {

        connection = DriverManager.getConnection(dbURL, user, password);

        return connection;

    } catch (SQLException ex) {
        System.out.println("SQL exception - connectToDB(): " + ex.getMessage());
        return null;
    }

}

}

}

EDIT

编辑

Simply explained: I just need to create virtual derby database which will be created every time at the program start. I don't know, how to do it in IntelliJ. I've added DERBY_HOMEto the enviroment variables and also added path to Derby. Now this error is thrown by IntelliJ: Error window

简单解释一下:我只需要创建每次程序启动时都会创建的虚拟 derby 数据库。我不知道,如何在 IntelliJ 中做到这一点。我已经添加DERBY_HOME到环境变量中,还添加了到德比的路径。现在这个错误是由 IntelliJ 抛出的:错误窗口

Thank you very much for your help and time

非常感谢您的帮助和时间

采纳答案by user2151486

I've managed to get this work. Here are the steps that I've made to successfully configure local Derby database to work in IntelliJ Idea 13/14.

我已经成功完成了这项工作。以下是我成功配置本地 Derby 数据库以在 IntelliJ Idea 13/14 中工作的步骤。

  1. First, you need to manually start derby server (if it is not already running) before using it in IDEA. I usually do it via command prompt by typing:

    C:\Users\PcName>startNetworkServer -noSecurityManager

    This command will start derby server on port number: 1527. Make sure you have correctly set path in enviroment variables

  2. Next go to IDEA, open Databasewindow, click on the green plus sign in the upper left corner and in the dropdown menu choose: Data Source -> Derby -> Remote

  3. You can inspire with my settings provided in the screenshot in my question. You can also download Derby driver files. IDEA does it just by clicking on the downloadbutton.

    Basic template which I always use is something like that:

    Name fieldshould be in the form: Derby - YourDatabaseName;create=true@localhost

    Host fieldshould have localhostinside

    Porthas to be 1527

    Database fieldhas to be in form: YourDatabaseName;create=true

    Urc to connect:jdbc:derby://localhost:1527/YourDatabaseName;create=true

  4. (Optional) - You can specify your database user name and password. In IDEA 14 there is a checkbox Save on disk with master password protection, which I personally always leave unchecked.

  1. 首先,您需要手动启动 derby 服务器(如果它尚未运行),然后才能在 IDEA 中使用它。我通常通过命令提示符输入:

    C:\Users\PcName>startNetworkServer -noSecurityManager

    此命令将在端口号1527上启动 derby 服务器。确保您在环境变量中正确设置了路径

  2. 接下来转到 IDEA,打开数据库窗口,单击左上角的绿色加号并在下拉菜单中选择:Data Source -> Derby -> Remote

  3. 您可以通过我的问题的屏幕截图中提供的设置来激发灵感。您还可以下载 Derby 驱动程序文件。IDEA 只需单击下载按钮即可完成。

    我一直使用的基本模板是这样的:

    名称字段应采用以下格式:Derby - YourDatabaseName;create=true@localhost

    主机字段localhost里面应该有

    端口必须是1527

    数据库字段必须采用以下形式:YourDatabaseName;create=true

    urc连接:jdbc:derby://localhost:1527/YourDatabaseName;create=true

  4. (可选) - 您可以指定您的数据库用户名和密码。在 IDEA 14 中有一个复选框Save on disk with master password protection,我个人总是不选中它。

That's it. I hope this little guide will help somebody to configure Derby in Intellij IDEA

就是这样。我希望这个小指南可以帮助某人在 Intellij IDEA 中配置 Derby

回答by GreyBeardedGeek

I think that for a local (embedded) database, you would leave off the host and port in the connection url.

我认为对于本地(嵌入式)数据库,您可以在连接 url 中省略主机和端口。

The documentationsays that the url should be of the form: jdbc:derby:MallDB;create=true

文件说,URL应该是这样的形式:jdbc:derby:MallDB;create=true

回答by user7610

For embedded database, use the default/In-Memory/URL onlyselect box on the right side of the connection URL input field. When set to In-Memory, it generates the following URL, which works for me.

对于嵌入式数据库,使用default/ In-Memory/URL only上的连接网址输入栏右侧的选择框。设置为 时In-Memory,它会生成以下 URL,这对我有用。

 jdbc:derby:memory:MallDB;create=true