有一个示例,在此版本中适用:
String sql = "insert into album (name) VALUES (:name)";
Object[] params = new Object[] { al.getName() };
jdbcTemplate.update(sql, params);
并在此版本中给出ArrayIndexOutOfBoundsException:
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。但是第二个给出了错误。谢谢