我正在尝试使用JDBCTemplate query()获取记录,并将参数绑定作为映射传递。Mysql正在抛出ArrayIndeOutOfBoundException。下面是stacktrace。
Exception in thread "main" org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [select * from customers where cid= :cid];
Invalid argument value: java.lang.ArrayIndexOutOfBoundsException; nested exception is java.sql.SQLException: Invalid argument value:
java.lang.ArrayIndexOutOfBoundsException
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:108)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:645)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:680)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:712)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:722)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:777)
at com.jlcnindia.spring.jdbc.JdbcCustomerDAO.getCustomerByCid(JdbcCustomerDAO.java:32)
at com.jlcnindia.spring.jdbc.Lab58.main(Lab58.java:14)
Caused by: java.sql.SQLException: Invalid argument value: java.lang.ArrayIndexOutOfBoundsException
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)
at com.mysql.jdbc.PreparedStatement.setSerializableObject(PreparedStatement.java:3887)
at com.mysql.jdbc.PreparedStatement.setObject(PreparedStatement.java:3603)
at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:428)
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$1.doInPreparedStatement(JdbcTemplate.java:686)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:629)
... 6 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
at com.mysql.jdbc.PreparedStatement.setBinaryStream(PreparedStatement.java:2871)
at com.mysql.jdbc.PreparedStatement.setSerializableObject(PreparedStatement.java:3884)
... 14 more
这是我的RowMapper课程
package com.jlcnindia.spring.jdbc;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
public class CustomerRowMapper implements RowMapper<CustomerTO> {
@Override
public CustomerTO mapRow(ResultSet rs, int rn) throws SQLException {
System.out.println("mapping ");
CustomerTO cto=new CustomerTO();
cto.setCid(rs.getInt(1));
cto.setCname(rs.getString(2));
cto.setEmail(rs.getString(3));
cto.setPhone(rs.getLong(4));
cto.setCity(rs.getString(5));
return cto;
}
}
调用查询的Dao方法
public CustomerTO getCustomerByCid(int cid) {
String sql="select * from customers where cid=:cid";
Map<String,Object> parameters=new HashMap<String, Object>();
parameters.put("cid", cid);
List<CustomerTO> list=jdbcTemp.query(sql, new CustomerRowMapper(),parameters);
if(null!=list && !list.isEmpty())
return list.get(0);
return null;
}
CustomerTO类
package com.jlcnindia.spring.jdbc;
public class CustomerTO {
private int cid;
private String cname;
private String email;
private long phone;
private String city;
public CustomerTO(){}
public CustomerTO(String cname, String email, long phone, String city) {
super();
this.cname = cname;
this.email = email;
this.phone = phone;
this.city = city;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getPhone() {
return phone;
}
public void setPhone(long phone) {
this.phone = phone;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String toString(){
return cid+"\t"+cname+"\t"+email+"\t"+phone+"\t"+city;
}
}
如果我用?而不是命名参数,并在查询中传递cid代替paramaters,如下所示
public CustomerTO getCustomerByCid(int cid) {
String sql="select * from customers where cid=?";
List<CustomerTO> list=jdbcTemp.query(sql, new CustomerRowMapper(),cid);
if(null!=list && !list.isEmpty())
return list.get(0);
return null;
}
它工作正常并给出结果,但是如果我像上面那样传递参数映射,就会得到异常。
不明白为什么
更新
Spring JBDC模板配置
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/jlcindiadb"/>
<property name="username" value="****"/>
<property name="password" value="***"/>
</bean>
<bean id="jdbcTemp" class="org.springframework.jdbc.core.JdbcTemplate" autowire="constructor">
<constructor-arg ref="dataSource"/>
</bean>
<bean id="jdbcCustomerDAO" class="com.jlcnindia.spring.jdbc.JdbcCustomerDAO"/>
最佳答案
JdbcTemplate不支持命名参数。你需要。。。为了这个。
您调用的方法是NamedParameterJdbcTemplate:
public <T> List<T> query(String sql,
RowMapper<T> rowMapper,
Object... args)
如您所见,它将参数值作为参数。因此,您实际上是将映射作为查询的唯一索引参数传递,但查询没有任何参数占位符,因此出现异常。