You are on page 1of 4

package campiology.

dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.security.NoSuchAlgorithmException;
import campiology.model.CustomerBean;
import campiology.connection.ConnectionManager;

public class CustomerDAO {


static Connection currentCon = null;
static ResultSet rs = null;
static PreparedStatement ps=null;
static Statement stmt=null;
static String custEmail, custName, custPhone, custPassword;
static double rating;

//login
public static CustomerBean CustomerLoginController(CustomerBean bean) throws
NoSuchAlgorithmException {

custEmail = bean.getCustEmail();
custPassword = bean.getCustPassword();
String searchQuery = "select * from customers where custEmail = '" +
custEmail + "' AND custPassword = '" + custPassword + "'";

System.out.println("Your email is " + custEmail);


System.out.println("Your password is " + custPassword);
System.out.println("Query: " + searchQuery);

try {
currentCon = ConnectionManager.getConnection();
stmt = currentCon.createStatement();
rs = stmt.executeQuery(searchQuery);
boolean more = rs.next();

// if user exists set the isValid variable to true


if (more) {
String custEmail = rs.getString("custEmail");

System.out.println("Welcome " + custEmail);


bean.setCustEmail(custEmail);
bean.setValid(true);
}

// if user does not exist set the isValid variable to false


else if (!more) {
System.out.println("Sorry, you are not a registered user! Please
sign up first");
bean.setValid(false);
}

catch (Exception ex) {


System.out.println("Log In failed: An Exception has occurred! " + ex);
}

finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
}
rs = null;
}

if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
}
stmt = null;
}

if (currentCon != null) {
try {
currentCon.close();
} catch (Exception e) {
}

currentCon = null;
}
}

return bean;
}

//add new user (register)


public void add(CustomerBean bean) throws NoSuchAlgorithmException{

custEmail = bean.getCustEmail();
custName = bean.getCustName();
custPhone = bean.getCustPhone();
custPassword = bean.getCustPassword();

try {
currentCon = ConnectionManager.getConnection();
ps=currentCon.prepareStatement("insert into customers
(custEmail,custPassword,custName,custPhone)values(?,?,?,?)");
ps.setString(1,custEmail);
ps.setString(2,custPassword);
ps.setString(3,custName);
ps.setString(4,custPhone);
ps.executeUpdate();

System.out.println("Your email is " + custEmail);


System.out.println("Your password is " + custPassword);

catch (Exception ex) {


System.out.println("failed: An Exception has occurred! " + ex);
}
finally {
if (ps != null) {
try {
ps.close();
} catch (Exception e) {
}
ps = null;
}

if (currentCon != null) {
try {
currentCon.close();
} catch (Exception e) {
}
currentCon = null;
}
}
}

public static CustomerBean getUser(CustomerBean bean) {

custEmail = bean.getCustEmail();

String searchQuery = "select * from customers where custEmail='" +


custEmail + "'";

try {
currentCon = ConnectionManager.getConnection();
stmt = currentCon.createStatement();
rs = stmt.executeQuery(searchQuery);
boolean more = rs.next();

// if user exists set the isValid variable to true


if (more) {
String email = rs.getString("custEmail");

bean.setCustEmail(email);
bean.setValid(true);
}

else if (!more) {
System.out.println("Sorry");
bean.setValid(false);
}

catch (Exception ex) {


System.out.println("Log In failed: An Exception has occurred! " + ex);
}

finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
}
rs = null;
}

if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
}
stmt = null;
}

if (currentCon != null) {
try {
currentCon.close();
} catch (Exception e) {
}

currentCon = null;
}
}

return bean;
}
}

You might also like