共用方式為


Azure Database for PostgreSQL libraries for Java

Overview

Azure Database for PostgreSQL is a relational database service in Azure built for developers based on the community version of open source PostgreSQL database engine.

To get started with Azure Database for PostgreSQL, see Use Java to connect and query data.

Client JDBC driver

Connect to Azure Database for PostgreSQL from your applications using the open-source PostgreSQL JDBC driver. You can use the Java JDBC API to directly connect to the database or use data access frameworks that interact with the database through JDBC such as Hibernate.

Add a dependency to your Maven pom.xml file to use the client JDBC driver in your project.

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.1.1</version>
</dependency>

Example

Connect to Azure Database for PostgreSQL using JDBC and select all records in the sales table. You can get the JDBC connection string for the database from the Azure Portal.

String url = String.format("jdbc:postgresql://[your-database-hostname].postgres.database.azure.com:5432/[your-database-name]?user=[your-username]@[your-database-hostname]&password=[your-password]&ssl=true");
Connection connection = null;
try {
    connection = DriverManager.getConnection(url);
    String selectSql = "SELECT * FROM SALES";
    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery(selectSql);
}

Samples

Design a PostgreSQL database using the Azure CLI

Explore more sample Java code for Azure Database for PostgreSQL you can use in your apps.