In GaussDB(DWS), you can use a JDBC driver to connect to a database on Linux or Windows. The driver can connect to the database through an ECS on the cloud platform or over the Internet.
When using the JDBC driver to connect to the data warehouse cluster, determine whether to enable SSL authentication. SSL authentication is used to encrypt communication data between the client and the server. It safeguards sensitive data transmitted over the Internet. You can download a self-signed certificate file on the GaussDB(DWS) management console. To make the certificate take effect, you must configure the client program using the OpenSSL tool and the Java keytool.
The SSL mode delivers higher security than the common mode. You are advised to enable SSL connection when using JDBC to connect to a GaussDB(DWS) cluster.
For details about how to use the JDBC API, see the official documentation.
GaussDB(DWS) also supports open-source JDBC driver: PostgreSQL JDBC 9.3-1103 or later.
The procedure for connecting to the database using a JDBC driver in a Linux environment is similar to that in a Windows environment. The following describes the connection procedure in a Windows environment.
You are advised to store the certificate file in a path of the English version and can specify the actual path when configuring the certificate. If the path is incorrect, a message stating that the file does not exist will be prompted.
openssl x509 -in cacert.pem -out cacert.crt.der -outform der keytool -keystore mytruststore -alias cacert -import -file cacert.crt.der
Enter the truststore password as prompted and answer y.
openssl pkcs12 -export -out client.pkcs12 -in client.crt -inkey client.key
Enter the client private key password Gauss@MppDB. Then enter and confirm the self-defined private key password.
keytool -importkeystore -deststorepass Gauss@MppDB -destkeystore client.jks -srckeystore client.pkcs12 -srcstorepass Password -srcstoretype PKCS12 -alias 1
Take the Eclipse project as an example. Store the JAR file to the project directory, for example, the lib directory in the project directory. In the Eclipse project, right-click the JAR file in the lib directory and choose Build Path to reference the JAR file.
The following methods are available:
The JDBC driver package downloaded on GaussDB(DWS)contains gsjdbc.jar.
The JDBC API does not provide the connection retry capability. You need to implement the retry processing in the service code.
DriverManager.getConnection() methods:
Parameter |
Description |
---|---|
url |
Specifies the database connection descriptor, which can be viewed on the management console. For details, see Obtaining the Cluster Connection Address. The URL format is as follows:
NOTE:
|
info |
Specifies database connection properties. Common properties include the following:
|
user |
Specifies the database user. |
password |
Specifies the password of the database user. |
The following describes the sample code used to encrypt the connection using the SSL certificate:
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 37 38 39 40 41 42 43 44 45 46 | // The following code obtains the database SSL connection operation and encapsulates the operation as an API. public static Connection GetConnection(String username, String passwd) { //Define the driver class. String driver = "org.postgresql.Driver"; //Set keyStore. System.setProperty("javax.net.ssl.trustStore", "mytruststore"); System.setProperty("javax.net.ssl.keyStore", "client.jks"); System.setProperty("javax.net.ssl.trustStorePassword", "password"); System.setProperty("javax.net.ssl.keyStorePassword", "password"); Properties props = new Properties(); props.setProperty("user", username); props.setProperty("password", passwd); props.setProperty("ssl", "true"); String url = "jdbc:postgresql://" + "10.10.0.13" + ':' + "8000" + '/' + "gaussdb"; Connection conn = null; try { //Load the driver. Class.forName(driver); } catch( Exception e ) { e.printStackTrace(); return null; } try { //Create a connection. conn = DriverManager.getConnection(url, props ); System.out.println("Connection succeed!"); } catch(Exception e) { e.printStackTrace(); return null; } return conn; } |
1 | Statement stmt = con.createStatement(); |
1 | int rc = stmt.executeUpdate("CREATE TABLE tab1(id INTEGER, name VARCHAR(32));"); |
1 | stmt.close(); |
This code sample illustrates how to develop applications based on the JDBC API provided by GaussDB(DWS).
Before completing the following example, you need to create a stored procedure. For details, see "Tutorial: Development Using JDBC or ODBC" in the Data Warehouse Service (DWS) Developer Guide.
1 2 3 4 5 6 7 8 9 10 11 | create or replace procedure testproc ( psv_in1 in integer, psv_in2 in integer, psv_inout in out integer ) as begin psv_inout := psv_in1 + psv_in2 + psv_inout; end; / |
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | //DBtest.java //gsjdbc4.jar is used as an example. //Demonstrate the main steps for JDBC development, including creating databases, creating tables, and inserting data. import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; import java.sql.CallableStatement; import java.sql.Types; public class DBTest { //Create a database connection. Replace the following IP address and database with the actual database connection address and database name. public static Connection GetConnection(String username, String passwd) { String driver = "org.postgresql.Driver"; String sourceURL = "jdbc:postgresql://10.10.0.13:8000/database"; Connection conn = null; try { // Load the database driver. Class.forName(driver).newInstance(); } catch (Exception e) { e.printStackTrace(); return null; } try { //Create a database connection. conn = DriverManager.getConnection(sourceURL, username, passwd); System.out.println("Connection succeed!"); } catch (Exception e) { e.printStackTrace(); return null; } return conn; }; //Run the common SQL statements to create table customer_t1. public static void CreateTable(Connection conn) { Statement stmt = null; try { stmt = conn.createStatement(); //Run the common SQL statements. int rc = stmt .executeUpdate("CREATE TABLE customer_t1(c_customer_sk INTEGER, c_customer_name VARCHAR(32));"); stmt.close(); } catch (SQLException e) { if (stmt != null) { try { stmt.close(); } catch (SQLException e1) { e1.printStackTrace(); } } e.printStackTrace(); } } //Run the prepared statements and insert data in batches. public static void BatchInsertData(Connection conn) { PreparedStatement pst = null; try { //Generate the prepared statements. pst = conn.prepareStatement("INSERT INTO customer_t1 VALUES (?,?)"); for (int i = 0; i < 3; i++) { //Add parameters. pst.setInt(1, i); pst.setString(2, "data " + i); pst.addBatch(); } //Execute batch processing. pst.executeBatch(); pst.close(); } catch (SQLException e) { if (pst != null) { try { pst.close(); } catch (SQLException e1) { e1.printStackTrace(); } } e.printStackTrace(); } } //Run the precompiled statement to update the data. public static void ExecPreparedSQL(Connection conn) { PreparedStatement pstmt = null; try { pstmt = conn .prepareStatement("UPDATE customer_t1 SET c_customer_name = ? WHERE c_customer_sk = 1"); pstmt.setString(1, "new Data"); int rowcount = pstmt.executeUpdate(); pstmt.close(); } catch (SQLException e) { if (pstmt != null) { try { pstmt.close(); } catch (SQLException e1) { e1.printStackTrace(); } } e.printStackTrace(); } } //Execute the storage procedure. public static void ExecCallableSQL(Connection conn) { CallableStatement cstmt = null; try { cstmt=conn.prepareCall("{? = CALL TESTPROC(?,?,?)}"); cstmt.setInt(2, 50); cstmt.setInt(1, 20); cstmt.setInt(3, 90); cstmt.registerOutParameter(4, Types.INTEGER); //Register a parameter of the out type. Its value is an integer. cstmt.execute(); int out = cstmt.getInt(4); //Obtain the out parameter. System.out.println("The CallableStatment TESTPROC returns:"+out); cstmt.close(); } catch (SQLException e) { if (cstmt != null) { try { cstmt.close(); } catch (SQLException e1) { e1.printStackTrace(); } } e.printStackTrace(); } } /** * Main program, which gradually invokes each static method. * @param args */ public static void main(String[] args) { //Create a database connection. Replace User and Password with the actual database user name and password. Connection conn = GetConnection("User", "Password"); //Create a table. CreateTable(conn); //Insert data in batches. BatchInsertData(conn); //Run the precompiled statement to update the data. ExecPreparedSQL(conn); //Execute the storage procedure. ExecCallableSQL(conn); //Close the database connection. try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } |