且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用NamedParameterJdbcTemplate将数据发送到数据库

更新时间:2023-01-01 16:51:39

您正在SQL查询中使用位置参数.这不起作用,因为您使用的是NamedParameterJdbcTemplate,它需要命名参数.只需修改查询以使用类似这样的命名参数即可:

You are using positional parameters in your SQL query. This doesn't work because you are using the NamedParameterJdbcTemplate which requires named parameters. Just modify your query to use named parameters like that:

"INSERT INTO employee(firstname, lastname, username, password) VALUES(:firstname, :lastname, :username, :password)"

还可以将Employee对象用作参数源:

Also you could use the Employee object as a parameter source:

public static int saveRecord(Employee e) {
        NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(dataSource);

        SqlParameterSource params = new BeanPropertySqlParameterSource(e);

        int result = template.update(sql, params);
        return result;
   }

BeanPropertySqlParameterSource将使用雇员"属性来设置参数.

The BeanPropertySqlParameterSourcewill use Employees properties to set the parameters.