· 6 years ago · Dec 05, 2019, 12:06 PM
1//SQL code to initialize the user table
2//Please execute this locally to create necessary tables
3
4//create table users (
5// id SERIAL PRIMARY KEY,
6// email varchar(128) NOT NULL,
7// password varchar(128) NOT NULL,
8// };
9
10//initialize the table, create some dummy users
11// insert into login (email, password) values ('abc', '123');
12
13//create table users (
14// id SERIAL PRIMARY KEY,
15// patientfirstname varchar(128) NOT NULL,
16// patientlastname varchar(128) NOT NULL,
17// email varchar(128) NOT NULL,
18// diabetestype varchar(128) NOT NULL,
19// insulintype varchar(128) NOT NULL,
20// admintype varchar(128) NOT NULL,
21// phonenumber varchar(32),
22// address varchar(128) NOT NULL,
23// doctorphonenumber varchar(128) NOT NULL
24// );
25
26//insert into patients (patientfirstname,patientlastname,phonenumber) values('Jo nes','Bill','07755678899');
27
28
29import java.sql.*;
30import java.util.ArrayList;
31
32public class login {
33 public login() {
34 }
35
36 public static void checkLogin(ArrayList<String> list) throws SQLException {
37 String email1 = list.get(0);
38 String password = list.get(1);
39
40 Connection conn = ConnectionFactory.getConnection();
41
42 //Checking if email exists in database
43 String queryCheckEmail = "SELECT * from login WHERE email ='ijk@gmail.com'";
44 //"'%\"+email+\"%';";
45
46 Statement st = conn.createStatement();
47 ResultSet rsemail = st.executeQuery(queryCheckEmail);
48
49 while(rsemail.next()) {
50 String entryEmail = rsemail.getString("email"); //from table
51 if (entryEmail == email1) {
52 //all good
53 System.out.println("email exists");
54 return;
55 } else {
56 //Print out
57 System.out.println("Invalid email - Register?");
58 }
59 }
60
61 //Checking for password
62 String queryCheckPwd = "SELECT password from login WHERE password ='%\"+password+\"%';";
63 ResultSet rspwd = st.executeQuery(queryCheckPwd);
64
65 while(rspwd.next()) {
66 String entryPwd = rspwd.getString("password");
67 if (password == entryPwd) {
68 //Redirect to next page
69 System.out.println("User credentials match.");
70 } else {
71 //Print out "Invalid password!"
72 System.out.println("Email and password do not match.");
73 }
74 }
75 }
76}