Help Center/
GaussDB/
Best Practices/
Best Practices for JDBC/
Best Practices for JDBC (Distributed Instances)/
User-defined Type/
Procedure/
Complete Example
Updated on 2025-09-04 GMT+08:00
Complete Example
import com.huawei.gaussdb.jdbc.jdbc.PgArray; import com.huawei.gaussdb.jdbc.util.PGobject; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Types; import java.sql.DriverManager; public class TypesTest { public static Connection getConnection() throws ClassNotFoundException, SQLException { String driver = "com.huawei.gaussdb.jdbc.Driver"; // Specify the source URL of the database. (Adjust $ip, $port, and database based on the actual services.) String sourceURL = "jdbc:gaussdb://$ip:$port/database"; // Obtain the username and password from the environment variables. String userName = System.getenv("EXAMPLE_USERNAME_ENV"); String password = System.getenv("EXAMPLE_PASSWORD_ENV"); Class.forName(driver); return DriverManager.getConnection(sourceURL, userName, password); } public static void main(String[] args) { try { Connection conn = getConnection(); Statement statement = conn.createStatement(); statement.execute("set behavior_compat_options='proc_outparam_override'"); statement.close(); CallableStatement cs = conn.prepareCall("{CALL PUBLIC.TEST_PROC(?,?,?,?)}"); // Set 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. 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 output parameters. // 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"); 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); } cs.close(); conn.close(); } catch (ClassNotFoundException | SQLException e) { throw new RuntimeException(e); } } }
Result Verification
Data of user-defined types is correctly obtained in Complete Example. Below are the execution results for Complete Example:
1 demo (10,demo10) (11,demo111)
Rollback Method
N/A
Parent topic: Procedure
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.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot