[英]java.sql.SQLException: No value specified for parameter
I am getting following error while using Spring MVC with hibernate Annotations. 在将Spring MVC与休眠注释一起使用时,出现以下错误。 I am trying to insert the record in the database using AUTO_INCREMENT
on the mysql database table Employee. 我正在尝试使用mysql数据库表Employee上的AUTO_INCREMENT
将记录插入数据库中。 On the model class I have the entry as below - 在模型类上,我有以下条目-
@Entity
@Table(name="EMPLOYEES")
public class Employee implements Serializable{
private static final long serialVersionUID = -1798070786993154676L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="emp_no")
private int empNo=0;
@Column(name="birth_date")
private Date birthDate;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="gender")
@Enumerated(EnumType.STRING)
private Gender gender=Gender.M;
@Column(name="hire_date")
private Date hireDate;
public Employee() {
// TODO Auto-generated constructor stub
}
public int getEmpNo() {
return empNo;
}
public void setEmpNo(int empNo) {
this.empNo = empNo;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
public String toString(){
return "EmpNo ::::"+this.empNo+"Birth Date ::::"+this.birthDate
+"First Name ::::"+this.firstName+" Last Name::::"+
this.lastName+" Hire Date ::::"+this.hireDate+" Gender::::"+this.gender;
}
}
On Database side I have defined the primary type column as below : 在数据库方面,我定义了主要类型列,如下所示:
Field Type Null Key Extra
emp_no int(11) NO PRI auto_increment
birth_date date YES
first_name varchar(14) YES
last_name varchar(16) YES
GENDER enum('M','F') YES
hire_date date YES
addEmployee method as follows : addEmployee方法如下:
@RequestMapping(value="/save",method=RequestMethod.POST)
public ModelAndView saveEmployee(@ModelAttribute("command")EmployeeBean employeeBean,BindingResult bindingResult){
Employee employee=prepareModel(employeeBean);
employeeService.addEmployee(employee);
Map<String, Object> model=new HashMap<String,Object>();
model.put("employees", prepareListOfEmployeeBeans(employeeService.listEmployees()));
model.put("genders", employeeBean.getGender().values());
return new ModelAndView("redirect:/add.html");
}
public Employee prepareModel(EmployeeBean employeeBean)
{
if(employeeBean!=null){
Employee employee=new Employee();
employee.setFirstName(employeeBean.getFirstName());
employee.setLastName(employeeBean.getLastName());
employee.setGender(employeeBean.getGender());
employee.setHireDate(employeeBean.getHireDate());
// employee.setEmpNo(employeeBean.getEmpNo());
employee.setBirthDate(employeeBean.getBirthDate());
// employeeBean.setEmpNo(null);
return employee;
}
return null;
}
AddEmployee JSP : AddEmployee JSP:
<%@page import="org.springframework.web.servlet.ModelAndView"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Employee Form</title>
</head>
<body>
<form:form modelAttribute="command" action="/MVC_CRUD_SPRING/save.html" method="post">
<table>
<tr>
<td colspan="2" align="center"><b>Add Employee Form</b></td>
</tr>
<tr>
<td><form:label path="empNo">Employee Number</form:label></td>
<td><form:input path="empNo" value="${employee.empNo}" readonly="true"/> </td>
</tr>
<tr>
<td><form:label path="birthDate">Birth Date</form:label> </td>
<td><form:input path="birthDate" value="${employee.birthDate}"/> </td>
</tr>
<tr>
<td><form:label path="firstName">First Name</form:label></td>
<td><form:input path="firstName" value="${employee.firstName}"/></td>
</tr>
<tr>
<td><form:label path="lastName">Last Name</form:label> </td>
<td><form:input path="lastName" value="${employee.lastName}"/> </td>
</tr>
<tr>
<td><form:label path="Gender">Gender</form:label></td>
<%-- <td><form:radiobutton path="gender" value="M"/>Male <form:radiobutton path="gender" value="F"/>Female</td> - -%>
<td><form:radiobuttons path="gender" items="${genders}"/></td>
</tr>
<tr>
<td><form:label path="hireDate">Hire Date</form:label></td>
<td><form:input path="hireDate" value="${employee.hireDate}"/> </td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
The error stack trace : 错误堆栈跟踪:
Hibernate: insert into EMPLOYEES (birth_date, first_name, last_name, gender, hire_date, emp_no) values (?, ?, ?, ?, ?, ?)
Apr 20, 2015 3:22:57 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [employee] in context with path [/MVC_CRUD_SPRING] threw exception [Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [com.cybage.projects.model.Employee]] with root cause
java.sql.SQLException: No value specified for parameter 6
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2578)
In the above code snippet while creating model of Employee I have commented out employee.setEmpNo
method in prepareModel
. 在上面的代码片段中,在创建Employee模型时,我在prepareModel
注释了employee.setEmpNo
方法。 What I think is as Employees table emp_no field has auto-increment I don't need to explicitly set empNo
value, the database should take care of this. 我认为,因为Employees表的emp_no字段具有自动增量,所以我不需要显式设置empNo
值,数据库应该注意这一点。 If I am uncommenting this , then also it is not working. 如果我对此没有评论,那么它也不起作用。
Any body who can help me understanding where 任何可以帮助我了解哪里的人
Keep the employee.setEmpNo statement as commented itself, since you are specifying the Generator here. 保留employee.setEmpNo语句作为注释本身,因为您在此处指定了Generator。
There is no need to initialize the empNo with zero value. 无需使用零值初始化empNo。 Have a look on this . 看看这个 。
The above link clearly depicts the steps to be followed, when using GenerationType.IDENTITY. 上面的链接清楚地描述了使用GenerationType.IDENTITY时要遵循的步骤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.