There are two methods: one is to judge the variables obtained by login with special characters
One is to use preparedstatement for query during login, which can be effectively used for SQL injection.
Method 1
Sqlstring. Java
- Package com. Test. util;
- Public class sqlstring {
- Public static Boolean SQL _inj (string Str ){
- String inj_str = "': And: Exec: insert: select: delete: Update: Count: *: %: CHR: Mid: MASTER: truncate: Char: declare:;: or: -: + :,";
- String inj_stra [] = inj_str.split (":");
- For (INT I = 0; I <inj_stra.length; I ++ ){
- If (Str. indexof (inj_stra [I])! =-1 ){
- Return false;
- }
- }
- Return true;
- }
- }
Use the following statement when calling:
Boolean F = sqlstring. SQL _inj (username );
PS: I have read the above Code on the Internet. They use the separator "|", but I found that string inj_stra [] = inj_str.split ("| "); all the characters are separated. instead of splitting strings, they are split into characters. I use jdk1.5. I don't know if it is a Java version, the example of the jdk1.5 API is separated by ":", so I can use this symbol to split each string.
The second method is to use preparedstatement during query.
- SQL = "select * from Admin where username =? And Password =? ";
- Preparedstatement psmt = con. preparestatement (SQL );
- Psmt. setstring (1, username );
- Psmt. setstring (2, password );
- Resultset rs = psmt.exe cutequery ();
- If (Rs. Next ){
- Rs. Close ();
- Con. Close ();
- Return false;
- }
- Else {
- Rs. Close ();
- Con. Close ();
- Return true;
- }
In this way, you can