After a database is connected, you can execute SQL statements in the database.
If you use an open-source Java Database Connectivity (JDBC) driver, ensure that the database parameter password_encryption_type is set to 1. If the value is not 1, the connection may fail. A typical error message is "none of the server's SASL authentication mechanisms are supported." To avoid such problems, perform the following operations:
Here are the reasons why you need to perform these operations:
JDBC provides the following three database connection methods:
Parameter |
Description |
---|---|
url |
gsjdbc4.jar database connection descriptor. The descriptor format can be:
NOTE:
If gsjdbc200.jar is used, replace jdbc:postgresql with jdbc:gaussdb.
|
info |
Database connection properties. Common properties include:
|
user |
Indicates a database user. |
password |
Indicates the password of a database user. |
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 36 | //gsjdbc4.jar is used as an example. //The following code encapsulates database connection operations into an interface. The database can then be connected using an authorized username and password. public static Connection GetConnection(String username, String passwd) { //Set the driver class. String driver = "org.postgresql.Driver"; //Set the database connection descriptor. String sourceURL = "jdbc:postgresql://10.10.0.13:8000/postgres?currentSchema=test"; Connection conn = null; try { //Load the driver. Class.forName(driver); } catch( Exception e ) { e.printStackTrace(); return null; } try { //Create a connection. conn = DriverManager.getConnection(sourceURL, username, passwd); System.out.println("Connection succeed!"); } catch(Exception e) { e.printStackTrace(); return null; } return conn; }; |