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
- 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
- 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.
- Compared with the PG driver, the GaussDB JDBC driver has the following enhanced features:
- SHA-256 is supported for encrypted login.
- The third-party log framework that implements the sf4j API can be connected.
- 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.
Parameter |
Description |
---|---|
url |
Database connection descriptor when opengaussjdbc.jar is used. The format is as follows:
NOTE:
|
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:
- 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;
- 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");
- Load the driver.
- Add the opengaussjdbc.jar package to the code running tool (such as IDE).
- Load the database driver com.huawei.opengauss.jdbc.Driver as follows:
String driver = "com.huawei.opengauss.jdbc.Driver"; Class.forName(driver);
- 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:
- 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 API Reference.
1 2 3
import java.sql.Connection; import java.sql.DriverManager; import java.util.Properties;
- 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");
- 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);
- Load the driver.
- Add the opengaussjdbc.jar package to the code running tool (such as IDE).
- Load the database driver com.huawei.opengauss.jdbc.Driver as follows:
String driver = "com.huawei.opengauss.jdbc.Driver"; Class.forName(driver);
- Establish a database connection.
Call DriverManager.getConnection(String url, Properties info) to connect to the database.
1
Connection conn = DriverManager.getConnection(sourceURL, info);
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot