Updated on 2025-03-13 GMT+08:00

Running SQL Statements

This section shows you the processes of creating the customer_t1 table by Running a Common SQL Statement, inserting data in batches by Executing a Prepared Statement for Insertion, update data by Executing a Prepared Statement for Update, and Creating and Calling a Stored Procedure.

Running a Common SQL Statement

SQL statements are run on applications to operate a database. Operations such as SELECT, UPDATE, INSERT, and DELETE can be performed on XML data.

The prerequisite is that you have connected to the database using the connection object conn. Create the customer_t1 table as follows:

  1. Create a statement object by calling the createStatement method of the Connection API.

    1
    Statement stmt = conn.createStatement();
    

  2. Run the SQL statement by calling the executeUpdate method of the Statement API.

    1
    int rc = stmt.executeUpdate("CREATE TABLE customer_t1(c_customer_sk INTEGER, c_customer_name VARCHAR(32));");
    

  3. Close the statement object stmt by calling the close method of the Statement API.

    1
    stmt.close();
    

  • If an execution request (not in a transaction block) received in the database contains multiple statements, the request is packed into a transaction. As VACCUUM is not supported in a transaction block, if one of the statements fails, the entire request will be rolled back.
  • Use semicolons (;) to separate statements. Stored procedures, functions, and anonymous blocks do not support multi-statement execution. When preferQueryMode is set to simple, the statement does not execute the parsing logic, and the semicolons (;) cannot be used to separate statements in this scenario.
  • The slash (/) can be used as the terminator for creating a single stored procedure, function, anonymous block, or package body. When preferQueryMode is set to simple, the statement does not execute the parsing logic, and the slash (/) cannot be used as the terminator in this scenario.
  • JDBC caches SQL statements in prepareStatement, which may cause memory bloat. If the JVM memory is small, you are advised to adjust preparedStatementCacheSizeMiB or preparedStatementCacheQueries.

Executing a Prepared Statement for Insertion

When a prepared statement processes multiple pieces of similar data, the database creates only one execution plan. This improves compilation and optimization efficiency.

The prerequisite is that the customer_t1 table has been created by executing the preceding basic SQL statements. Execute the prepared statement to insert data in batches as follows:

  1. Create the prepared statement object pst by calling the prepareStatement method of the Connection API.

    1
    PreparedStatement pst = conn.prepareStatement("INSERT INTO customer_t1 VALUES (?,?)");
    

  2. Call the corresponding API to set parameters for each piece of data and call addBatch to add SQL statements to the batch processing.

    1
    2
    3
    4
    5
    for (int i = 0; i < 3; i++) {
       pst.setInt(1, i);
       pst.setString(2, "data " + i);
       pst.addBatch();
    }
    

  3. Perform batch processing by calling the executeBatch method of the PreparedStatement API.

    1
    pst.executeBatch();
    

  4. Close the prepared statement object pst by calling the close method of the PreparedStatement API.

    1
    pst.close();
    

Do not terminate a batch processing action when it is ongoing; otherwise, database performance will deteriorate. Therefore, disable automatic commit during batch processing. Manually commit several lines at a time. The statement for disabling automatic commit is as follows:

conn.setAutoCommit(false);

Executing a Prepared Statement for Update

Prepared statements are complied and optimized once but can be used in different scenarios by assigning different values. Using prepared statements improves execution efficiency. If you want to run a statement for several times, use a prepared statement.

The prerequisite is that the preceding prepared statement has been executed and data has been inserted into the customer_t1 table in batches. Update data by running a prepared SQL statement as follows:

  1. Create the prepared statement object pstmt by calling the prepareStatement method of the Connection API.

    1
    PreparedStatement pstmt = conn.prepareStatement("UPDATE customer_t1 SET c_customer_name = ? WHERE c_customer_sk = 1");
    

  2. Set parameters by calling the setString method of the PreparedStatement API.

    1
    pstmt.setString(1, "new Data");
    

  3. Execute the prepared statement by calling the executeUpdate method of the PreparedStatement API.

    1
    int rowcount = pstmt.executeUpdate();
    

  4. Close the prepared statement object pstmt by calling the close method of the PreparedStatement API.

    1
    pstmt.close();
    

After binding parameters are set in prepareStatement, a B packet or U packet is constructed and sent to the server when the SQL statement is executed. However, the maximum length of a B packet or U packet cannot exceed 1023 MB. If the data bound at a time is too large, an exception may occur because the packet is too long. Therefore, when setting binding parameters in prepareStatement, you need to evaluate and control the size of the bound data to avoid exceeding the upper packet limit.

Creating and Calling a Stored Procedure

GaussDB can call stored procedures through JDBC. The prerequisite is that the database connection is established using the connection object conn.

Create the testproc stored procedure as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Create the stored procedure (containing the out parameter) in the database as follows:
CREATE or REPLACE procedure testproc 
(
    psv_in1 in integer,
    psv_in2 in integer,
    psv_inout inout integer
)
AS
BEGIN
    psv_inout := psv_in1 + psv_in2 + psv_inout;
END;
/

Call the testproc stored procedure as follows:

  1. Create a call statement object cstmt by calling the prepareCall method of Connection.

    1
    CallableStatement cstmt = conn.prepareCall("{? = CALL testproc(?,?,?)}");
    

  2. Set parameters by calling the setInt method of CallableStatement.

    1
    2
    3
    cstmt.setInt(2, 50);
    cstmt.setInt(1, 20);
    cstmt.setInt(3, 90);
    

  3. Register an output parameter by calling the registerOutParameter method in CallableStatement.

    1
    cstmt.registerOutParameter(4, Types.INTEGER);  // Register an OUT parameter of the integer type.
    

  4. Execute SQL statements by calling the execute method of CallableStatement.

    1
    cstmt.execute();
    

  5. Obtain the out parameter by calling the getInt method of CallableStatement.

    1
    int out = cstmt.getInt(4);
    

  6. Close the call statement object cstmt by calling the close method of CallableStatement.

    1
    cstmt.close();
    

  • Some JDBC drivers support named parameters, which can be used to set parameters by name rather than sequence. If a parameter has the default value, you do not need to specify any parameter value but can use the default value directly. Even though the parameter sequence changes during a stored procedure, the application does not need to be modified. Currently, the GaussDB JDBC driver does not support this method.
  • GaussDB does not support functions containing OUT parameters, or stored procedures and function parameters containing default values.
  • When conn.prepareCall("{? = CALL testproc(?,?,?)}") is used to execute a stored procedure, you can bind parameters in the sequence of placeholders. The first parameter is registered as an output parameter. You can also bind parameters based on the parameter sequence in the stored procedure and register the fourth parameter as an output parameter. In this example, the fourth parameter is registered as an output parameter.
  • If JDBC is used to call a stored procedure whose returned value is a cursor, the returned cursor cannot be used.
  • A stored procedure and a basic SQL statement must be run separately.
  • Output parameters must be registered for parameters of the inout type in the stored procedure.

Adding Single-Shard Execution Syntaxes to Statements

The prerequisite is that the database has been connected using the conn connection object, the test table has been created, and data has been inserted into the table.

  1. Call the setClientInfo(String name,String value) method of the Connection object to set the nodeName parameter.

    conn.setClientInfo("nodeName","datanode1");

  2. Execute the SQL statements by using the executeQuery(String sql) and execute(String sql) methods in Statement and the executeQuery() and execute() methods in PreparedStatement.

    PreparedStatement pstm = conn.prepareStatement("SELECT * FROM test");
    pstm.execute();
    pstm.executeQuery();
    Statement stmt=conn.createStatement();
    stmt.execute("SELECT * FROM test");
    stmt.executeQuery("SELECT * FROM test");

  3. Set the parameter to an empty string to disable it.

    conn.setClientInfo("nodeName","");
    • This function is adapted based on the single-shard execution function of the kernel. Therefore, before using this function, check whether the database kernel supports single-shard execution.
    • If the nodeName parameter is enabled, you need to manually disable the parameter after executing SQL statements. Otherwise, the execution of other query statements will be affected.
    • Once the parameter is enabled, all statements of the current connection will be executed on the specified DN.
    • After the parameter is enabled, the cache mechanism of PreparedStatement will be affected, cached statements will be cleared, and subsequent statements executed for single-shard queries will not be cached until the parameter is disabled.
    • The parameter is a connection parameter. Therefore, the parameter value takes effect once. The API cannot be used to execute the statements on different shards at the same time.