The PreparedStatement of Java Learning

Source: Internet
Author: User

prepared see this word, prepare the meaning, PreparedStatement actually is precompiled. So what's the difference with statement?
Preparedstatemen has the advantage of:
1. Prevent repeated writing of multiple SQL statements with similar structure
2. The trouble of not stitching strings
3. Preventing SQL Injection (splicing strings can cause SQL injection problems)

4.sql statement pre-compiled in PreparedStatement object, good performance

once in the study of "Beef Brisket news Release System", the Cow teacher said "SQL statement injection" Such knowledge, in fact, to Java or there will be such a problem, PreparedStatement is the perfect solution to this problem.

So I'm going to add an example from the DRP and learn it together.

import Java.sql.connection;import Java.sql.preparedstatement;import Java.sql.resultset;import java.sql.SQLException; Import Java.sql.timestamp;import java.util.arraylist;import java.util.date;import java.util.List; 
/** * Add user * @param user */public void AddUser (user user) {String sql = "INSERT into T_user (user_id, user_name, password,   Contact_tel, email, create_date) "+" VALUES (?,?,?,?,?,?) "; Insert New USER Connection conn = null;//Instantiate PreparedStatement and result set PreparedStatement pstmt = null;//Instantiate PreparedStatement and result set try {conn = dbutil.getconnection ();//Get SQL statement pstmt = conn.preparestatement (sql);//The role of precompilation is reflected here.   Pstmt.setstring (1, User.getuserid ());  User idpstmt.setstring (2, User.getusername ());  User name Pstmt.setstring (3, User.getpassword ()); User Password pstmt.setstring (4, User.getcontacttel ()); User Phone number pstmt.setstring (5, User.getemail ());//user Mailbox//pstmt.settimestamp (6, New Timestamp (System.currenttimemillis ( ));p Stmt.settimestamp (6, New Timestamp (New Date (). GetTime ()));p stmt.executeupdate ();                   catch (SQLException e) {e.printstacktrace (); Intercept exception handling}finally {dbutil.close (pstmt);D butil.close (conn);}}
  This is only part of the code, but it is a detailed representation of PreparedStatement's usage. The application of the underlying should be fixed knowledge, when they have seen more, there is no previous kind of flustered.


The PreparedStatement of Java Learning

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.