Executing SQL Statements
Executing Regular SQL Statements
To manipulate database data by executing SQL statements (statements without parameter passing), an application must follow these steps:
Call the createStatement method of
Connectionto create a statement object.javaConnection conn = DriverManager.getConnection("url","user","password"); Statement stmt = conn.createStatement();Call the executeUpdate method of
Statementto execute the SQL statement.javaint rc = stmt.executeUpdate("CREATE TABLE customer_t1(c_customer_sk INTEGER, c_customer_name VARCHAR(32));");Close the statement object.
javastmt.close();
Executing Prepared SQL Statements
A prepared statement is compiled and optimized only once, and can then be reused multiple times with different parameter values. Since it is precompiled, subsequent executions take less time. Therefore, if a statement is to be executed multiple times, use a prepared statement.
Call the prepareStatement method of
Connectionto create a prepared statement object.javaPreparedStatement pstmt = con.prepareStatement("UPDATE customer_t1 SET c_customer_name = ? WHERE c_customer_sk = 1");Call the setShort method of
PreparedStatementto set parameters.javapstmt.setShort(1, (short)2);Call the executeUpdate method of
PreparedStatementto execute the prepared SQL statement.javaint rowcount = pstmt.executeUpdate();Call the close method of
PreparedStatementto close the prepared statement object.javapstmt.close();
Calling Stored Procedures
oGRAC supports directly calling pre-created stored procedures through JDBC. The steps are as follows:
Call the prepareCall method of
Connectionto create a call statement object.javaConnection myConn = DriverManager.getConnection("url","user","password"); CallableStatement cstmt = myConn.prepareCall("{? = CALL TESTPROC(?,?,?)}");Call the setInt method of
CallableStatementto set parameters.javacstmt.setInt(2, 50); cstmt.setInt(1, 20); cstmt.setInt(3, 90);Call the registerOutParameter method of
CallableStatementto register output parameters.javacstmt.registerOutParameter(4, Types.INTEGER); // Register the OUT parameter of integer type.Call the execute method of
CallableStatementto execute the call.javacstmt.execute();Call the getInt method of
CallableStatementto obtain the output parameter.javaint out = cstmt.getInt(4); // Obtain the OUT parameter.Call the close method of
CallableStatementto close the call statement.javacstmt.close();
Executing Batch Processing
When processing multiple similar data entries with a single prepared statement, the database creates the execution plan only once, saving statement compilation and optimization time. The execution can be performed as follows:
Call the prepareStatement method of
Connectionto create a prepared statement object.javaConnection conn = DriverManager.getConnection("url","user","password"); PreparedStatement pstmt = conn.prepareStatement("INSERT INTO customer_t1 VALUES (?)");For each data entry, call setShort to set the parameters, and call addBatch to add the entry to the parameter list.
javapstmt.setShort(1, (short)2); pstmt.addBatch();Call the executeBatch method of
PreparedStatementto execute batch processing.javaint[] rowcount = pstmt.executeBatch();Call the close method of
PreparedStatementto close the prepared statement object.javapstmt.close();NOTE
In actual batch processing, do not terminate the execution of the batch processing program, as this will degrade database performance. Therefore, when executing batch processing, you should disable the auto-commit feature and commit every few rows. The statement to disable auto-commit is: conn.setAutoCommit(false);