0% found this document useful (0 votes)
49 views4 pages

Customer DAO

This Java code defines a CustomerDAO class with methods for customer login, registration, and account retrieval from a database. The login method queries the customers table to validate the user's email and password and sets a valid flag on the returned CustomerBean. The add method inserts a new customer record into the table. The getUser method queries for a user by email to return their profile or set the valid flag to false if not found. All methods open and close database connections using the ConnectionManager.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views4 pages

Customer DAO

This Java code defines a CustomerDAO class with methods for customer login, registration, and account retrieval from a database. The login method queries the customers table to validate the user's email and password and sets a valid flag on the returned CustomerBean. The add method inserts a new customer record into the table. The getUser method queries for a user by email to return their profile or set the valid flag to false if not found. All methods open and close database connections using the ConnectionManager.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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