Spring jdbc模板&x27;没有PreparedStatementCreator,s的更新方法不会更新

Spring jdbc模板&x27;没有PreparedStatementCreator,s的更新方法不会更新,spring,spring-3,spring-jdbc,jdbctemplate,Spring,Spring 3,Spring Jdbc,Jdbctemplate,我有下表 CREATE TABLE user_use_case_control ( id integer GENERATED BY DEFAULT AS IDENTITY (START WITH 10, INCREMENT BY 1) not null, name varchar(20) not null, start_date timestamp not null, end_date timestamp null, duration timestamp null, status varchar

我有下表

CREATE TABLE user_use_case_control (
id integer GENERATED BY DEFAULT AS IDENTITY (START WITH 10, INCREMENT BY 1) not null,
name varchar(20) not null,
start_date timestamp not null,
end_date timestamp null,
duration timestamp null,
status varchar(20) null,
user_id varchar(20) not null ); 
我正在使用SpringFramework4.0.5和JdbcTemplate,我有以下插入方法(该方法的一部分)

Alpha

this.jdbcTemplate.update(

        new PreparedStatementCreator()  {

            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {

                PreparedStatement ps = connection.prepareStatement(sql,  
                Statement.RETURN_GENERATED_KEYS);

                ps.setString(1, userControl.getName());
                ps.setTimestamp(2, new Timestamp( userControl.getStartDate().getTime() ));
                ps.setTimestamp(3, new Timestamp( userControl.getEndDate().getTime() ));
                ps.setTimestamp(4, new Timestamp( userControl.getDuration().getTime() ));
                ps.setString(5, userControl.getStatus());
                ps.setString(6, userControl.getUserId());

           return ps;
          }
    }, keyHolder);
@Override
public UserControl update(UserControl userControl) {

    logger.info("UserControlJdbcRepository - update {}", userControl.toString());

    String sql = "UPDATE user_use_case_control uucc " +
                 "SET uucc.name=?, uucc.start_date=?, uucc.end_date=?, uucc.duration=?, uucc.status=? " +
                 "WHERE uucc.id=?";


    int result = this.jdbcTemplate.update(sql,
                    userControl.getName(),
                    new Timestamp( userControl.getStartDate().getTime() ),
                    new Timestamp( userControl.getEndDate().getTime() ),
                    new Timestamp( userControl.getDuration().getTime() ),
                    userControl.getStatus(),
                    userControl.getUserId());

    logger.info("result {}", result);
…
注意:

  • 我正在使用
    PreparedStatementCreator
    获取生成的密钥
  • 我正在使用
    ps.setTimestamp
    新时间戳
插入方法可以很好地工作

问题在于更新

我有以下资料:

测试版

this.jdbcTemplate.update(

        new PreparedStatementCreator()  {

            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {

                PreparedStatement ps = connection.prepareStatement(sql,  
                Statement.RETURN_GENERATED_KEYS);

                ps.setString(1, userControl.getName());
                ps.setTimestamp(2, new Timestamp( userControl.getStartDate().getTime() ));
                ps.setTimestamp(3, new Timestamp( userControl.getEndDate().getTime() ));
                ps.setTimestamp(4, new Timestamp( userControl.getDuration().getTime() ));
                ps.setString(5, userControl.getStatus());
                ps.setString(6, userControl.getUserId());

           return ps;
          }
    }, keyHolder);
@Override
public UserControl update(UserControl userControl) {

    logger.info("UserControlJdbcRepository - update {}", userControl.toString());

    String sql = "UPDATE user_use_case_control uucc " +
                 "SET uucc.name=?, uucc.start_date=?, uucc.end_date=?, uucc.duration=?, uucc.status=? " +
                 "WHERE uucc.id=?";


    int result = this.jdbcTemplate.update(sql,
                    userControl.getName(),
                    new Timestamp( userControl.getStartDate().getTime() ),
                    new Timestamp( userControl.getEndDate().getTime() ),
                    new Timestamp( userControl.getDuration().getTime() ),
                    userControl.getStatus(),
                    userControl.getUserId());

    logger.info("result {}", result);
…
  • 使用logger,我可以看到对象,并且它们的所有变量都不是空的
问题是:
jdbcTemplate.update
不更新任何内容

如果我使用

int result = this.jdbcTemplate.update(sql,
                 userControl.getName(),
                 userControl.getStartDate(),
                 userControl.getEndDate(),
                 userControl.getDuration(),
                 userControl.getStatus(),
                 userControl.getUserId());
如果使用调试级别执行调试和记录器,则可以看到以下内容:

Executing prepared SQL update
Executing prepared SQL statement [UPDATE user_use_case_control uucc SET uucc.name=?, uucc.start_date=?, uucc.end_date=?, uucc.duration=?, uucc.status=? WHERE uucc.id=?]
SQL update affected 0 rows
SQLWarning ignored: SQL state '02000', error code '-1100', message [no data]
我不明白为什么会这样

如果记录器具有信息级别,则不会出现任何错误,我的意思是不会出现某些异常

如果我使用以下内容更改方法:

Omicron

int result = this.jdbcTemplate.update(

        new PreparedStatementCreator()  {

            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {

                PreparedStatement ps = connection.prepareStatement(sql);

                ps.setString(1, userControl.getName());
                ps.setTimestamp(2, new Timestamp( userControl.getStartDate().getTime() ));
                ps.setTimestamp(3, new Timestamp( userControl.getEndDate().getTime() ));
                ps.setTimestamp(4, new Timestamp( userControl.getDuration().getTime() ));
                ps.setString(5, userControl.getStatus());
                ps.setInt(6, userControl.getId());

                return ps;
            }

        });

logger.info("result {}", result);
就在那时:它工作得很好

注意:

  • Alpha、Beta和Omicron使用
    ps.setTimestamp
    新时间戳
问题:

  • 为什么测试版失败?(消息[无数据])
  • 为什么只使用
    PreparedStatementCreator
    进行更新
  • 注意:我还有其他存储库,基于Beta
    update
    方法可以在没有
    PreparedStatementCreator
    的情况下正常工作

    仅强调一下其他存储库:

    • 其中一些数据库没有
      时间戳
      字段,只有db表模式中的
      日期
    • 其中一些数据库表模式中既没有
      时间戳
      字段,也没有
      日期
      字段
    因此,只有当涉及
    时间戳
    字段时,更新才会出现这种奇怪的行为


    提前感谢。

    您正在显示更新中使用的id的两个不同方法调用-userControl.getId()和userControl.getUserId()-这可能是一个更新有效而另一个更新无效的原因吗?哦…。这就是我工作很多小时的问题…。谢谢!