1. preparedstatement is pre-compiled, which greatly improves the efficiency of batch processing. It is also called the JDBC stored procedure.
2. Use the statement object.When only one-time access is performed to the database, the statement object is used for processing. The overhead of the preparedstatement object is greater than that of the statement object, which does not bring additional benefits to one-time operations.
3. Each time statement executes an SQL statement, the relevant database must compile the SQL statement. preparedstatement is pre-compiled, and preparedstatement supports batch processing.
4.Code fragment 1: String updatestring = "Update coffees set sales = 75" + "where cof_name like 'Colombian ′";
Stmt.exe cuteupdate (updatestring );
Code fragment 2:
Preparedstatement updatesales = con. preparestatement ("Update coffees set sales =? Where cof_name like? ");
Updatesales. setint (1, 75 );
Updatesales. setstring (2, "Colombian ");
Updatesales.exe cuteupdate ();
The difference between Segment 2 and segment 1 is that the latter uses the preparedstatement object, while the former is a common statement object. The preparedstatement object not only contains the SQL statement, but also has been pre-compiled in most cases. Therefore, when executing the statement, you only need to run the SQL statement in the DBMS instead of compiling it first. When you need to execute the statement object multiple times, the preparedstatement object will greatly reduce the running time and, of course, speed up database access.
This conversion also brings you great convenience. You do not have to repeat the syntax of SQL statements. Instead, you only need to change the value of the variable to execute the SQL statement again. Whether the preparedstatement object is selected depends on whether the SQL statements with the same syntax are executed multiple times, and the difference between the two statements is only the difference between variables. If it is executed only once, it should be no different from ordinary objects, and it does not reflect the superiority of its pre-compilation.
5. JDBC programs that execute many SQL statements generate a large number of statement and preparedstatement objects. The preparedstatement object is generally considered to be more effective than the statement object, especially when the same SQL statement with different parameters is executed multiple times. The preparedstatement object allows the database to pre-compile SQL statements, saving time and increasing code readability in subsequent operations.
However, in the Oracle environment, developers are actually more flexible. When statement or preparedstatement object is used, the Oracle database caches SQL statements for later use. In some cases, execution of the preparedstatement object takes a longer time because the drive itself requires additional processing and added network activity between the Java application and the Oracle server.
However, in addition to buffering, there is at least one better reason why we prefer to use the preparedstatement object in enterprise applications, that isSecurity.Parameters passed to the preparedstatement object can be forcibly converted, so that developers can ensure that they match the underlying database format when inserting or querying data..
When executing SQL commands, we have two options: You can use the preparedstatement object or the statement object. No matter how many times you use the same SQL command, preparedstatement only parses and compiles it once. When a statement object is used, it is parsed and compiled every time an SQL command is executed.