All Products
Search
Document Center

AnalyticDB:JDBC

Last Updated:Apr 17, 2024

AnalyticDB for PostgreSQL supports connection by using the Java Database Connectivity (JDBC) driver of PostgreSQL or Greenplum. This topic describes how to use JDBC to connect to an AnalyticDB for PostgreSQL instance.

Prerequisites

  • The internal or public endpoint of an AnalyticDB for PostgreSQL instance is obtained.

    • If your client is deployed on an Elastic Compute Service (ECS) instance that resides in the same region and uses the same network type as the AnalyticDB for PostgreSQL instance, you can use the internal endpoint to connect to the AnalyticDB for PostgreSQL instance. Log on to the AnalyticDB for PostgreSQL console. In the Database Connection Information section of the Basic Information page, view the internal endpoint of the instance.

    • If your client is deployed on an ECS instance that resides in a different region or uses a different network type from your AnalyticDB for PostgreSQL instance or a system outside Alibaba Cloud, you must apply for a public endpoint and then use the public endpoint to connect to the AnalyticDB for PostgreSQL instance. For information about how to apply for a public endpoint, see Manage public endpoints.

  • The IP address of your client is added to an IP address whitelist of the AnalyticDB for PostgreSQL instance. For more information, see Configure an IP address whitelist.

Procedure

  1. Use one of the following methods to download the JDBC driver:

    • Obtain the PostgreSQL JDBC driver from the PostgreSQL official website and add the driver to an environment variable.

    • Note

      AnalyticDB for PostgreSQL V7.0 requires JDBC V42.2.0 or later.

    • Obtain the Greenplum JDBC driver from the Greenplum official website and add the driver to an environment variable.

  2. Sample code:

    import java.sql.Connection;  
    import java.sql.DriverManager;  
    import java.sql.ResultSet;  
    import java.sql.SQLException;  
    import java.sql.Statement;  
    public class gp_conn {  
        public static void main(String[] args) {  
            try {  
                Class.forName("org.postgresql.Driver");  
                Connection db = DriverManager.getConnection("<jdbc:postgresql://mygpdbpub.gpdb.rds.aliyuncs.com:5432/postgres>","myusername","mypassword");  
                /*
                jdbc:postgresql://mygpdbpub.gpdb.rds.aliyuncs.com:5432/postgres>:<Endpoint of the database>
                myusername:<Name of the database account>
                mypassword:<Password of the database account>
                */
                Statement st = db.createStatement();  
                ResultSet rs = st.executeQuery("<SELECT * FROM gp_segment_configuration;>");  
                while (rs.next()) {  
                    System.out.print(rs.getString(1));  
                    System.out.print("    |    ");  
                    System.out.print(rs.getString(2));  
                    System.out.print("    |    ");  
                    System.out.print(rs.getString(3));  
                    System.out.print("    |    ");  
                    System.out.print(rs.getString(4));  
                    System.out.print("    |    ");  
                    System.out.print(rs.getString(5));  
                    System.out.print("    |    ");  
                    System.out.print(rs.getString(6));  
                    System.out.print("    |    ");  
                    System.out.print(rs.getString(7));  
                    System.out.print("    |    ");  
                    System.out.print(rs.getString(8));  
                    System.out.print("    |    ");  
                    System.out.print(rs.getString(9));  
                    System.out.print("    |    ");  
                    System.out.print(rs.getString(10));  
                    System.out.print("    |    ");  
                    System.out.println(rs.getString(11));  
                }  
                rs.close();  
                st.close();  
            } catch (ClassNotFoundException e) {  
                e.printStackTrace();  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  
    }

For more information about the JDBC driver, see the JDBC driver documentation.

References

The Greenplum official website provides tool packages that include the JDBC, ODBC, and libpq tools. The packages are easy to install and use. For more information, see the Greenplum documentation.