Connecting to the Database
After creating a database connection, you can use it to execute SQL statements and manipulate data.
Function Prototypes
JDBC offers three methods for creating database connections:
- DriverManager.getConnection(String url);
- DriverManager.getConnection(String url, Properties info);
- DriverManager.getConnection(String url, String user, String password);
Parameters
Parameter |
Description |
---|---|
url |
Database connection descriptor. The format is:
NOTE:
|
info |
Database connection properties. Commonly used properties include:
|
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// The following examples use gsjdbc4.jar. To use gsjdbc200.jar, replace the driver class name (org.postgresql with com.huawei.gauss200.jdbc in the code). // The following code encapsulates the operation of obtaining a database connection into an interface, allowing connection to the database using a provided username and password. public static Connection GetConnection() { // Driver class. String driver = "org.postgresql.Driver"; // Database connection descriptor. String sourceURL = "jdbc:fabricsql://10.10.0.13:443/fabricsql_default"; Connection conn = null; Properties properties = new Properties(); try { // Load the driver. Class.forName(driver); } catch (ClassNotFoundException e ){ e.printStackTrace(); return null; } try { properties.setProperty("workspaceId", ""); properties.setProperty("endpointId", ""); properties.setProperty("lakeformation_instance_id", ""); properties.setProperty("AccessKeyID", ""); properties.setProperty("SecretAccessKey", ""); // Establish a connection. conn = DriverManager.getConnection(sourceURL, properties); System.out.println("Connection succeed!"); } catch (SQLException e) { e.printStackTrace(); return null; } return conn; } |
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