PostgreSQL Java 教程: 连接到 PostgreSQL 数据库

八月 9, 2023

总结:在本教程中,我们将向您展示如何设置 Java 环境,下载 PostgreSQL JDBC 驱动程序,以及从 Java 程序连接到 PostgreSQL 数据库服务器。

设置 Java 开发环境

要开发 Java 程序,您需要在计算机上安装 JDK。要设置 JDK,首先,您访问 Oracle 网站下载最新的 JDK。然后,将其安装在计算机上。

PostgreSQL JDBC Download JDK and NetBeans

设置很简单,您只需要接受安装程序提供的默认参数即可设置完成。

实际上,要开发 Java 应用程序,您需要有一个好的 IDE。有许多好的 IDE 可以免费获得,例如 Eclipse,NetBeans,IntelliJ IDEA 社区版等。

在本教程中,我们将使用 NetBeans IDE 开发 Java 应用程序。您可以直接从 Oracle 网站下载 NetBeans IDE,也可以从 NetBeans 下载页面下载。

PostgreSQL JDBC - install netbean

您可以按照下面的动画图片所示的步骤安装 NetBeans IDE。

下载 PostgreSQL JDBC 驱动程序

要从 Java 程序连接到 PostgreSQL 数据库服务器,您需要具有 PostgreSQL JDBC 驱动程序。您可以在 PostgreSQL 官方网站上的下载页面下载最新版本的驱动程序。

下载的文件是一个 jar 文件。您应该将其复制到特定的文件夹,例如 C:\demo\libs,以便您可以记住它的位置,并能够在以后将其添加到您的 Java 应用程序中。

连接到 PostgreSQL 数据库服务器

首先,创建一个名为PostgreSQLJDBC的新项目,并在包net.rockdata.tutorial中创建名为App的主类。

Create a project and add the PostgreSQL JDBC Driver jar File

其次,将 PostgreSQL JDBC 驱动程序 jar 文件添加到项目中。

第三,您需要准备以下内容:

  • PostgreSQL 数据库服务器的地址,例如 localhost
  • 数据库名称,例如 dvdrental
  • 用于连接到数据库的账号的用户名和密码。

对于这些信息,您可以使用以下格式构造 PostgreSQL JDBC 连接字符串:

jdbc:postgresql://<database_host>:<port>/<database_name>

是可选的。

在我们的示例中,连接字符串为:

jdbc:postgresql://localhost/dvdrental

为了方便起见,我们可以定义 App 类的属性,用于存储连接字符串、用户和密码:

private final String url = "jdbc:postgresql://localhost/dvdrental";
private final String user = "postgres";
private final String password = "<add your password>";

若要建立与 PostgreSQL 数据库服务器的连接,请调用DriverManager类的getConnection方法。此方法会返回一个Connection对象。

下面的connect()方法连接到 PostgreSQL 数据库服务器,并返回一个Connection对象。

 public Connection connect() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("Connected to the PostgreSQL server successfully.");
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }

        return conn;
    }

连接到 PostgreSQL 数据库服务器的完整程序如下:

package net.rockdata.tutorial;

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

/**
 *
 * @author rockdata.net
 */
public class App{

    private final String url = "jdbc:postgresql://localhost/dvdrental";
    private final String user = "postgres";
    private final String password = "<add your password>";

    /**
     * Connect to the PostgreSQL database
     *
     * @return a Connection object
     */
    public Connection connect() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("Connected to the PostgreSQL server successfully.");
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }

        return conn;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        App app = new App();
        app.connect();
    }
}

PostgreSQL JDBC - Run Connecting to PostgreSQL Database Program

因此,我们可以成功连接到PostgreSQL数据库服务器。

在本教程中,我们向您展示了如何设置 Java 开发环境以使用 PostgreSQL 数据库。