与KeyHolder一起使用insert时的ArrayIndexOutOfBoundsException [英] ArrayIndexOutOfBoundsException when using insert with KeyHolder

查看:103
本文介绍了与KeyHolder一起使用insert时的ArrayIndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个示例,在此版本中适用:

There is an example, which works in this version:

    String sql = "insert into album (name) VALUES (:name)";
    Object[] params = new Object[] { al.getName() };
    jdbcTemplate.update(sql, params);

并在此版本中给出ArrayIndexOutOfBoundsException:

And gives ArrayIndexOutOfBoundsException when in this version:

    KeyHolder holder = new GeneratedKeyHolder();
    String sql = "insert into album (name) VALUES (:name)";
    Object[] params = new Object[] { al.getName() };
    jdbcTemplate.update(sql, params, holder);

其他信息:

@Autowired
public void setDataSource(DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
}

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.sqlite.JDBC"></property>
    <property name="url" value="jdbc:sqlite:db/springDB.db"></property>
    <property name="username" value=""></property>
    <property name="password" value=""></property>
</bean>

有一个堆栈跟踪:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at org.sqlite.core.CorePreparedStatement.batch(CorePreparedStatement.java:128)
at org.sqlite.jdbc3.JDBC3PreparedStatement.setObject(JDBC3PreparedStatement.java:388)
at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:402)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:235)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:166)
at org.springframework.jdbc.core.ArgumentPreparedStatementSetter.doSetValue(ArgumentPreparedStatementSetter.java:66)
at org.springframework.jdbc.core.ArgumentPreparedStatementSetter.setValues(ArgumentPreparedStatementSetter.java:47)
at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:914)
at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:909)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:644)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:909)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:970)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:980)
at com.user.impls.SQLiteDAO.insertAlbum(SQLiteDAO.java:214)
at com.user.app.Start.main(Start.java:34)

这是怎么了?好像没有错误.第一个版本插入数据并创建一个ID.但是第二个给出了错误.谢谢

What is wrong here? Seems like there is no error. The first version inserts data and creates an id. But the second gives error. Thank you

推荐答案

因此,使用NamedParameterJdbcTemplate代替JdbcTemplate,作为 Mark Rotteveel建议,解决问题.

So, using NamedParameterJdbcTemplate instead of JdbcTemplate, as Mark Rotteveel suggested, solves the problem.

这篇关于与KeyHolder一起使用insert时的ArrayIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆