Connect to Database Through Intellij Data Sources and Drivers – 通过 Intellij 数据源和驱动程序连接数据库

最后修改: 2023年 12月 10日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Connecting to a database through IntelliJ IDEA involves configuring a data source and selecting the appropriate database driver.

通过 IntelliJ IDEA 连接数据库需要配置数据源并选择适当的数据库驱动程序。

In this tutorial, we’ll learn how to connect to a database through IntelliJ data sources and drivers.

在本教程中,我们将学习如何通过 IntelliJ 数据源和驱动程序连接到数据库

2. Enable the Database Tools and SQL Plugin

2.启用数据库工具和 SQL 插件

The Database Tools and SQL plugin is usually enabled by default in IntelliJ IDEA Ultimate. However, if we encounter a situation where it’s not enabled, we can follow these steps to ensure it’s enabled:

Database Tools and SQL plugin 通常在 IntelliJ IDEA Ultimate 中默认启用。但是,如果遇到未启用的情况,我们可以按照以下步骤确保启用该插件:

  1. First, let’s open IntelliJ IDEA and then navigate to “File” -> “Settings” (on Windows/Linux) or “IntelliJ IDEA” -> “Preferences” (on macOS)
  2. Once the Settings/Preferences dialog pops up, we can navigate to “Plugins”
  3. Now we need to look for the “Database Tools and SQL” plugin in the list of installed plugins
  4. If the “Database Tools and SQL” plugin is not already checked, we’ll need to check it
  5. If the plugin is not installed, we must click on the “Marketplace” tab and search for “Database Tools and SQL” to install it from there
  6. After enabling or installing the plugin, we may need to restart IntelliJ IDEA to apply the changes:

restart IntelliJ

3. Configuring the Data Source

3.配置数据源

Let’s see how to use the database view in IntelliJ IDEA, which allows an easy data source configuration. For demonstration purposes, we’ll use the PostgreSQL database.

让我们看看如何在 IntelliJ IDEA 中使用数据库视图,它允许轻松配置数据源。为便于演示,我们将使用 PostgreSQL 数据库。

3.1. Open the Database Tool Window

3.1.打开数据库工具窗口

First, let’s open the “Database Tool Window”. To do so, we can simply navigate to the “View” menu and select “Tool Windows” -> “Database” or use the shortcut Alt + 8 (Windows/Linux) or ⌘ + 8 (Mac):

首先,让我们打开 “数据库工具窗口”。为此,我们只需导航到 “视图 “菜单,然后选择 “工具窗口”->”数据库”,或使用快捷键 Alt + 8 (Windows/Linux) 或 ⌘ + 8 (Mac):

Database Tool Window

A new Database tool window will open and must be visible on IntelliJ IDEA:

将打开一个新的数据库工具窗口,该窗口必须在 IntelliJ IDEA 上可见:

IntelliJ IDEA tool window

3.2. Add a Data Source

3.2.添加数据源

Second, in the Database tool window:

第二,在数据库工具窗口中:

  1. Let’s click on the “+” icon and then on “Data Source”, or right-click in the window and choose “New” and then “Data Source”
  2. Next, we can select the appropriate database type from the list (e.g., MySQL, PostgreSQL, Oracle):

Database Type

3.3. Configure Connection Details

3.3.配置连接详情

After choosing a database, a new window “Data Sources and Drivers” pops up. We can now configure the connection details for our database:

选择数据库后,会弹出一个新窗口 “数据源和驱动程序”。现在我们可以配置数据库的连接细节:

Data Sources and Drivers

We need now to click on “Test Connection” to ensure that IntelliJ IDEA can successfully connect to the database. Once the connection is successfully established, the last step is to click on the “OK” button to save the data source configuration.

现在,我们需要点击 “测试连接”,以确保 IntelliJ IDEA 能够成功连接到数据库。连接成功建立后,最后一步是点击 “确定 “按钮保存数据源配置。

4. Configuring the Database Driver

4.配置数据库驱动程序

In order to properly connect a database to IntelliJ IDEA, we typically need to download a JDBC driver. Luckily, in IntelliJ, we can simply click on the “Download” link next to the “Driver” field to download the appropriate database driver:

为了将数据库正确连接到 IntelliJ IDEA,我们通常需要下载一个 JDBC 驱动程序。幸运的是,在 IntelliJ 中,我们只需点击 “驱动程序 “字段旁边的 “下载 “链接,即可下载相应的数据库驱动程序:

Driver

If the database driver is not bundled with IntelliJ IDEA, we’ll need to download it separately and configure the driver manually as follows:

如果数据库驱动程序没有与 IntelliJ IDEA 捆绑,我们需要单独下载,并按如下步骤手动配置驱动程序:

First, we need to click on the “Drivers” tab of the “Data Sources and Drivers” window:

首先,我们需要点击 “数据源和驱动程序 “窗口中的 “驱动程序 “选项卡:

Data sources and drivers

Second, let’s click on the “+” icon to add a new driver.

其次,点击 “+”图标添加新的驱动程序。

Finally, we can choose the appropriate driver we already downloaded for the database type and fill in the required details. The appropriate driver class information can usually be found in the documentation for our specific database:Driver Class Information

最后,我们可以为数据库类型选择已下载的相应驱动程序,并填写所需的详细信息。相应的驱动程序类别信息通常可以在特定数据库的文档中找到:驱动程序类别信息

5. Connect and Explore the Database

5.连接并探索数据库

At this step, we can find our newly added data source along with all created data sources in the Database tool window. Furthermore, a green dot appears next to the Data sources icons that have an active connection to a database.

在这一步,我们可以在数据库工具窗口中找到新添加的数据源和所有已创建的数据源。此外,与数据库有活动连接的数据源图标旁边会出现一个绿点。

Data Sources Icons

Once connected, we can explore the database structure, execute SQL queries, and perform other database-related tasks directly from IntelliJ IDEA.

连接后,我们就可以直接从 IntelliJ IDEA 探索数据库结构、执行 SQL 查询和其他与数据库相关的任务。

6. Conclusion

6.结论

In this tutorial, we learned how to connect to a database through IntelliJ data sources and drivers. Remember that the exact steps may vary slightly based on the database type and version, as well as the specific IntelliJ IDEA version.

在本教程中,我们学习了如何通过 IntelliJ 数据源和驱动程序连接到数据库。请记住,具体步骤可能会因数据库类型和版本以及特定的 IntelliJ IDEA 版本而略有不同。