Java Foundation--statement and Preparestatement

Source: Internet
Author: User

Statement

statement is an important way for Java to run database operations. Used on the basis of a database connection has been established. Sends the SQL statement to run to the database. A statement object that is used to run simple SQL statements with no parameters. The object that is used to run a static SQL statement and return the results it produces.


By default, every Statement object at the same time can only open a ResultSet object. Therefore, suppose that reading a ResultSet object has a crossover with the read. The two objects must be generated by a different Statement object.

Assuming that there is an open current ResultSet Object for a statement, all running methods in the Statement interface will close it implicitly.


        statement object is used to set the SQL statements are sent to the database.

There are actually three kinds of Statement SQL Statement's containment: statement , preparedstatement Statement CallableStatement PreparedStatement SQL statement: statement SQL statement; preparedstatement object for running with or without in SQL statement; callablestatement object is used to run a call to a stored procedure that already exists on the database.


the Statement Interface provides a basic way to run statements and get results.

PreparedStatement interface joins the processing inch the method of the number of references; CallableStatement joined the processing out the method of the number of references.

preparestatement   

The PreparedStatement object is precompiled, so it runs faster than the Statement object. So. SQL statements that run multiple times are often created as preparedstatement objects. To improve efficiency.


As a subclass of Statement, PreparedStatement inherits the full functionality of Statement. It also adds a complete set of methods for setting the values that are sent to the database in place of the in placeholder.

CallableStatement

the CallableStatement object provides a way for all DBMS to invoke stored procedures in a standard form. Stored procedures are stored in the database. The call to the stored procedure is the content contained in the CallableStatement object.

Such calls are written in a code-changing syntax, in two forms: a form with a result, and a form without a result parameter. The result parameter is an output (out) parameter. is the return value of the stored procedure. Both forms can have a variable number of inputs (in parameters), outputs (out parameters), or inputs and outputs (inout parameters). The question mark is used as a placeholder for the number of parameters.

Statement or preparestatement?

  &NBSP,        read other people's code. You can see that most of the use is preparestatement preparestatement   Run precompiled statements, The precompiled statement was db The compiler compiled run code is cached. Then the next call is only if the same precompiled statement is not compiled, for multiple insertions but different data, use preparestatement can improve very large performance .

inserting 1 data with Statement time consuming:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqva2fuz2xpedfhbg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

inserting 1 data with preparestatement time consuming:


    &NBSP,     security is preparestatement use Preparestatement .

You should all know SQL injection; There is a risk of SQL injection for SQL statements that are spelled in a bunch of ways. Instead, assume that a precompiled statement is used. Whatever content is passed in will not match the original statement regardless of the relationship. Security has a very high level of improvement.


Summary

Although preparestatement has a lot of advantages compared to Statement. But Statement is not useless. Suppose the inserted SQL statement is not fixed. There are no rules, so using preparestatement has no advantage in performance. Existence is a certain rationality. So we should choose the right way according to the situation.

Java Foundation--statement and Preparestatement

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.