Updated on 2025-05-29 GMT+08:00

Connecting to a Database in Non-Encrypted Mode

To connect to a database in non-encrypted mode, you need to load a driver and then establish a database connection. This section describes methods of loading drivers, APIs for creating database connections, and APIs for non-encrypted connections.

Methods of Loading Drivers

You can load drivers in the following ways:
  • Implicitly load a driver at any position in code before creating a connection.
    Class.forName("com.huawei.gaussdb.jdbc.Driver");
  • Pass parameters when a JVM is started. jdbctest is the name of the test case program.
    java -Djdbc.drivers=com.huawei.gaussdb.jdbc.Driver jdbctest;
  1. GaussDB is PG-compatible in the use of JDBC. Therefore, when two JDBC drivers are used in the same process, class names may conflict.
  2. GaussDB JDBC driver has the following enhanced features:
    1. SHA-256 is supported for encrypted login.
    2. The third-party log framework that implements the sf4j API can be connected.
    3. DR failover is supported.

APIs for Creating Database Connections

JDBC provides three APIs for creating database connections. For details about the url, info, user, and password parameters, see Table 1.

API 1: DriverManager.getConnection(String url). You need to write a database username and password in a URL, which is insecure and not recommended.

API 2: DriverManager.getConnection(String url, String user, String password). For details, see Connecting to a Database Using API 2.

API 3: DriverManager.getConnection(String url, Properties info). For details, see Connecting to a Database Using API 3.

Table 1 Database connection parameters

Parameter

Description

url

gaussdbjdbc.jar database connection descriptor.

If host is set to a server name or an IPv4 address, formats are as follows:

  • jdbc:gaussdb: (If the database name is left empty, the username is used.)
  • jdbc:gaussdb:database
  • jdbc:gaussdb://host/database
  • jdbc:gaussdb://host:port/database
  • jdbc:gaussdb://host:port/database?param1=value1&param2=value2
  • jdbc:gaussdb://host1:port1,host2:port2/database?param1=value1&param2=value2

If host is set to an IPv6 address, formats are as follows:

  • jdbc:gaussdb: (If the database name is left empty, the username is used.)
  • jdbc:gaussdb:database
  • jdbc:gaussdb://host/database or jdbc:gaussdb://[host]/database
  • jdbc:gaussdb://[host]:port/database
  • jdbc:gaussdb://[host]:port/database?param1=value1&param2=value2
  • jdbc:gaussdb://[host1]:port1,[host2]:port2/database?param1=value1&param2=value2
NOTE:
  • database indicates the name of the database to connect.
  • host indicates the name or IP address of the database server. Both IPv4 and IPv6 addresses are supported.

    For security purposes, a primary database node forbids access from other nodes in the database without authentication. To access the primary database node from inside the database, deploy the JDBC program on the host where the primary database node is located and set host to 127.0.0.1. Otherwise, the error message "FATAL: Forbid remote connection with trust method!" may be displayed.

    It is recommended that a service system be deployed outside the database. Otherwise, the database performance may be affected.

    By default, the localhost is used to connect to the server.

  • port indicates the port number of the database server.

    By default, the database on port 5432 is connected.

  • If host is set to an IPv6 address and the port number is specified in a URL, use square brackets ([]) to enclose the IP address. The format is [IP address]:Port number.
  • param indicates a database connection attribute. The parameter can be configured in a URL. The URL starts with a question mark (?), uses an equal sign (=) to assign a value to the parameter, and uses an ampersand (&) to separate parameters.
  • value indicates the database connection attribute values.
  • The connectTimeout and socketTimeout parameters must be set for connection. If they are not set, the default value 0 is used, indicating that the connection will not time out. When the network between a DN and the client is faulty, the client cannot receive the ACK packet from the DN and retries transmission repeatedly. A timeout error is reported only when the number of retransmission times reaches 15 (the default value). As a result, the RTO is high.
  • You are advised to ensure the validity of the URL when using the standard JDBC API to establish a connection. An invalid URL may cause an exception, and the exception contains the original URL character string, which may cause sensitive information leakage.

info

Database connection attribute. For details about all parameters, see Connection Parameter Reference.

user

Database username.

password

Password of a database user.

Connecting to a Database Using API 2

Use the DriverManager.getConnection(String url, String user, String password) API to establish a database connection. The commands are as follows:

  1. Import java.sql.Connection and java.sql.DriverManager.

    java.sql.Connection is a database connection API. The getConnection() method of java.sql.DriverManager is used to connect applications to a database. In addition, you need to import other APIs and classes based on the actual application scenario. For details, see JDBC API Reference.
    1
    2
    import java.sql.Connection;
    import java.sql.DriverManager;
    

  2. Specify the database sourceURL (change $ip, $port, and database as required), username, and password.

    Writing the username and password to code has great security risks. You are advised to store the username and password in environment variables.
    String sourceURL = "jdbc:gaussdb://$ip:$port/database";
    String userName = System.getenv("EXAMPLE_USERNAME_ENV");
    String password = System.getenv("EXAMPLE_PASSWORD_ENV");

  3. Load the driver.

    1. Add the gaussdbjdbc.jar package to the code running tool (such as IDE).
    2. Load the database driver com.huawei.gaussdb.jdbc.Driver as follows:
      String driver = "com.huawei.gaussdb.jdbc.Driver";
      Class.forName(driver);

  4. Establish a database connection.

    Call DriverManager.getConnection(String url, String user, String password) to connect to the database.
    Connection conn = DriverManager.getConnection(sourceURL, userName, password);

Connecting to a Database Using API 3

Use the DriverManager.getConnection(String url, Properties info) API to establish a database connection. The commands are as follows:

  1. Import java.sql.Connection, java.sql.DriverManager, and java.util.Properties.

    The setProperty() method of java.util.Properties is used to set attribute values of the Properties object. In addition, you need to import other APIs and classes based on the actual application scenario. For details, see JDBC API Reference.
    1
    2
    3
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.util.Properties;
    

  2. Specify the database sourceURL (change $ip, $port, and database as required), username, and password.

    Writing the username and password to code has great security risks. You are advised to store the username and password in environment variables.
    String sourceURL = "jdbc:gaussdb://$ip:$port/database";
    String userName = System.getenv("EXAMPLE_USERNAME_ENV");
    String password = System.getenv("EXAMPLE_PASSWORD_ENV");

  3. Create a Properties object and set userName and password as the attribute values of the object.

    Properties info = new Properties();
    info.setProperty("user", userName);
    info.setProperty("password", password);

  4. Load the driver.

    1. Add the gaussdbjdbc.jar package to the code running tool (such as IDE).
    2. Load the database driver com.huawei.gaussdb.jdbc.Driver as follows:
    String driver = "com.huawei.gaussdb.jdbc.Driver";
    Class.forName(driver);

  5. Establish a database connection.

    Call DriverManager.getConnection(String url, Properties info) to connect to the database.
    1
    Connection conn = DriverManager.getConnection(sourceURL, info);