Detailed Procedure
- Establish a database connection.
Here are suggestions for commonly used parameters in the connection string. For more detailed settings, refer to "Application Development Guide > Development Based on JDBC > Development Procedure > Connecting to a Database" in Developer Guide.
- connectTimeout: timeout interval (in seconds) for connecting to the server's OS. If the time taken for JDBC to establish a TCP connection with the database exceeds this interval, the connection will be closed. It is advisable to set this parameter based on network conditions. The default value is 0, whereas the recommended value is 2.
- socketTimeout: timeout interval (in seconds) for socket reads. If the time taken to read data streams from the server exceeds this interval, the connection will be closed. Not setting this parameter may lead to prolonged waiting times for the client in the event of abnormal database processes. It is advisable to set this parameter based only the acceptable SQL execution time for services. The default value is 0, with no specific recommended value provided.
- connectionExtraInfo: specifies whether the driver reports its deployment path, process owner, and URL connection configurations to the database. The default value is false, whereas the recommended value is true.
- logger: specifies a third-party log framework as needed by your application. It is advisable to choose one that incorporates slf4j APIs. These APIs can record JDBC logs to facilitate exception locating. The recommended value is Slf4JLogger when a third-party log framework is needed.
String url = "jdbc:gaussdb://$ip:$port/database?connectTimeout=xx&socketTimeout=xx&connectionExtraInfo=true&logger=Slf4JLogger&autoBalance=true" Connection conn = DriverManager.getConnection("url",userName,password);
- Set GUC parameters.
Execute the SQL statement SET behavior_compat_options='proc_outparam_override'; to enable proc_outparam_override, that is, overloading of stored procedures.
Statement statement = conn.createStatement(); statement.execute("SET behavior_compat_options='proc_outparam_override'"); statement.close();
- Prepare a stored procedure.
Use the Call syntax to declare the SQL statement for calling the stored procedure TEST_PROC. Then use prepareCall to prepare the statement.
CallableStatement cs = conn.prepareCall("{CALL PUBLIC.TEST_PROC(?,?,?,?)}");
- Bind input parameters.
Use PGobject to assemble data of the user-defined type. Then use prepareCall to bind input parameters.
PGobject pgObject = new PGobject(); pgObject.setType("public.compfoo"); // Set the name of a composite type. pgObject.setValue("(1,demo)"); // Bind values of the composite type, formatted as "(value1,value2)". cs.setObject(1, pgObject); pgObject = new PGobject(); pgObject.setType("public.compfoo_table"); // Set the name of a table type. pgObject.setValue("{\"(10,demo10)\",\"(11,demo111)\"}"); // Bind values of the table type, formatted as "{\"(value1,value2)\",\"(value1,value2)\",...}". cs.setObject(2, pgObject);
- Register the output type.
Use prepareCall to register the output type of the stored procedure. For the composite type, register Types.STRUCT. For the table type, register Types.ARRAY.
// Register output parameters for the composite type. cs.registerOutParameter(3, Types.STRUCT, "public.compfoo"); // Register output parameters for the table type. cs.registerOutParameter(4, Types.ARRAY, "public.compfoo_table");
- Execute SQL statements and view the results.
Call the stored procedure and view the results corresponding to the output parameters.
cs.execute(); // Obtain output parameters. // The return structure is of the user-defined type. PGobject result = (PGobject) cs.getObject(3); // Obtain output parameters. result.getValue(); // Obtain character string values of the composite type. String[] arrayValue = result.getArrayValue(); // Obtain array values of the composite type and sort them based on columns of the composite type. result.getStruct(); // Obtain the names of subtypes in the composite type and sort them based on the order in which they were created. result.getAttributes(); // Return objects of the user-defined type in each column. For the array and table types, PgArray is returned. For the user-defined type, PGobject is encapsulated. Other types of data are stored as character strings. for (String s : arrayValue) { System.out.println(s); } PgArray pgArray = (PgArray) cs.getObject(4); ResultSet rs = pgArray.getResultSet(); Object[] array = (Object[]) pgArray.getArray(); for (Object element : array) { System.out.println(element); }
- Release resources and close the database connection.
cs.close(); conn.close();
- (Optional) Handle exceptions if any.
If your program encounters any SQL exception during runtime, utilize the try-catch module to handle them and add the necessary exception handling logic for your services.
try { // Service code } catch (SQLException e) { // Exception handling logic }
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