You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot存JSP表单数据到MySQL报错:未知列'p_word'

Fixing the "Unknown column 'p_word' in 'field list'" Error in Your Spring Boot API

Let's break down the root cause of your error and walk through the fixes step by step:

1. Main Issue: Missing @Entity Annotation

The biggest problem is that your User class isn't marked as a JPA entity. Without the @Entity annotation, Spring Data JPA doesn't recognize the class as a database entity, so it ignores your @Column annotations entirely. Instead, it uses the default naming strategy which converts camelCase field names to snake_case (so pWord becomes p_word), but your database table has a pWord column—hence the "unknown column" error.

Fix: Add @Entity to Your User Class

Update your model to include the @Entity annotation, plus add a no-arg constructor (required by JPA) and proper ID generation:

import jakarta.persistence.*; // or javax.persistence.* for older Spring Boot versions

@Entity // This is critical!
@Table(name = "user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) // Auto-increment ID for MySQL
    @Column(name = "ID")
    private int id;
    
    @Column(name = "Fname")
    private String fName;
    
    @Column(name = "Lname")
    private String lName;
    
    @Column(name = "dob")
    private String dob;
    
    @Column(name = "email")
    private String email;
    
    @Column(name = "pWord")
    private String pWord;

    // No-arg constructor required by JPA
    public User() {}

    // Parameterized constructor (exclude ID since it's auto-generated)
    public User(String fName, String lName, String dob, String email, String pWord) {
        this.fName = fName;
        this.lName = lName;
        this.dob = dob;
        this.email = email;
        this.pWord = pWord;
    }

    // Getters and Setters (make sure these match your form field names)
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }

    public String getfName() { return fName; }
    public void setfName(String fName) { this.fName = fName; }

    public String getlName() { return lName; }
    public void setlName(String lName) { this.lName = lName; }

    public String getDob() { return dob; }
    public void setDob(String dob) { this.dob = dob; }

    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }

    public String getpWord() { return pWord; }
    public void setpWord(String pWord) { this.pWord = pWord; }

    // Optional: Add this if your form uses "password" instead of "pWord"
    public void setPassword(String password) {
        this.pWord = password;
    }
}

2. Fix Controller Logic

Your controller is creating a new User object manually, but you can save the already-populated formData instance directly (since @ModelAttribute handles binding form fields to the object):

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

@Controller // Don't forget this annotation!
public class MController {
    @Autowired
    private UserRepository userRepository;

    @PostMapping("/successSignUp")
    public String dataToDB(@ModelAttribute("User") User formData, Model model) {
        userRepository.save(formData); // Save the populated form data directly
        model.addAttribute("user", new User());
        return "welcomeUser";
    }
}

3. Verify Form Field Names

Ensure your HTML form fields match the getter/setter names in your User class. For example:

  • If your form uses <input name="password">, add the setPassword method to your User class (as shown above) to map it to the pWord field.
  • For fields like first name, use <input name="fName"> to match the setfName method.

4. Check Database Table Structure

Double-check that your MySQL user table has the exact column names specified in your @Column annotations:

  • ID (auto-incrementing integer)
  • Fname (varchar)
  • Lname (varchar)
  • dob (varchar or date type)
  • email (varchar)
  • pWord (varchar)

Content of this question originates from Stack Exchange, asked by Mayur Tripathi.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:05:10