-
Notifications
You must be signed in to change notification settings - Fork 1
/
login.java
85 lines (79 loc) · 3.79 KB
/
login.java
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
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class login{
JFrame loginPage = new JFrame("Login"); // JFrame means a window
JLabel userIDText = new JLabel("Enter UserID:"); //JLabel is a view-only text
JTextField userID = new JTextField(); //JTextField is an input field
JLabel passwordText = new JLabel("Enter Password:");
JPasswordField password = new JPasswordField(); //JPasswordField is a password input field
JLabel newUserText = new JLabel("New User ?");
JButton logIn = new JButton("Log In");
JButton signUpButton = new JButton("Sign Up");
login(){
//Set Positions of Components inside Jframe
userIDText.setBounds(0,0,150,50);
userID.setBounds(150,0,100,50);
passwordText.setBounds(0, 50, 150, 50);
password.setBounds(150, 50, 100, 50);
logIn.setBounds(150,100,100,50);
newUserText.setBounds(30, 150, 150, 50);
signUpButton.setBounds(150, 150, 100, 50);
//Add components to the JFrame
loginPage.add(logIn);
loginPage.add(userIDText);
loginPage.add(userID);
loginPage.add(passwordText);
loginPage.add(password);
loginPage.add(newUserText);
loginPage.add(signUpButton);
loginPage.setLayout(null); //Remove the layout which is present by default
loginPage.setExtendedState(JFrame.MAXIMIZED_BOTH); //Make JFrame full screen
//Optional Code
// loginPage.setSize(1000, 1000); // Set size instead of full screen
// loginPage.setUndecorated(true); //Make completely full screen(no buttons)
// loginPage.setLocationRelativeTo(null); //Move JFrame to centre
loginPage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Make program exit when window is Closed(X pressed)
loginPage.getRootPane().setDefaultButton(logIn);// Press button when return key is pressed
// loginPage.setVisible(true); //Show JFrame
logIn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", "mysecretpass");//connecting to database
Statement stmt = conn.createStatement();
stmt.execute("USE CABBOOKING");
ResultSet rs = stmt.executeQuery("SELECT PASSWORD FROM USERS WHERE USERID=\""+userID.getText()+"\"");
boolean loginSuccess = false;//checking the identity of user
while(rs.next()){
if(new String(password.getPassword()).equals(rs.getString("PASSWORD"))){
book myBooking = new book(userID.getText());
loginPage.setVisible(false);
myBooking.bookingPage.setVisible(true);
loginSuccess = true;
}
}
if(!loginSuccess){
ToastMessage toastMessage = new ToastMessage("Incorrect UserID or Password",3000);
toastMessage.setVisible(true);
}
conn.close();
} catch (Exception exception) {
System.out.println(exception);
}
}
});
signUpButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
signUp mySignUp = new signUp();
loginPage.setVisible(false);
mySignUp.register.setVisible(true);
}
});
}
public static void main(String[] args) {
login MyLoginPage = new login();
MyLoginPage.loginPage.setVisible(true);
}
}