|
| 1 | +package top.mryan2005.managesysteminjava.BasicClass; |
| 2 | + |
| 3 | +import org.apache.commons.codec.digest.DigestUtils; |
| 4 | +import top.mryan2005.managesysteminjava.SQLs.SQLLinker; |
| 5 | + |
| 6 | +import java.sql.ResultSet; |
| 7 | +import java.sql.SQLException; |
| 8 | +import java.util.HashMap; |
| 9 | + |
| 10 | +import static java.lang.String.valueOf; |
| 11 | + |
| 12 | +public class LoginPart { |
| 13 | + |
| 14 | + private SQLLinker sql; |
| 15 | + |
| 16 | + private HashMap<String, User> users = new HashMap<String, User>(); |
| 17 | + |
| 18 | + public String getMaxId() { |
| 19 | + ResultSet res = sql.runSQL("SELECT MAX(id) 'maxid' FROM Users.[user]"); |
| 20 | + if (res == null) { |
| 21 | + return "-1"; |
| 22 | + } |
| 23 | + try { |
| 24 | + res.next(); |
| 25 | + return valueOf(res.getInt("maxid") + 1); |
| 26 | + } catch (SQLException e) { |
| 27 | + return "-1"; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public void readUsersTable() { |
| 32 | + users.clear(); |
| 33 | + ResultSet res = sql.runSQL("SELECT * FROM Users.[user]"); |
| 34 | + if (res == null) { |
| 35 | + return; |
| 36 | + } |
| 37 | + try { |
| 38 | + while (res.next()) { |
| 39 | + User user = new User(res.getString("username"), res.getString("password"), res.getString("level"), res.getString("role"), res.getString("name"), res.getString("avator")); |
| 40 | + users.put(res.getString("username"), user); |
| 41 | + } |
| 42 | + } catch (SQLException e) { |
| 43 | + return; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public LoginPart(SQLLinker sql1) { |
| 48 | + sql = sql1; |
| 49 | + } |
| 50 | + |
| 51 | + public void register(String username, String password, String Sex, String UName) { |
| 52 | + readUsersTable(); |
| 53 | + if (username.matches("") || password.matches("") || UName.matches("")) { |
| 54 | + return; |
| 55 | + } |
| 56 | + if(users.containsKey(username)) { |
| 57 | + return; |
| 58 | + } |
| 59 | + password = runMd5(password); |
| 60 | + sql.runSQL("INSERT INTO Users.[user] (id, username, password, Sex, name) VALUES ('" + getMaxId() +"', '" + username + "', '" + password + "', '" + Sex + "', '" + UName + "')"); |
| 61 | + } |
| 62 | + |
| 63 | + public boolean login(String username, String password) { |
| 64 | + readUsersTable(); |
| 65 | + password = runMd5(password); |
| 66 | + if (users.containsKey(username)) { |
| 67 | + return users.get(username).password.matches(password); |
| 68 | + } |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + private String runMd5(String password) { |
| 73 | + for (int i = 0; i < 5; i++) { |
| 74 | + password = DigestUtils.md5Hex(password); |
| 75 | + } |
| 76 | + return password; |
| 77 | + } |
| 78 | + |
| 79 | + public void setAvatar(String username, String avatar) { |
| 80 | + sql.runSQL("UPDATE Users.[user] SET avatar = '" + avatar + "' WHERE username = '" + username + "'"); |
| 81 | + } |
| 82 | + |
| 83 | + public void setUName(String username, String UName) { |
| 84 | + sql.runSQL("UPDATE Users.[user] SET name = '" + UName + "' WHERE username = '" + username + "'"); |
| 85 | + } |
| 86 | + |
| 87 | + public void setPassword(String username, String password) { |
| 88 | + password = runMd5(password); |
| 89 | + sql.runSQL("UPDATE Users.[user] SET password = '" + password + "' WHERE username = '" + username + "'"); |
| 90 | + } |
| 91 | + |
| 92 | + public void setSex(String username, String sex) { |
| 93 | + sql.runSQL("UPDATE Users.[user] SET sex = '" + sex + "' WHERE username = '" + username + "'"); |
| 94 | + } |
| 95 | + |
| 96 | + public void setLevel(String username, int level) { |
| 97 | + sql.runSQL("UPDATE Users.[user] SET level = '" + valueOf(level) + "' WHERE username = '" + username + "'"); |
| 98 | + } |
| 99 | + |
| 100 | + public void setRole(String username, int role) { |
| 101 | + sql.runSQL("UPDATE Users.[user] SET role = '" + valueOf(role) + "' WHERE username = '" + username + "'"); |
| 102 | + } |
| 103 | + |
| 104 | + public void removeUser(String username) { |
| 105 | + sql.runSQL("DELETE FROM Users.[user] WHERE username = '" + username + "'"); |
| 106 | + readUsersTable(); |
| 107 | + } |
| 108 | + |
| 109 | + public static void main(String[] args) { |
| 110 | + try { |
| 111 | + SQLLinker sql = new SQLLinker("SQL Server", "127.0.0.1", "1433", "sa", "123456", "wuzhouDict"); |
| 112 | + System.out.println("连接成功!"); |
| 113 | + LoginPart login = new LoginPart(sql); |
| 114 | + login.register("test", "123456", "M", "test"); |
| 115 | + if(login.login("test", "123456")) { |
| 116 | + System.out.println("登录成功!"); |
| 117 | + } else { |
| 118 | + System.out.println("登录失败!"); |
| 119 | + } |
| 120 | + } catch (SQLException e) { |
| 121 | + throw new RuntimeException(e); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments