Java PreparedStatement and Addbatch () methods

Source: Internet
Author: User

This article describes two content, why use PreparedStatement? and PreparedStatement's Addbatch () method, the Addbatch () method is introduced first because the tool is often used.

First, Addbatch use method

Yesterday, using Java to do a guide table function, the amount of data is very much, using the following buffer method is very useful.

1. Create a link

Connection Connection =getconnection ();

2. Remove auto-commit function Commit

Connection.setautocommit (FALSE);

3. Precompiled SQL statements, compiled only one back oh, high efficiency ah.

PreparedStatement statement = connection.preparestatement ("INSERT into TABLEX VALUES (?,?)");

4. Note Statement

//Record 1statement.setint (1,1); Statement.setstring (2, "Cujo"); Statement.addbatch (); //Record 2statement.setint (1,2); Statement.setstring (2, "Fred"); Statement.addbatch (); //Record 3statement.setint (1,3); Statement.setstring (2, "Mark"); Statement.addbatch (); 

5. Execute the above 3 statements in bulk

int []=//Commit  It swallow, into the belly (DB) inside connection. Commit ();

Second, the advantage of using PreparedStatement:

1, the readability and maintainability of the code:

Although using PreparedStatement instead of statement will make the code a few more lines, but such code in terms of readability or maintainability, than the direct use of statement code much higher grade:

--The first method
Stmt.executeupdate ("Insert intoTb_name (COL1,COL2,COL2,COL4)Values('"+var1+"','"+var2+"',"+Var3+",'"+var4+"')");//stmt is an statement object instance---------------------------------------------------------------------------The second method
Perstmt=Con.preparestatement ("Insert intoTb_name (COL1,COL2,COL2,COL4)Values (?,?,?,?)"); Perstmt.setstring (1, var1);p erstmt.setstring (2, Var2);p erstmt.setstring (3, Var3);p erstmt.setstring (4, VAR4);p erstmt.executeupdate (); //Prestmt is an PreparedStatement object instance

As you can see, for the first method, don't say other people to read your code, it is your own time to read again, will feel sad.

2, PreparedStatement to maximize the performance of the best possible:

Statements are cached by the compiler-compiled execution code of the DB, so the next call will not need to be compiled as long as the same precompiled statement is passed, as long as the parameters pass directly into the compiled statement execution code (which is equivalent to a culvert number) and will be executed. This is not to say that only a precompiled statement that executes more than once in a connection is cached, but for the entire DB, as long as the precompiled statement syntax matches the cache, it can be executed at any time without having to compile again.

In statement statements, even the same operation, and because the data for each operation is different so that the chances of matching the entire statement are minimal, it is almost impossible to match. Like what:

Insert  into Values ('One ',' a '); Insert  into Values ('one',' ') ';

Even though the same operation but because the data content is not the same, so the entire statement itself does not match, there is no meaning of the cached statement. The fact is that no database executes code caches after the normal statement is compiled.

(Of course, not all precompiled statements are bound to be cached, and the database itself uses a strategy, such as frequency, to determine when the pre-compiled results are no longer cached, to save more space to store new precompiled statements.) )

3, the most important point is to improve the security:

Perhaps so far, some people still don't know the basic semantics of SQL syntax:

= "Select*fromwhere name='" +varname+ "'   and passwd='"+varpasswd+"";

If we pass [' or ' 1 ' = ' 1] in as varpasswd. User name feel free to see what will become?

Select *  from = ' Casual '  and = "' or ' 1 ' = ' 1 ';

because ' 1 ' = ' 1 ' is sure to be true, so can any pass validation. This is more tactful, more people to [';d ROP table tb_name;] Pass in as varpasswd ... The data security aspects are not discussed here, in short, using PreparedStatement to make SQL execution more secure.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.