How can we save the form data from jsp file in database using spring boot?

Mayur Tripathi :

I am creating a spring boot API which basically asks the user to create an account. The account details are showed on a form. I want to fetch the details from the form and save that to the database(MYSQL).

The model class is as follows:

@Table(name = "user")
public class User {

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

The controller class is as follows:

public class MController {

    @Autowired
    private UserRepository userRepository;

    @PostMapping("/successSignUp")
    public String dataToDB(@ModelAttribute("User") User formData, Model model) {

        userRepository.save(new User(formData.getFname(), formData.getLname(), formData.getDob(), formData.getEmail(), formData.getPassword()));    
        model.addAttribute("user", new User());
        return "welcomeUser";
    }

When i am executing this code, i am getting the following error:

java.sql.SQLSyntaxErrorException: Unknown column 'p_word' in 'field list'

What am I doing wrong here? Thanks in advance.

Paplusc :

The easiest solution to fix this is to put the column names in lowercase or uppercase.

@Column(name = "pword")
private String pWord;

or

@Column(name = "PWORD")
private String pWord;

This will avoid that spring convert the name into snakecase.

Name of the columns in MySql are not case sensitive, so it will work.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=165407&siteId=1