-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
97 lines (74 loc) · 2.3 KB
/
Copy pathUser.java
File metadata and controls
97 lines (74 loc) · 2.3 KB
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
87
88
89
90
91
92
93
94
95
96
97
package sample;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDateTime;
import sample.mainDatabase;
public class User {
private String username;
private String password;
private String loginTime;
private int userId;
public User(){
this.username = "";
this.password = "";
this.userId = -1;
this.loginTime = "-1";
}
public boolean attemptLogin(){
try {
PreparedStatement statement = mainDatabase.getCurrentConnection().prepareStatement("SELECT * FROM user WHERE userName = '" + this.username +
"' and password ='" + this.password + "';");
ResultSet loginResults = statement.executeQuery();
if(loginResults.next()) {
this.userId = loginResults.getInt("userId");
this.loginTime = LocalDateTime.now().toString();
mainDatabase.setCurrent_user(this.username);
mainDatabase.setCurrent_user_id(this.userId);
writeLogFile();
return true;
}
} catch (SQLException e){
System.out.println("SQL Statement error! " + e);
return false;
}
return false;
}
public void writeLogFile(){
try{
FileWriter userlog = new FileWriter("userlog.txt", true);
String data = "\n" + this.username + " " + this.loginTime;
userlog.write(data);
userlog.close();
} catch (IOException e){
System.out.println("Error creating log file: " + e);
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getLoginTime() {
return loginTime;
}
public void setLoginTime(String loginTime) {
this.loginTime = loginTime;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
}