1

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);

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);

Additional information:

@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>

There is a stack trace:

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)

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

FalseScience
  • 197
  • 2
  • 17
  • Can you post the stack trace? – Denis Zavedeev Nov 11 '18 at 11:53
  • yes, sure, I will edit the question and add the stack trace – FalseScience Nov 11 '18 at 11:57
  • 1
    Which version of Spring and which version sqlite are you using? Also, please post a [mcve]. It is unclear what type `jdbcTemplate` is, `JdbcTemplate` or `NamedParameterJdbcTemplate`, although the stacktrace suggests `JdbcTemplate`; which may be the problem. Try using `?` instead of `:name` – Mark Rotteveel Nov 11 '18 at 12:00
  • Spring version 4.0.3 and SqLite version 3.24.0. All right, I have added more information. – FalseScience Nov 11 '18 at 12:09
  • @MarkRotteveel you mentioned, that there may be a problem with the JdbcTemplate, I tried NamedParameterJdbcTemplate and it worked. Thank you, so this is solved. I don't know how to vote comment as solution. – FalseScience Nov 11 '18 at 12:22
  • Consider posting your own solution as an answer. I was just guessing and didn't have time to test anything to write a real answer, so I posted the comment as a hint. – Mark Rotteveel Nov 11 '18 at 12:27

1 Answers1

2

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

FalseScience
  • 197
  • 2
  • 17