拾慧 2019-09-22 15:59 采纳率: 0%
浏览 253

练习Spring事务管理,使用jdbc模板的时候,传参提示索引超出

今天照着黑马视频练习Spring事务,使用jdbc模板的时候,传参时会报错,希望能告知,拜托

public class AccountDaoIpml extends JdbcDaoSupport implements AccountDao {
    @Override
    public int outMoney(String outAccount,Double money) {
        // TODO Auto-generated method stub
        return this.getJdbcTemplate().update("update T_AccountTransfer set FMoney=FMoney-? where FAccount=?",money,outAccount);
    }
    @Override
    public int inMoney(String inAccount,Double money) {
        return this.getJdbcTemplate().update("update T_AccountTransfer set FMoney=FMoney+? where FAccount=?",money,inAccount);
    }
}

报错信息

org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [update T_AccountTransfer set FMoney=FMoney-? where FAccount=?]; 
索引 1 超出范围。; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: 索引 1 超出范围。

如果改为直接执行sql,没有传参就测试通过,这能判断是我传参方式有问题

        return this.getJdbcTemplate().update("update T_AccountTransfer set FMoney=FMoney-1000 where FAccount='account1'");

还达不到看源码能力,不能发现这个变量传参有什么问题

    @Override
    public int update(String sql, Object... args) throws DataAccessException {
        return update(sql, newArgPreparedStatementSetter(args));
    }

而且和老师的代码一样,为什么他就没错呢

图片说明

代码:
1、Dao

public class AccountDaoIpml extends JdbcDaoSupport implements AccountDao {
    @Override
    public int outMoney(String outAccount,Double money) {
        // TODO Auto-generated method stub
        return this.getJdbcTemplate().update("update T_AccountTransfer set FMoney=FMoney-? where FAccount=?",money,outAccount);
    }
    @Override
    public int inMoney(String inAccount,Double money) {
        return this.getJdbcTemplate().update("update T_AccountTransfer set FMoney=FMoney+? where FAccount=?",money,inAccount);
    }
}

2、Service

public class AccountServiceIpml implements AccountService{

    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void accountTransfer(String outAccount, String inAccount, double money) {
        // TODO Auto-generated method stub
        accountDao.outMoney(outAccount, money);
        accountDao.inMoney(inAccount, money);
    }
}

3、test

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TranTest1 {
    @Resource(name="accountService")
    public AccountService accountService;

    @Test
    public void demo1() {
        accountService.accountTransfer("account1","account2",1000d);
//      ApplicationContext applictionContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    }
}

  • 写回答

1条回答 默认 最新

  • dabocaiqq 2019-09-22 16:50
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致