My problem is with regards to JDBC SQL. What i want to do is use my username in the employee entity to retrieve the password for my login. I have a datainitalization singleton bean to prepopulate my system admin.
Error Message in GlassFish.
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150605-
31e8258): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column
'PASSWORD' in 'field list'
Error Code: 1054
Call: SELECT EMPLOYEEID, ACCESSRIGHT, ADDRESSLINE1, ADDRESSLINE2,
CONTACTNUMBER, FIRSTNAME, LASTNAME, PASSWORD, POSTALCODE, USERNAME FROM
EMPLOYEE WHERE USERNAME LIKE ?
bind => [1 parameter bound]
Query: ReadAllQuery(referenceClass=Employee sql="SELECT EMPLOYEEID,
ACCESSRIGHT, ADDRESSLINE1, ADDRESSLINE2, CONTACTNUMBER, FIRSTNAME, LASTNAME,
PASSWORD, POSTALCODE, USERNAME FROM EMPLOYEE WHERE USERNAME LIKE ?")
My method to retrieve password for login using username
@Override
public Employee retrievePasswordByUsername(String username) throws UserNameNotFoundException
{
//SELECT sl FROM SystemLogin sl WHERE sl.userName = :inUserName
return (Employee) em.createQuery("SELECT e FROM Employee e WHERE e.username LIKE :inUsername").setParameter("inUsername", username).getResultList();
}
This is my attempt to do a login method.
@Override
public Employee employeeLogin(String username, String password) throws
InvalidLoginCredentialException
{
Employee employee = null;
try {
employee = retrievePasswordByUsername(username);
} catch (UserNameNotFoundException ex) {
Logger.getLogger(EmployeeController.class.getName()).log(Level.SEVERE, null, ex);
}
if(employee.getPassword().equals(password))
{
return employee;
}
else
{
throw new InvalidLoginCredentialException("Username does not exist or invalid password!");
}
}
Lastly, this is the singleton bean that i mention for my system admin
private void initializeData()
{
try {
employeeControllerLocal.createNewEmployee(new Employee("Default",
"Administrator", "99999999", "NUS", "Computing","599999",
EmployeeAccessRight.ADMINISTRATOR, "admin", "password"));
} catch (EmployeeExistException ex) {
Logger.getLogger(DataInitializationSessionBean.class.getName()).log(Level.SEVERE
, null, ex);
} catch (GeneralException ex) {
Logger.getLogger(DataInitializationSessionBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
This is part of my employee entity class.
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long employeeId;
@Column(length = 32, nullable = false)
private String firstName;
@Column(length = 32, nullable = false)
private String lastName;
@Column(length = 32, nullable = false)
private String contactNumber;
@Column(length = 32, nullable = false)
private String addressLine1;
@Column(length = 32, nullable = false)
private String addressLine2;
@Column(length = 32, nullable = false)
private String postalCode;
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private EmployeeAccessRight accessRight;
@Column(unique = true)
private String username;
@Column(length = 32, nullable = false)
private String password;
Thanks everyone for reading this! Hope i'll find a solution soon. :)
PS: This question has been solved. I've also posted one link for anyone who needs to check if they need to escape any reserved words: https://dev.mysql.com/doc/refman/5.7/en/keywords.html