The difference between preparestatement and createstatement in Java

Source: Internet
Author: User

Let's start with two pieces of code:

First use of createstatement ()

1  Public voidDeleteintID) {2         Try {3Connection C =dbutil.getconnection ();4Statement s =c.createstatement ();5String sql = "Delete from category where id =" +ID;6 s.execute (SQL);7}Catch(SQLException e) {8 e.printstacktrace ();9         }Ten}

The following section of code uses preparestatement () to complete

1 /**2 * Add information to the database3      * @paramConfig4      */5      Public voidAdd (config config) {6String sql = "INSERT into category values (null,?,?)";7         Try {8Connection C =dbutil.getconnection ();9PreparedStatement PS =c.preparestatement (SQL);TenPs.setstring (1, Config.key); OnePs.setstring (2, config.value); A Ps.execute (); -ResultSet rs =Ps.getgeneratedkeys (); -              the              while(Rs.next ()) { -                 intid = rs.getint (1); -Config.id =ID; -             } +}Catch(SQLException e) { - e.printstacktrace (); +         } A}

The difference between the two can be broadly seen:

1: Conceptually

Preparestatement Initializes SQL first, submits the SQL to the database for preprocessing, and uses multiple times to improve efficiency.

Createstatement is not initialized, there is no preprocessing, and every time you execute SQL from 0.

2: On the variable

Preparestatement can be used in SQL to replace variables

String sql = "INSERT into category values (null,?,?)";

Createstatement not supported? Replace variables, only stitching parameters in SQL

String sql = "Delete from category where id =" + ID;

3: Functional

If you want to delete three data

For createstatement, you need to write three statements

String sql = "Delete from category where id = 2"  = "Delete from category where id = 3"  = "Delete From category where id = 7 "  ;

And Preparestatement, by set different data only need to generate one execution plan, can reuse

String sql = "Delete from category where id =?" "  ;

1.PreparedStatement is precompiled and can greatly improve efficiency for batch processing. Also known as the JDBC stored procedure.

2. Use the Createstatement object. When only one-time access to the database is performed, the Createstatement object is used for processing. The overhead of PreparedStatement objects is larger than createstatement, and there is no additional benefit for one-time operations.

3.createStatement each execution of SQL statements, the relevant database to execute SQL statement compilation, PreparedStatement is precompiled, PreparedStatement support batch processing

4: repeatability

For the above two-segment code: the first segment ID must be given a value

In the second paragraph, the key,value of the object can be defined by itself, and in most cases the statement has been precompiled, so when it executes, only the DBMS runs the SQL statement without having to compile it first.

This conversion also provides you with great convenience, eliminating the need to repeat the syntax of the SQL statement, and simply changing the value of the variable in it to re-execute the SQL statement.

The choice of the PreparedStatement object is whether the SQL statement with the same syntax executes multiple times, and the difference between the two times is only the difference between variables. If it is executed only once, it should be no different from the ordinary object, and it cannot demonstrate the superiority of its precompilation.

The difference between preparestatement and createstatement in Java

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.