"Preface" in the last article, we talked about JDBC in the operation of the database, the need to create an execution object. Objects are divided into: statement and preparestatement Two species, then the two, and what kind of source.
Just start, do not understand the time, go to the internet to see, top will preparestatement the force of high, such as: In the JDBC application, if you are already a slightly level of developers, you should always take preparestatement instead of statement. In other words, do not use statement at all times. See here, small weave heart can not help secretly determined, later also want to use preparestatement.
So, in real development, they are both superior and inferior.
In the previous article, when JDBC creates objects and executes SQL statements, we can know the difference between the two:
<span style= "Font-family:microsoft yahei;font-size:14px;" > //Create statement execution Object
//SQL statement to execute
String sql = "Select User_name,password from T_user where user_id=?";
Use Statement object
Statement st=conn.createstatement ();
Use the Preparestatement object
preparedstatement PST = conn.createstatement (SQL);
EXECUTE statement
//Use Statement object
st.executequery ("SQL");
Use Preparestatement object
pst.executequery () </span>
As you can see, when you use both, you use SQL in a different location. Preparestatement has been precompiled, and statement is using execution-time creation.
In the JDBC API, statement requires developers to devote a great deal of time and effort to using statement to obtain JDBC access, a common problem is to enter the appropriate format of the date and time stamp. And this problem, by adopting preparestatement can be solved automatically. Also, Preparestatement's other advantage string is not dynamically created.
OK, speaking here, we can preliminarily conclude that the preparestatement of the internet to blow so high, there is a certain reason. So why say: in JDBC applications, if you are already a slightly level developer, you should always replace statement with Preparestatement. Is the use of preparestatement just to push the grid higher? Obviously not, the bottom of the small series to give you good chatter.
JDBC-driven optimization is based on what functionality is being used. Choosing Preparestatement or statement depends on how you want to use them. Choosing statement is best for executing an SQL statement, but instead, if the SQL statement needs to be executed multiple times, then we can choose Preparestatement.
1, preparestatement lifting performance
Preparestatement is a precompiled approach, with each database doing its best to maximize performance optimizations for precompiled statements. Because a precompiled statement might be called repeatedly, so the SQL statement is compiled by the database compiler, the execution code is cached, then the next call, as long as the same precompiled statement does not need to compile, as long as the parameters passed directly into the compiled statement execution code, it will execute. Of course, this is not to say that only a precompiled statement that executes multiple times in one connection is cached, but to the entire database. As long as the precompiled statements match the cache, they can be compiled at any time and executed directly.
In statement statements, even the same operation, because the data for each operation is different, so the chance of matching the entire statement is very small, almost impossible to match exactly. Like what:
<span style= "Font-family:microsoft yahei;font-size:14px;" > INSERT INTO Tb_name (col1,col2) VALUES (' One ', ' all ');
Insert into Tb_name (col1,col2) VALUES (' One ', ', ');</span>
Even if the same operation, but because the inserted data content is different, so the entire statement itself does not match, do not have the meaning of cached statements.
Of course, not all precompiled statements are bound to be cached, and the database itself uses certain policies. such as the use of access frequency indicators, to determine when not to cache the existing precompiled results, to save more storage space to store new precompiled statements.
2, improve the readability and maintainability of code
Demo of a database insert
<span style= "Font-family:microsoft yahei;font-size:14px;" > //Perform an insert operation
//statement
Statement St;
St.executeupdate ("INSERT into Tb_name (COL1,COL2,COL2,COL4) VALUES (' +var1+" ', ' +var2+ ', ' +var3+ ', ' "+var4+ ')");
Preparestatement
PreparedStatement PST;
PST = con.preparestatement (INSERT into Tb_name (COL1,COL2,COL2,COL4) VALUES (?,?,?,?));
Pst.setstring (1,VAR1);
Pst.setstring (2,VAR2);
Pst.setstring (3,VAR3);
Pst.setstring (4,VAR4);
Pst.executeupdate ();</span>
Although using preparestatement will be a few more lines of code than statement, this change is worthwhile, both in terms of readability and maintainability of the code.
3. Safety
Preparestatement prevents SQL injection and improves the security of the program.
<span style= "Font-family:microsoft yahei;font-size:14px;" >string sql = "SELECT * from Tb_name where name= '" +varname+ "' and passwd= '" +varpasswd+ "";</span>
Here, if we enter [' or ' 1 ' = ' 1] as VARPASSWD, the user name is filled in, and the SQL injection problem is generated.
<span style= "Font-family:microsoft yahei;font-size:14px;" >select * from tb_name = ' random ' and passwd = ' or ' 1 ' = ' 1 ';</span>
Because ' 1 ' = ' 1 ' is definitely set up, so you can pass any verifier.
Further, put [';d rop table tb_name;] Passed in as VARPASSWD.
<span style= "Font-family:microsoft yahei;font-size:14px;" >select * from tb_name = ' random ' and passwd = ';d rop table tb_name;</span>
On some databases, these statements can be executed completely.
Of course, if you use a precompiled statement, any incoming content will not have any matching relationship with the original statement. Using Preparestatement, precompiled statements, do not have to do any consideration of the incoming data, if you use statement, you need to judge the drop and so on to prevent SQL injection.
Tail
Encountered not understand the problem, the Internet to check, standing on the shoulders of giants, is a good way. However, we still need to digest the problems of the giant people, and only then can we become giants.