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