Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

Connecting to a Database in Non-Encrypted Mode

Updated on 2025-02-27 GMT+08:00

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.opengauss.jdbc.Driver");
  • Pass parameters when a JVM is started. jdbctest is the name of the test case program.
    java -Djdbc.drivers=com.huawei.opengauss.jdbc.Driver jdbctest
    NOTE:
    1. GaussDB is compatible with PG in the use of JDBC. Therefore, when two JDBC drivers are used in the same process, class names may conflict.
    2. Compared with the PG driver, the 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

Database connection descriptor when opengaussjdbc.jar is used. The format is as follows:

  • jdbc:opengauss:database
  • jdbc:opengauss://host/database
  • jdbc:opengauss://host:port/database
  • jdbc:opengauss://host:port/database?param1=value1&param2=value2
  • jdbc:opengauss://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.

    For security purposes, the 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 the service system be deployed outside the database. If it is deployed inside, database performance may be affected.

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

  • port indicates the port number of the database server.

    By default, the database on port 5431 of the localhost is connected.

  • param indicates a database connection attribute.

    The parameter can be configured in the 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 property. For details about all parameters, see Connection Parameter Reference.

user

Database user.

password

Password of the 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 Interface 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:opengauss://$ip:$port/database";
    String userName = System.getenv("EXAMPLE_USERNAME_ENV");
    String password = System.getenv("EXAMPLE_PASSWORD_ENV");

  3. Load the driver.

    1. Add the opengaussjdbc.jar package to the code running tool (such as IDE).
    2. Load the database driver com.huawei.opengauss.jdbc.Driver as follows:
      String driver = "com.huawei.opengauss.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 property values of a Properties object. In addition, you need to import other APIs and classes based on the actual application scenario. For details, see JDBC Interface 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:opengauss://$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 property values of the object.

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

  4. Load the driver.

    1. Add the opengaussjdbc.jar package to the code running tool (such as IDE).
    2. Load the database driver com.huawei.opengauss.jdbc.Driver as follows:
    String driver = "com.huawei.opengauss.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);
    

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback