1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
| package com.leiking.userlogin;
import Javax.swing.plaf.nimbus.State;
import Java.sql.*;
import Java.util.HashMap;
import Java.util.Map;
import Java.util.Scanner;
public class LoginMain {
public static void main(String[] args) {
//初始化界面
Map<String,String> userInfo=initUI();
//验证用户名和密码
boolean bol=login(userInfo);
//输出结果
System.out.println(bol==true?"登陆成功":"登陆失败");
}
private static boolean login(Map<String, String> userInfo) {
boolean loginSuccess=false;
Connection conn=null;
Statement stat=null;
ResultSet res=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata?useSSL=false");
stat=conn.createStatement();
String sql="select * from t_user where " +
"loginName='"+userInfo.get("username")+
"' and loginPwd='"+userInfo.get("password")+"';";
res=stat.executeQuery(sql);
if (res.next()){
loginSuccess=true;
}
return loginSuccess;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if (res!=null){
try {
res.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (stat!=null){
try {
stat.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
return true;
}
/**
* 初始化用户界面
* @return 用户输入的用户名与信息
*/
private static Map<String, String> initUI() {
Scanner s=new Scanner(System.in);
System.out.print("输入用户名:");
String username=s.nextLine();
System.out.println();
System.out.print("输入密码:");
String password=s.nextLine();
System.out.println();
Map<String,String> userInfo=new HashMap<>();
userInfo.put("username",username);
userInfo.put("password",password);
return userInfo;
}
}
//sql注入现象
//输入:用户名:fdss;密码:125' or '1'='1
select * from t_user where loginName='fdss' and loginPwd='125' or 1 = '1
|