Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit dffa578

Browse files
committed
add: add Login function alpha
1 parent cba80be commit dffa578

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

src/main/java/top/mryan2005/managesysteminjava/BasicClass/Entry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public String generateHTML() {
6464

6565
public String generateCurrentHash() throws UnsupportedEncodingException {
6666
Base64 base64 = new Base64();
67-
currentHash = DigestUtils.md5Hex(html.getBytes("utf-8"));
67+
currentHash = DigestUtils.md5Hex(base64.encode(html.getBytes("GBK")));
6868
return currentHash;
6969
}
7070

src/main/java/top/mryan2005/managesysteminjava/BasicClass/LoginPart.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ public LoginPart(SQLLinker sql1) {
6363
sql = sql1;
6464
}
6565

66+
public void setSQL(SQLLinker sql1) {
67+
sql = sql1;
68+
}
69+
70+
public LoginPart() {}
71+
6672
public void register(String username, String password, String Sex, String UName) throws UnsupportedEncodingException {
6773
readUsersTable();
6874
if (username.matches("") || password.matches("") || UName.matches("")) {

src/main/java/top/mryan2005/managesysteminjava/BasicClass/User.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package top.mryan2005.managesysteminjava.BasicClass;
22

3+
import top.mryan2005.managesysteminjava.SQLs.SQLLinker;
4+
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
38
public class User {
49
public String username; // 用户名
510
public String password; // 密码
@@ -18,4 +23,29 @@ public User(String username, String password, int level, String role, String UNa
1823
this.avatar = avatar;
1924
this.sex = "Unknown";
2025
}
26+
27+
public User() {
28+
}
29+
30+
public void loadUser(String username, SQLLinker sql) {
31+
if (username == null) {
32+
return;
33+
}
34+
try {
35+
ResultSet res = sql.runSQL("SELECT * FROM Users.[user] WHERE username = '" + username + "'");
36+
if (res == null) {
37+
return;
38+
}
39+
while (res.next()) {
40+
this.username = res.getString("username");
41+
this.password = res.getString("password");
42+
this.level = res.getInt("level");
43+
this.role = res.getString("role");
44+
this.UName = res.getString("name");
45+
this.avatar = res.getString("avator");
46+
}
47+
} catch (SQLException e) {
48+
throw new RuntimeException(e);
49+
}
50+
}
2151
}

src/main/java/top/mryan2005/managesysteminjava/Core.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package top.mryan2005.managesysteminjava;
2+
import top.mryan2005.managesysteminjava.BasicClass.LoginPart;
3+
import top.mryan2005.managesysteminjava.BasicClass.User;
24
import top.mryan2005.managesysteminjava.ConnectToNet.POSTAndGET;
35
import top.mryan2005.managesysteminjava.SQLs.SQLLinker;
46
import top.mryan2005.managesysteminjava.Settings.Info;
57

68
import java.awt.*;
79
import java.awt.event.*;
810
import java.io.IOException;
11+
import java.io.UnsupportedEncodingException;
912
import java.sql.Connection;
1013
import java.sql.ResultSet;
1114
import java.sql.SQLException;
@@ -23,6 +26,10 @@ public class Core extends JFrame {
2326

2427
private SQLLinker sql;
2528

29+
private LoginPart loginPart;
30+
31+
private User currentUser;
32+
2633
private boolean isLogin = false;
2734

2835
public void print(String str) {
@@ -197,6 +204,7 @@ public Core() throws IOException, SQLException {
197204
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
198205
setBounds(0,0,1729,972);
199206
sql = new SQLLinker("SQL Server", "127.0.0.1", "1433", "sa", "123456", "wuzhouDict");
207+
loginPart = new LoginPart(sql);
200208
}
201209

202210
public void actionPerformed(ActionEvent e) {
@@ -527,7 +535,18 @@ public void createLoginWindow(JDialog owner) {
527535
LoginButton.addActionListener(new ActionListener() {
528536
@Override
529537
public void actionPerformed(ActionEvent e) {
530-
538+
try {
539+
if(loginPart.login(jTextFieldUsername.getText(), jTextFieldPassword.getText())) {
540+
isLogin = true;
541+
currentUser = new User();
542+
currentUser.loadUser(jTextFieldUsername.getText(), sql);
543+
jDialogInput.dispose();
544+
} else {
545+
JOptionPane.showMessageDialog(jDialogInput, "Login Failed");
546+
}
547+
} catch (UnsupportedEncodingException ex) {
548+
throw new RuntimeException(ex);
549+
}
531550
}
532551
});
533552
gbc.fill = GridBagConstraints.CENTER;

0 commit comments

Comments
 (0)