我的问题是关于JDBC SQL。我想要做的是使用员工实体中的用户名来检索我的登录密码。我有一个datainitalization单例bean来预先填充我的系统管理员。
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 ?")
我使用用户名
检索登录密码的方法 @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();
}
这是我尝试登录方法。
@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!");
}
}
最后,这是我为系统管理员提到的单例bean
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);
}
}
这是我员工实体类的一部分。
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;
感谢大家阅读本文!希望我能尽快找到解决方案。 :)
PS:这个问题已经解决了。我还为任何需要检查是否需要保留任何保留字的人发布了一个链接: https://dev.mysql.com/doc/refman/5.7/en/keywords.html
答案 0 :(得分:0)
由于PASSWORD
是保留的工作,您需要在映射中明确指定转义的列名称
@Column(length = 32, nullable = false)
private String password;
应该改为
@Column(name = "`PASSWORD`", length = 32, nullable = false)
private String password;
或
@Column(name = "\"PASSWORD\"", length = 32, nullable = false)
private String password;
基于MySQL配置支持的变体(一些细节here)