Java_web Learning (9) Introduction of dynamic parameters of PreparedStatement, preparedstatement

Source: Internet
Author: User

Java_web Learning (9) Introduction of dynamic parameters of PreparedStatement, preparedstatement
1. Overview of PreparedStatement

During database operations, the PreparedStatement object is a very inconspicuous but important interface object. It inherits from the Statement and is different from the following two aspects:

1) 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.

2) 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.

 

Ii. PreparedStatement application instance 2.1 Step 1 import the jar package

2.2 create table t1

1 <body> 2 <% 3 // load the database driver to jdbc 4 Class. forName ("org. gjt. mm. mysql. driver "); 5 out. print ("successfully loaded driver"); 6 String url = "jdbc: mysql: // 127.0.0.1: 3306/datebase? User = root & password = 123456 "; 7 // get the database Connection, so that java can operate mysql 8 Connection conn = DriverManager. getConnection (url); 9 // define an SQL command to create a table named t1 10 String SQL = "create table t1 (sno varchar (20), name varchar (20 ), birth String) "; 11 // obtain an SQL executor 12 PreparedStatement ps = conn from the connection object. prepareStatement (SQL); 13 // run 14 ps.exe cute (); 15 out. print (conn); 16 // first close the connection for 18 ps. close (); 19 conn. close (); 20%> 21 22 </body>

 

2.3 Replace the SQL command for data operations in Table t1
<%
String sno="1";
String name = "James ";
String birth="2008-08-24";
// Load the database driver to jdbcClass. forName ("org. gjt. mm. mysql. driver "); out. print ("successfully loaded driver"); String url = "jdbc: mysql: // 127.0.0.1: 3306/datebase? User = root & password = 123456 "; // obtain the database connection so that java can operate mysqlConnection conn = DriverManager. getConnection (url); // define an SQL command String SQL = "insert into student (sno, name, birth) values (?,?,?) "; // Get an SQL executor PreparedStatement ps = conn. prepareStatement (SQL) from the connection object; // prevent code from being written to death
Ps. setString (1, sno); ps. setString (2, name); ps. setString (3, birth); // execute ps.exe cute (); out. print (conn); // first close the connection ps. close (); conn. close (); %> </body>

 

 

// Add information
String SQL = "insert into t1 (sno, name, birth) values (?,?,?) ";

// Modify the information
String sql1 = "UPDATE t1 SET birth =? Where sno =? ";

// Delete information
String sql2 = "delete from t1 where sno =? ";

 

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.