java.sql.SQLException:没有为参数指定值

时间:2015-04-20 10:34:33

标签: hibernate spring-mvc

使用Spring MVC和hibernate Annotations时出现以下错误。我试图在mysql数据库表Employee上使用AUTO_INCREMENT在数据库中插入记录。在模型类中,我有如下条目 -

@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;
}
}

在数据库方面,我已经定义了主要类型列,如下所示:

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方法如下:

@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:

<%@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 &nbsp;&nbsp;&nbsp;&nbsp;<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>

错误堆栈跟踪:

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)

在创建Employee模型的上面的代码片段中,我在employee.setEmpNo中注释了prepareModel方法。我认为Employees表emp_no字段有自动增量我不需要显式设置empNo值,数据库应该处理这个问题。如果我取消注释,那么它也无效。

任何可以帮助我了解

的机构

1 个答案:

答案 0 :(得分:0)

  1. 将employee.setEmpNo语句保持为已注释,因为您在此处指定了生成器。

  2. 无需使用零值初始化empNo。请查看this

  3. 以上链接清楚地描述了使用GenerationType.IDENTITY时要遵循的步骤。