-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserManager.java
More file actions
81 lines (52 loc) · 2.02 KB
/
UserManager.java
File metadata and controls
81 lines (52 loc) · 2.02 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
package ExpenseTracker;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
public class UserManager {
public void registerUser() {
try {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Name: ");
String name = sc.nextLine();
System.out.print("Enter Email: ");
String email = sc.nextLine();
System.out.print("Enter Password: ");
String password = sc.nextLine();
Connection con = DatabaseConnection.getConnection();
String sql = "INSERT INTO users(name,email,password) VALUES(?,?,?)";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, email);
ps.setString(3, password);
ps.executeUpdate();
System.out.println("User Registered Successfully");
} catch(Exception e) {
e.printStackTrace();
}
}
// NEW METHOD (add below registerUser)
public int loginUser() {
try {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Email: ");
String email = sc.nextLine();
System.out.print("Enter Password: ");
String password = sc.nextLine();
Connection con = DatabaseConnection.getConnection();
String sql = "SELECT id FROM users WHERE email=? AND password=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, email);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
System.out.println("Login Successful");
return rs.getInt("id");
}
System.out.println("Invalid login");
} catch(Exception e) {
e.printStackTrace();
}
return -1;
}
}