如何使用SqlParameterSource将String的ArrayList插入数据库

时间:2013-04-28 13:39:58

标签: java spring jdbc spring-jdbc

我希望使用Spring的 SqlParameterSource 将Person bean的内容插入我的数据库表人。 Person类中的一个属性是java.util.List,它导致org.springframework.jdbc.BadSqlGrammarException

Person类如下:

package learn.spring.model;

import java.util.List;

public class Person {
    private String name;
    private int age;
    private List<String> affiliations;
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", affiliations="
                + affiliations + "]";
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public List<String> getAffiliations() {
        return affiliations;
    }
    public void setAffiliations(List<String> affiliations) {
        this.affiliations = affiliations;
    }


}

将此Person bean插入db的代码为:

String query="insert into person (name, age, affiliations) values (:name,:age,:affiliations)";
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(p);
KeyHolder key = new GeneratedKeyHolder();
System.out.println(template.update(query, paramSource, key));

我想插入的bean是:Person [name=rickesh, age=22, affiliations=[1, 2, 3]]

我收到以下异常:

Exception in thread "main" org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [insert into person (name, age, affiliations) values (?,?,?, ?, ?)]; nested exception is java.sql.SQLException: Column count doesn't match value count at row 1
    at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:98)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:603)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:843)
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:288)
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:270)
    at learn.spring.main.ApplicationRunner.createPerson(ApplicationRunner.java:46)
    at learn.spring.main.ApplicationRunner.main(ApplicationRunner.java:38)
Caused by: java.sql.SQLException: Column count doesn't match value count at row 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:233)
    at org.springframework.jdbc.core.JdbcTemplate$3.doInPreparedStatement(JdbcTemplate.java:845)
    at org.springframework.jdbc.core.JdbcTemplate$3.doInPreparedStatement(JdbcTemplate.java:1)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:587)
    ... 5 more

通过一些点击和试用,我能够理解Person.affiliations导致此异常。如何使用 SqlParameterSource 将List插入数据库表?请指教。

1 个答案:

答案 0 :(得分:0)

您的方法不正确。

编辑:您收到错误,因为Spring无法解释您要将列表存储在单行中。

您无法在单行中存储字符串列表。将您的private List<String> affiliations;转换为逗号单独的列表,它将起作用。

使用此选项从列表中生成以逗号分隔的列表:

String delim = "";
    for (Item i : list) {
        sb.append(delim).append(i);
        delim = ",";
    }