Executing SQL Statements
Executing an Ordinary SQL Statement
The application performs data (parameter statements do not need to be transferred) in the database by running SQL statements, and you need to perform the following steps:
- Create a statement object by triggering the createStatement method in Connection.
- Execute the SQL statement by triggering the executeUpdate method in Statement.
If an execution request (not in a transaction block) received in the database contains multiple statements, the request is packed into a transaction. VACUUM is not supported in a transaction block. If one of the statements fails, the entire request will be rolled back.
- Close the statement object.
Executing a Prepared SQL Statement
Pre-compiled statements were once complied and optimized and can have additional parameters for different usage. For the statements have been pre-compiled, the execution efficiency is greatly improved. If you want to execute a statement for several times, use a precompiled statement. Perform the following procedure:
- Create a prepared statement object by calling the prepareStatement method in Connection.
- Set parameters by triggering the setShort method in PreparedStatement.
- Execute the precompiled SQL statement by triggering the executeUpdate method in PreparedStatement.
- Close the precompiled statement object by calling the close method in PreparedStatement.
Calling a Stored Procedure
Perform the following steps to call existing stored procedures through the JDBC interface in GaussDB(DWS):
- Create a call statement object by calling the prepareCall method in Connection.
- Set parameters by calling the setInt method in CallableStatement.
- Register with an output parameter by calling the registerOutParameter method in CallableStatement.
- Call the stored procedure by calling the execute method in CallableStatement.
- Obtain the output parameter by calling the getInt method in CallableStatement.
For example:
- Close the call statement by calling the close method in CallableStatement.
- Many database classes such as Connection, Statement, and ResultSet have a close() method. Close these classes after using their objects. Close these actions after using their objects. Closing Connection will close all the related Statements, and closing a Statement will close its ResultSet.
- Some JDBC drivers support named parameters, which can be used to set parameters by name rather than sequence. If a parameter has a 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(DWS) JDBC driver does not support this method.
- GaussDB(DWS) does not support functions containing OUT parameters, or default values of stored procedures and function parameters.
- 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 an SQL statement must be executed separately.
Batch Processing
When a prepared statement batch processes multiple pieces of similar data, the database creates only one execution plan. This improves the compilation and optimization efficiency. Perform the following procedure:
- Create a prepared statement object by calling the prepareStatement method in Connection.
- Call the setShort parameter for each piece of data, and call addBatch to confirm that the setting is complete.
- Execute batch processing by calling the executeBatch method in PreparedStatement.
- Close the precompiled statement object by calling the close method in PreparedStatement.
Do not terminate a batch processing action when it is ongoing; otherwise, the database performance will deteriorate. Therefore, disable the automatic submission function during batch processing, and manually submit every several lines. The statement for disabling automatic submission is conn.setAutoCommit(false).
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.