In JSP, The PreparedStatement object is used to operate databases.

Source: Internet
Author: User

In JSP, The PreparedStatement object is used to operate databases.

The PreparedStatement interface inherits Statement, which is different from the following two aspects:
The PreparedStatement instance contains compiled SQL statements. This is to make the statement "ready ". The SQL statement contained IN the PreparedStatement object can have one or more IN parameters. The value of the IN parameter is not specified when the SQL statement is created. On the contrary, this statement reserves a question mark ("?") for each IN parameter. As a placeholder. The value of each question mark must be provided through the appropriate setXXX method before the statement is executed.
Because the PreparedStatement object has been pre-compiled, its execution speed is faster than the Statement object. Therefore, SQL statements executed multiple times are often created as PreparedStatement objects to improve efficiency.
As a subclass of Statement, PreparedStatement inherits all functions of Statement. IN addition, it also adds a complete set of methods to set the values sent to the database to replace the IN parameter placeholder. At the same time, the three methods execute, executeQuery, and executeUpdate have been changed so that they no longer need parameters. The Statement form of these methods (the form that accepts SQL Statement parameters) should not be used for the PreparedStatement object.
1. Create a PreparedStatement object
The following code snippet (where con is the Connection object) creates a PreparedStatement object that contains an SQL statement with two IN parameter placeholders:
PreparedStatement pstmt = con. prepareStatement ("UPDATE table4 SET m =? WHERE x =? ");
The pstmt object contains the statement "UPDATE table4 SET m =? WHERE x =? ", It has been sent to the DBMS and is ready for execution.
2. Pass IN Parameters
Before executing the PreparedStatement object, you must set each? Parameter value. This can be done by calling the setXXX method, where XXX is the type corresponding to this parameter. For example, if the parameter has the Java type long, the method used is setLong. The first parameter of the setXXX method is the ordinal position of the parameter to be set, and the second parameter is the value set to this parameter. For example, the following code sets the first parameter to 123456789 and the second parameter to 100000000:

pstmt.setLong(1, 123456789);pstmt.setLong(2, 100000000);

Once the parameter value of a given statement is set, it can be used to execute the statement multiple times until the clearParameters method is called to clear it. In the default connection mode (enable automatic submission), the statement is automatically submitted or restored when the statement is completed.
If the basic database and driver remain open after the statement is submitted, the same PreparedStatement can be executed multiple times. If this is not true, it is meaningless to try to use the PreparedStatement object instead of the Statement object to improve performance.
The following code uses pstmt (the PreparedStatement object created earlier) to demonstrate how to set the values of Two Parameter placeholders and execute pstmt for 10 times. To do this, the database cannot close pstmt. In this example, the first parameter is set to "Hi" and kept as a constant. In the for loop, the second parameter is set to a different value each time: from 0 to 9.

pstmt.setString(1, "Hi");for (int i = 0; i < 10; i++) { pstmt.setInt(2, i); int rowCount = pstmt.executeUpdate();}

3. consistency of data types IN the IN Parameter
In the setXXX method, XXX is of the Java type. It is an implicit JDBC Type (generally an SQL type), because the driver maps the Java type to the corresponding JDBC Type (according to section 8 in the JDBCGuide. 6.2 ing specified in the "ing Java and JDBC types" table) and send the JDBC Type to the database. For example, the following code snippet sets the second parameter of the PreparedStatement object pstmt to 44 and the Java type to short:
Pstmt. setShort (2, 44 );
The driver sends 44 to the database as jdbc smallint, which is a standard ing of the Java short type.
The programmer's responsibility is to ensure that the Java type of each IN parameter is mapped to the JDBC Type compatible with the JDBC data type required by the database. Consider the situation where the database needs jdbc smallint. If setByte is used, the driver sends the jdbc tinyint to the database. This is feasible because many databases can be converted from one type to another, and TINYINT can usually be used anywhere SMALLINT applies.
The Preprocessing statement object PreparedStatement. You can use PreparedStatement to add, update, delete, and query data.

Instance:
1. Add data

<% @ Page language = "java" contentType = "text/html; charset = gb2312" %> <% @ page import = "java. SQL. *" %> <! DOCTYPE html> 

Do not use case-insensitive letters. It takes a long time to mark in red because the first letter is in uppercase.

2. Update Data

<% @ Page language = "java" contentType = "text/html; charset = gb2312" %> <% @ page import = "java. SQL. *" %> <! DOCTYPE html> 

3. delete data

<% @ Page language = "java" contentType = "text/html; charset = gb2312" %> <% @ page import = "java. SQL. *" %> <! DOCTYPE html> 

4. query data

<% @ Page language = "java" contentType = "text/html; charset = gb2312" %> <% @ page import = "java. SQL. *" %> <! DOCTYPE html> 

Articles you may be interested in:
  • Summary of common SQL tag usage for operating databases in JSP
  • Jsp obtains data from the database filling drop-down box to implement the two-level linkage menu
  • Example of SQL Server 2008 database access using JDBC in JSP
  • Java Implementation of JSP connection to Oracle Database in Servelt
  • Analysis on paging technology for jsp database reading
  • Jsp reads database connection parameters from web. xml
  • JSP connection MySql/ms SQL Server/Oracle Database Connection Methods [finishing]
  • Jsp connection to the Access Database (do not create an ODBC Data Source)
  • JSP connection to Access database
  • Using asp or jsp, how does flash read and display all records in a table in the database?
  • Pagination of JSP database operation data
  • Access the Oracle database in JSP
  • How to access MySQL Database Using JSP

Related Article

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.