JavaSpring将Arraylist插入postgresql数据库JdbcTemplate

JavaSpring将Arraylist插入postgresql数据库JdbcTemplate,java,spring,postgresql,arraylist,prepared-statement,Java,Spring,Postgresql,Arraylist,Prepared Statement,我需要使用Spring和PostgreSQL作为数据库创建一个rest应用程序。我需要向数据库中输入一些对象,其中包含字符串的ArrayList:ArrayList。我的问题是,我找不到将arraylist设置为preparedStatement的方法。我尝试使用setObject,但显然不起作用。具体如下: public int insert(Location location) { return jdbcTemplate.update(conn -> {

我需要使用Spring和PostgreSQL作为数据库创建一个rest应用程序。我需要向数据库中输入一些对象,其中包含字符串的ArrayList:ArrayList。我的问题是,我找不到将arraylist设置为preparedStatement的方法。我尝试使用setObject,但显然不起作用。具体如下:

public int insert(Location location) {
        return jdbcTemplate.update(conn -> {
            PreparedStatement ps = conn.prepareStatement(INSERT_STMT);
            ps.setInt(1, location.getParam1());
            ps.setString(2, location.getParam2());
            ps.setString(3, location.getParam3());
            ps.setString(4, location.getParam4());
            ps.setString(5, location.getParam5());
            ps.setString(6, location.getParam6());
            ps.setString(7, location.getParam7());
            ps.setObject(8, location.getArrayListDate());
            return ps;
        });
    }
private static final String INSERT_STMT =
        " insert into locations (param1, param2, param3, param4, param5, param6, param7, dates)"
                + " values (?, ?, ?, ?, ?, ?, ?, ?)"
        ;
表格格式:

create table locations (
    param1 int primary key
  , param2 text
  , param3 text
  , param4 text
  , param5 text
  , param6 text
  , param7 text
  , dates TEXT[]
)

我怎样才能做到这一点?谢谢:)

您需要使用
conn.createArrayOf()
java.util.ArrayList
转换为
java.sql.Array
,并将其作为

pstmt.setArray("text", array)

参考

非常好,谢谢!!我只需要添加:String[]data=location.getDate().toArray(新字符串[location.getDate().size()]);将我的arraylist转换为字符串列表@王家明如果对你有用,那么至少接受z21的答案。