You are on page 1of 16

ACCOUNT CONTROLLER

package com.customer.controller;
import com.customer.dao.AccountDAO;
import com.customer.entities.Account;
import java.util.*;

public class AccountController


{
private AccountDAO accountDAO;

public AccountController()
{this.accountDAO = new AccountDAO();}

public boolean addAccount(Account account)


{return accountDAO.addAccount(account);}

public boolean updateAccount(Account account,int id)


{return accountDAO.updateAccount(account,id);}

public boolean deleteAccount(int id)


{return accountDAO.deleteAccount(id);}

public Account getAccount(int id)


{return accountDAO.getAccount(id);}

public ArrayList<Account> getAllAccount()


{return accountDAO.getAllAccount();}
}

CUSTOMER CONTROLLER

package com.customer.controller;
import com.customer.dao.CustomerDAO;
import com.customer.entities.Customer;
import java.util.*;

public class CustomerController


{
private CustomerDAO customerDAO;

public CustomerController()
{this.customerDAO = new CustomerDAO();}

public boolean addCustomer(Customer customer)


{return customerDAO.addCustomer(customer);}

public boolean updateCustomer(Customer customer,int id)


{return customerDAO.updateCustomer(customer,id);}

public boolean deleteCustomer(int id)


{return customerDAO.deleteCustomer(id);}

public Customer getCustomer(int id)


{return customerDAO.getCustomer(id);}

public ArrayList<Customer> getAllCustomer()


{return customerDAO.getAllCustomer();}
/*public boolean validateCustomer(String username)
{return customerDAO.validateCustomer();}*/
}

LOGIN CONTROLLER

package com.customer.controller;
import com.customer.dao.*;
import com.customer.entities.*;
import com.customer.helper.Helper;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class LoginController


{
private LoginDAO loginDAO;

public LoginController()
{this.loginDAO = new LoginDAO();}

public boolean validateUsername(String username)


{
return loginDAO.validateUsername(username);
}

public ArrayList<Login> getAllLogin()


{
ArrayList<Login> login = new ArrayList();
String query = "Select * from login";
PreparedStatement preparedStatement =
Helper.getConnectionPreparedStatement(query);
try{
ResultSet resultset = preparedStatement.executeQuery();
while(resultset.next())
{ String username = resultset.getString(1);
String password = resultset.getString(2);

Login templogin= new Login(username,password);


login.add(templogin);}
}
catch(SQLException ex)
{ ex.printStackTrace(); }

return login;
}
}

ACCOUNT DAO

package com.customer.dao;

import com.customer.entities.Account;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import com.customer.helper.Helper;

public class AccountDAO


{
public boolean addAccount(Account account)
{
boolean flag = false;
String query = "Insert into account values (?,?,?,?,?,?)";
PreparedStatement preparedStatement =
Helper.getConnectionPreparedStatement(query);
try{
preparedStatement.setInt(1,account.getcustid());
preparedStatement.setInt(2,account.getaccid());
preparedStatement.setString(3,account.getacctype());
preparedStatement.setString(4,account.getstatus());
preparedStatement.setString(5,account.getmessage());
preparedStatement.setString(6,account.getlastUpdated());

int temp = preparedStatement.executeUpdate();


if(temp>=1)
flag = true;
} catch(SQLException ex)
{ ex.printStackTrace(); }

Helper.closeConnectionPreparedStatement();
return flag;
}

public boolean deleteAccount(int id)


{
boolean flag = true;
String query = "delete from account where custid=?";
PreparedStatement preparedStatement =
Helper.getConnectionPreparedStatement(query);
try{
preparedStatement.setInt(1,id);

int temp = preparedStatement.executeUpdate();


if(temp>=1)
flag = true; } catch(SQLException ex)
{ ex.printStackTrace(); }
Helper.closeConnectionPreparedStatement();
return flag;
}

public ArrayList<Account> getAllAccount()


{
ArrayList<Account> account = new ArrayList();
String query = "Select * from account";
PreparedStatement preparedStatement =
Helper.getConnectionPreparedStatement(query);
try{
ResultSet resultset = preparedStatement.executeQuery();
while(resultset.next())
{ int custID = resultset.getInt(1);
int accID = resultset.getInt(2);
String acctype = resultset.getString(3);
String status = resultset.getString(4);
String message = resultset.getString(5);
String lastUpdated = resultset.getString(6);
Account tempaccount= new Account(custID, accID, acctype, status, message,
lastUpdated);
account.add(tempaccount);}
}
catch(SQLException ex)
{ ex.printStackTrace(); }

return account;
}

/*public boolean updateCustomer(Customer customer,int id)


{
boolean flag = false;
String query = "update Customer set name=?,purchaseAmount=?, contact=? where
id=?";
PreparedStatement preparedStatement =
Helper.getConnectionPreparedStatement(query);
try{
preparedStatement.setString(1,customer.getName());
preparedStatement.setInt(2,customer.getPurcahseAmount());
preparedStatement.setString(3,customer.getContact());
preparedStatement.setInt(4,id);

int temp = preparedStatement.executeUpdate();


if(temp>=1)
flag = true; } catch(SQLException ex)
{ ex.printStackTrace(); }
Helper.closeConnectionPreparedStatement();
return flag;
}

public boolean deleteCustomer(int id)


{
boolean flag = true;
String query = "delete from Customer where id=?";
PreparedStatement preparedStatement =
Helper.getConnectionPreparedStatement(query);
try{
preparedStatement.setInt(1,id);

int temp = preparedStatement.executeUpdate();


if(temp>=1)
flag = true; } catch(SQLException ex)
{ ex.printStackTrace(); }
Helper.closeConnectionPreparedStatement();
return flag;
}

public Customer getCustomer(int id)


{
ArrayList<Customer> customer = new ArrayList();
String query = "Select * from Customer where id=?";
PreparedStatement preparedStatement =
Helper.getConnectionPreparedStatement(query);
try{
preparedStatement.setInt(1,id);
ResultSet resultset = preparedStatement.executeQuery();
while(resultset.next())
{ int cid = resultset.getInt(1);
String name = resultset.getString(2);
int purchaseAmount = resultset.getInt(3);
String contact = resultset.getString(4);
Customer tempcustomer = new Customer(name, cid, purchaseAmount,
contact);
customer.add(tempcustomer);}
}
catch(SQLException ex){ ex.printStackTrace();}

return customer.get(0);
}

public ArrayList<Customer> getAllCustomer()


{
ArrayList<Customer> customer = new ArrayList();
String query = "Select * from Customer";
PreparedStatement preparedStatement =
Helper.getConnectionPreparedStatement(query);
try{
ResultSet resultset = preparedStatement.executeQuery();
while(resultset.next())
{ int cid = resultset.getInt(1);
String name = resultset.getString(2);
int purchaseAmount = resultset.getInt(3);
String contact = resultset.getString(4);
Customer tempcustomer = new Customer(name, cid, purchaseAmount,
contact);
customer.add(tempcustomer);}
}
catch(SQLException ex)
{ ex.printStackTrace(); }

return customer;
}*/
}

CUSTOMER DAO

package com.customer.dao;
import com.customer.entities.Customer;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import com.customer.helper.Helper;

public class CustomerDAO


{
public boolean addCustomer(Customer customer)
{
boolean flag = false;
String query = "Insert into customer values (?,?,?,?,?)";
PreparedStatement preparedStatement =
Helper.getConnectionPreparedStatement(query);
try{
preparedStatement.setInt(1,customer.getssnid());
preparedStatement.setInt(2,customer.getcustomerid());
preparedStatement.setString(3,customer.getstatus());
preparedStatement.setString(4,customer.getmessage());
preparedStatement.setString(5,customer.getlastUpdated());

int temp = preparedStatement.executeUpdate();


if(temp>=1)
flag = true;
} catch(SQLException ex)
{ ex.printStackTrace(); }

Helper.closeConnectionPreparedStatement();
return flag;
}

public boolean deleteCustomer(int id)


{
boolean flag = true;
String query = "delete from customer where ssnid=?";
PreparedStatement preparedStatement =
Helper.getConnectionPreparedStatement(query);
try{
preparedStatement.setInt(1,id);

int temp = preparedStatement.executeUpdate();


if(temp>=1)
flag = true; } catch(SQLException ex)
{ ex.printStackTrace(); }
Helper.closeConnectionPreparedStatement();
return flag;
}

public ArrayList<Customer> getAllCustomer()


{
ArrayList<Customer> customer = new ArrayList();
String query = "Select * from Customer";
PreparedStatement preparedStatement =
Helper.getConnectionPreparedStatement(query);
try{
ResultSet resultset = preparedStatement.executeQuery();
while(resultset.next())
{ int ssnID = resultset.getInt(1);
int customerID = resultset.getInt(2);
String status = resultset.getString(3);
String message = resultset.getString(4);
String lastUpdated = resultset.getString(5);
Customer tempcustomer = new Customer(ssnID, customerID, status, message,
lastUpdated);
customer.add(tempcustomer);}
}
catch(SQLException ex)
{ ex.printStackTrace(); }

return customer;
}
public boolean updateCustomer(Customer customer,int id)
{
boolean flag = false;
String query = "update Customer set ssnid
= ?,custid=?,status=?,mess=?,lastupdated=? where ssnid = ? ";
PreparedStatement preparedStatement =
Helper.getConnectionPreparedStatement(query);
try{
preparedStatement.setInt(1,customer.getssnid());
preparedStatement.setInt(2,customer.getcustomerid());
preparedStatement.setString(3,customer.getstatus());
preparedStatement.setString(4,customer.getmessage());
preparedStatement.setString(5,customer.getlastUpdated());
preparedStatement.setInt(6,id);

int temp = preparedStatement.executeUpdate();


if(temp>=1)
flag = true; } catch(SQLException ex)
{ ex.printStackTrace(); }
Helper.closeConnectionPreparedStatement();
return flag;
}

/*public boolean validateCustomer(String username)


{
boolean flag = false;

return flag;
}*/

/*public boolean updateCustomer(Customer customer,int id)


{
boolean flag = false;
String query = "update Customer set name=?,purchaseAmount=?, contact=? where
id=?";
PreparedStatement preparedStatement =
Helper.getConnectionPreparedStatement(query);
try{
preparedStatement.setString(1,customer.getName());
preparedStatement.setInt(2,customer.getPurcahseAmount());
preparedStatement.setString(3,customer.getContact());
preparedStatement.setInt(4,id);

int temp = preparedStatement.executeUpdate();


if(temp>=1)
flag = true; } catch(SQLException ex)
{ ex.printStackTrace(); }
Helper.closeConnectionPreparedStatement();
return flag;
}

public boolean deleteCustomer(int id)


{
boolean flag = true;
String query = "delete from Customer where id=?";
PreparedStatement preparedStatement =
Helper.getConnectionPreparedStatement(query);
try{
preparedStatement.setInt(1,id);

int temp = preparedStatement.executeUpdate();


if(temp>=1)
flag = true; } catch(SQLException ex)
{ ex.printStackTrace(); }
Helper.closeConnectionPreparedStatement();
return flag;
}

public Customer getCustomer(int id)


{
ArrayList<Customer> customer = new ArrayList();
String query = "Select * from Customer where id=?";
PreparedStatement preparedStatement =
Helper.getConnectionPreparedStatement(query);
try{
preparedStatement.setInt(1,id);
ResultSet resultset = preparedStatement.executeQuery();
while(resultset.next())
{ int cid = resultset.getInt(1);
String name = resultset.getString(2);
int purchaseAmount = resultset.getInt(3);
String contact = resultset.getString(4);
Customer tempcustomer = new Customer(name, cid, purchaseAmount,
contact);
customer.add(tempcustomer);}
}
catch(SQLException ex){ ex.printStackTrace();}

return customer.get(0);
}

public ArrayList<Customer> getAllCustomer()


{
ArrayList<Customer> customer = new ArrayList();
String query = "Select * from Customer";
PreparedStatement preparedStatement =
Helper.getConnectionPreparedStatement(query);
try{
ResultSet resultset = preparedStatement.executeQuery();
while(resultset.next())
{ int cid = resultset.getInt(1);
String name = resultset.getString(2);
int purchaseAmount = resultset.getInt(3);
String contact = resultset.getString(4);
Customer tempcustomer = new Customer(name, cid, purchaseAmount,
contact);
customer.add(tempcustomer);}
}
catch(SQLException ex)
{ ex.printStackTrace(); }

return customer;
}*/
}

LOGIN DAO
package com.customer.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.customer.entities.*;
import com.customer.helper.Helper;

public class LoginDAO {

public boolean validateUsername(String username)


{
boolean flag = false;
String query = "SELECT * FROM LOGIN WHERE username = ?";
PreparedStatement preparedStatement =
Helper.getConnectionPreparedStatement(query);
try{
preparedStatement.setString(1,username);
//preparedStatement.setInt(2,customer.getcustomerid());
//preparedStatement.setString(3,customer.getstatus());
//preparedStatement.setString(4,customer.getmessage());
//preparedStatement.setString(5,customer.getlastUpdated());
//preparedStatement.setInt(6,id);

int temp = preparedStatement.executeUpdate();


if(temp>=1)
flag = true; } catch(SQLException ex)
{ ex.printStackTrace(); }
Helper.closeConnectionPreparedStatement();
return flag;
}
}

ACCOUNT

package com.customer.entities;

public class Account {

private int custid;

private int accid;

private String acctype;

private String status;

private String mess;

private String lastupdated;


//private String message;

//private int amount;

public Account()
{
}

public Account(int custid, int accid, String acctype, String status, String
mess, String lastupdated)
{

this.custid = custid;
this.accid = accid;
this.acctype = acctype;
this.status = status;
this.mess = mess;
this.lastupdated = lastupdated;

public int getcustid()


{
return this.custid;
}

public int getaccid()


{
return this.accid;
}

public String getacctype()


{
return this.acctype;
}

public String getstatus()


{
return this.status;
}

public String getmessage()


{
return this.mess;
}

public String getlastUpdated()


{
return this.lastupdated;
}

@Override
public String toString() {

return "CustomerID = " + custid + ", AccountID = " + accid + ",


AccountType = " + acctype + ", Status = " + status
+ ", Message = " + mess + ", Last Updated = " + lastupdated
+ "]";
}

CUSTOMER
package com.customer.entities;

public class Customer {

private int ssnID;

private int customerID;

private String status;

private String message;

private String lastUpdated;

public Customer()
{

public Customer(int ssnID, int customerID, String status, String message,


String lastUpdated)
{

this.ssnID = ssnID;
this.customerID = customerID;
this.status = status;
this.message = message;
this.lastUpdated = lastUpdated;

public int getssnid()


{
return this.ssnID;
}

public int getcustomerid()


{
return this.customerID;
}

public String getstatus()


{
return this.status;
}

public String getmessage()


{
return this.message;
}

public String getlastUpdated()


{
return this.lastUpdated;
}

@Override
public String toString() {
return "Customer [SSNID = " + ssnID + ", CustomerID = " + customerID +
", Status = " + status + ", Message = " + message
+ ", Last Updated = " + lastUpdated + "]";
}

LOGIN

package com.customer.entities;

public class Login {

private String username;

private String password;

//private String message;

//private int amount;

public Login()
{

public Login(String username, String password)


{

this.username = username;
this.password = password;

/*public int getcustid()


{
return this.custid;
}

public int getaccid()


{
return this.accid;
}

public String getacctype()


{
return this.acctype;
}

public String getstatus()


{
return this.status;
}

public String getmessage()


{
return this.mess;
}

public String getlastUpdated()


{
return this.lastupdated;
}*/

@Override
public String toString() {

return "username = " + username + ", password = " + password + "]";


}

BASE QUERY

package com.customer.helper;

import java.sql.Statement;

public class BaseQuery {


public static void makeTable(boolean istobeDeleted)throws Exception
{
if(istobeDeleted)
{
String query = "Drop table customer";
Statement statement = Helper.getConnectionStatement();
statement.execute(query);

query = "Create table customer("


+"ssnid int,"
+"custid int,"
+"status varchar(50),"
+"mess varchar(50),"
+"lastupdated varchar(50))";
statement.execute(query);
}
else{
String query = "Create table customer("
+"ssnid int,"
+"custid int,"
+"status varchar(50),"
+"mess varchar(50),"
+"lastupdated varchar(50))";
Statement statement = Helper.getConnectionStatement();
statement.execute(query);
}

Helper.closeConnectionStatement();
}

public static void makeTableAccount(boolean istobeDeleted)throws Exception


{
if(istobeDeleted)
{
String query = "Drop table account";
Statement statement = Helper.getConnectionStatement();
statement.execute(query);

query = "Create table account("


+"custid int,"
+"accid int,"
+"accType varchar(50),"
+"status varchar(50),"
+"mess varchar(50),"
+"lastupdated varchar(50))";
statement.execute(query);
}
else{
String query = "Create table account("
+"custid int,"
+"accid int,"
+"accType varchar(50),"
+"status varchar(50),"
+"mess varchar(50),"
+"lastupdated varchar(50))";
Statement statement = Helper.getConnectionStatement();
statement.execute(query);
}

Helper.closeConnectionStatement();
}

public static void makeTableLogin()throws Exception


{
/*if(istobeDeleted)
{
String query = "Drop table account";
Statement statement = Helper.getConnectionStatement();
statement.execute(query);

query = "Create table account("


+"custid int,"
+"accid int,"
+"accType varchar(50),"
+"status varchar(50),"
+"mess varchar(50),"
+"lastupdated varchar(50))";
statement.execute(query);
}
else{*/

String query = "Create table login("


+"username varchar(10),"
+"password varchar(10))";
Statement statement = Helper.getConnectionStatement();
statement.execute(query);

Helper.closeConnectionStatement();
}

}
HELPER

package com.customer.helper;

import java.sql.*;

public class Helper {

private static Connection connection; // making the connection with database


private static Statement statement; // both are using for the queries
private static PreparedStatement preparedStatement;

public static Statement getConnectionStatement()


{
try{ Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
// Need to import the jar file

Helper.connection =
DriverManager.getConnection("jdbc:derby:demoDB;create=true");
Helper.statement = Helper.connection.createStatement();

return Helper.statement;
}
catch(ClassNotFoundException ex){
ex.printStackTrace();
return null;
}
catch(SQLException ex){
ex.printStackTrace();
return null; }
}

public static PreparedStatement getConnectionPreparedStatement(String sql)


{
try{ Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
// Need to import the jar file

Helper.connection =
DriverManager.getConnection("jdbc:derby:demoDB;create=true");
Helper.preparedStatement =
Helper.connection.prepareStatement(sql);

return Helper.preparedStatement;
}
catch(ClassNotFoundException ex){
ex.printStackTrace();
return null;
}
catch(SQLException ex)
{ ex.printStackTrace();
return null;}
}
// Create Closing Function

public static void closeConnectionStatement()


{
try {Helper.statement.close();
Helper.connection.close();}
catch(SQLException ex)
{ ex.printStackTrace();}
}

public static void closeConnectionPreparedStatement()


{
try{Helper.preparedStatement.close();
Helper.preparedStatement.close(); }
catch(SQLException ex)
{
ex.printStackTrace();
}
}

You might also like