JDBC entry (3) --- PrepareStatement,
I. Overview of PrepareStatement
PrepareStatement is a subinterface of the Statement interface;
1. Strength:
-
- Protects against SQL attacks;
- Improve code readability;
- Improve efficiency;
2. Usage of PrepareStatement:
-
- How to get the PrepareStatement object
- Provides an SQL template.
- Call the PreparedStatement prepareStatement of Connection (String SQL template );
- In the SQL template that calls the setXxx () Series Method of pstmt? Assignment
- Call the executeUpdate () or executeQuery () of pstmt, but its method does not have any parameters.
3. Principles of preprocessing:
-
- Server work:
- Check the syntax of an SQL statement;
- Compile: something similar to a function
- Run: Call the function.
- PreparedStatement:
- Prerequisites: The connected database must support preprocessing, which is almost not supported.
- Each pstmt is bound with an SQL template. The SQL template is first sent to the database, and the database is verified before compilation. During execution, the parameter is passed.
- If you perform the second operation, you do not need to verify the syntax again or compile it again.
- Preprocessing is disabled by default. You can add parameters when setting the url, for example:
String url = "jdbc:mysql://localhost:3306/mydb1?useServerPrepstmts=ture&cachePreStmts=true";
Ii. What is SQL attack?
Where user input is required, the user inputs the SQL statement segment. The SQL segment entered by the end user is combined with the SQL statement written in DAO into a complete SQL statement, for example, the username and password entered by the user during login are fragments of SQL statements.
1 package demo3; 2 3 import org. junit. test; 4 import java. io. IOException; 5 import java. SQL. *; 6 7/** 8 * PreapredStatement 9 * SQL attack prevention 10 */11 public class Demo3 {12/** 13 * log on to 14 * use username and password to query data 15 * If query the result set, correct description. true16 * is returned. If no query result is displayed, the user name or password is incorrect. false17 */18 public boolean login (String username, String password) is returned) throws Exception {19/* 20*1. Connection21 * 2. Statement22 * 3. ResultSet23. * 4. rs. for what is returned by next (), we will put it back. 24 **/25 String driverClassName = "com. mysql. jdbc. driver "; 26 String url =" jdbc: mysql: // localhost: 3306/mydb1 "; 27 String mysqlUsername =" root "; 28 String mysqlPassword =" "; 29 Class. forName (driverClassName); 30 Connection con = DriverManager. getConnection (url, mysqlUsername, mysqlPassword); 31 Statement stmt = con. createStatement (); 32 String SQL = "SELECT * FROM t_user WHER E username = '"+ username +"' AND password = '"+ password +"' "; 33 System. out. println (SQL); 34 ResultSet rs = stmt.exe cuteQuery (SQL); 35 return rs. next (); 36} 37 @ Test38 public void fun1 () throws Exception {39 // SELECT * FROM t_user WHERE username = 'A' or 'A' = 'A' AND password = 'A' or 'A' = 'A' 40 String username = "A' or 'A' = 'a "; 41 String password = "A' or 'A' = 'a"; 42 boolean bool = login (username, password); 43 Syst Em. out. println (bool); // The output is true44} 45 public boolean login2 (String username, String password) throws Exception {46/* 47*1. Connection48 * 2. Statement49 * 3. ResultSet50 * 4. rs. if next () is returned, we will put it back. 51 **/52 String driverClassName = "com. mysql. jdbc. driver "; 53 String url =" jdbc: mysql: // localhost: 3306/mydb1? Useserverprepsponts = ture & cachepresponts = true "; 54 String mysqlUsername =" root "; 55 String mysqlPassword =" "; 56 Class. forName (driverClassName); 57 Connection con = DriverManager. getConnection (url, mysqlUsername, mysqlPassword); 58 59/* 60*1. Get PreparedStatement61 * 1. SQL template: All parameters are used? To replace 62*2. Call the Connection method to obtain PreparedStatement63 **/64 // SELECT * FROM t_user WHERE username =? AND password =? 65 String SQL = "SELECT * FROM t_user WHERE username =? AND password =? "; 66 PreparedStatement pstmt = con. prepareStatement (SQL); 67/* 68*2. Assign 69 **/70 pstmt to the parameter. setString (1, username); // assign values to the 1st question marks. The value is username71 pstmt. setString (2, password); // assign a value to the 2nd question mark with the value password72 ResultSet rs = pstmt.exe cuteQuery (); // call the query method, send the query statement 73 return rs to the database. next (); 74} 75 @ Test76 public void fun2 () throws Exception {77 String username = "A' or 'A' = 'a "; 78 String password = "A' or 'A' = 'a"; 79 boolean bool = login2 (username, password); 80 System. out. println (bool); // The output is fasle81} 82/* 83 * test JdbcUtils. getConnection84 **/85 @ Test86 public void fun3 () throws IOException, ClassNotFoundException, SQLException {87 Connection con = JdbcUtils. getConnection (); 88 System. out. println (con); 89 Connection con1 = JdbcUtils. getConnection (); 90 System. out. println (con); 91} 92}