拾慧 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 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿