0

我有一个映射器方法,定义如下

@InsertProvider(type = ActivityMapperSQLBuilder.class, method = "insertIntoActivityComment")
    public int insertIntoActivityComment(@Param("activityComment")ActivityComment activityComment);

对应的 SQLBuilder 方法定义为

public String insertIntoActivityComment(Map<String, Object> params) {
        ActivityComment activityComment = (ActivityComment) params
                .get("activityComment");

        params.put("fileIds",  activityComment.getFileIds());
        params.put("commentType",activityComment.getCommentType());
        params.put("commentText",activityComment.getCommentText());
        params.put("commentingUserId",activityComment.getCommentingUser().getId());
        params.put("attachments",activityComment.getAttachments());
        params.put("activityId",activityComment.getActivity().getId());

        StringBuilder builder = new StringBuilder(
                "insert into activityComment (commenttype, commenttext, commentdate, commentinguser_id, attachments, activity_id, fileids) VALUES "
                        + " (#{commentType}, #{commentText}, now(), #{commentingUserId}, #{attachments}, #{activityId}, #{fileIds} )");

        return builder.toString();
    }

每当我如下调用我的 mappermethod 时

getActivityMapper().insertIntoActivityComment(activityComment);

我遇到了以下错误。

error updating database.  Cause: org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.util.ArrayList. Use setObject() with an explicit Types value to specify the type to use.

错误可能涉及ActivityMapper.insertIntoActivityComment-Inline 设置参数时出错\n### SQL: insert into activityComment (commenttype, commenttext, commentdate,commentinguser_id, attachments, activity_id, fileids) VALUES (?, ?, now(), ?, ?, ?, ? )\n### 原因:org.postgresql.util.PSQLException:无法推断用于 java.util.ArrayList 实例的 SQL 类型。使用带有显式类型值的 setObject() 来指定要使用的类型。”

我的 ActivityComment 对象的结构是

public class ActivityComment implements Serializable, IsSerializable {
    private static final long serialVersionUID = 1L;
    private int id;
    private ActivityCommentType commentType;
    private String commentText;
    private Timestamp commentDate;
    private Assignment activity;
    private User commentingUser;
    private String attachments;
    private List<Integer> fileIds = new ArrayList<Integer>();

/*getters and setters*
}

谁能帮我将 ArrayList 插入 postgresql 数据库?

4

1 回答 1

0

您将要做的是,获取要插入的数据。然后你必须将它们添加到 arrayList 中。然后我假设您将 arrayList 传递给一个函数,该函数完成将数据插入数据库的工作。

要插入数组列表,您需要动态地构建查询。

我会为你写一个示例代码,如果你需要做一些改变。

//向ArrayList中添加数据

ArrayList<String> data=new ArrayList<String>();
data.add(value1);
data.add(value2);
data.add(value3);
data.add(value4);
insertIntoMyDatabase(data); // function that will take this arraylist form a query and do the insertion.

//insertIntoMyDatabase :

public void insertIntoMyDatabase(ArrayList<String> data) {
PreparedStatement p;
String newdata="";
  for(i=0;i<data.size();i++){ // This loop will take care of framing the query dynamically

                if(i==data.size()-1){
                    newdata+="'"+data.toArray()[i]+"'";
                }else{
                    newdata+="'"+data.toArray()[i]+"',";
                }
String query="insert into table values("+newdata+")"; // This is the framed query for inserting data
try{
p=con.prepareStatement(query);
ResultSet r=pr.executeQuery();
}catch(SQLExecption e){
e.printStacktrace();
}

}
于 2014-09-08T12:55:32.333 回答