将数组值插入数据库postgresql

问题描述:

我有一组数组值,我想插入到数据库中,但它出来的结果是我不想要的。将数组值插入数据库postgresql

我希望我的数据库可以存储这样的:

id_code | id_description 
--------------------------- 
PO1  | hello 
PO2  | hi 

但我运行的代码,其存储这样的:

id_code  | id_description 
----------------------------------- 
[PO1,P02]  | [hello, hi] 

这里是编码:

public void addID(IDInfo IDInformation) { 
     try { 
      PreparedStatement preparedStatement = connection 
        .prepareStatement("INSERT INTO id (id_code, id_description)" 
          + "VALUES (?, ?)"); 
      preparedStatement.setString(1, Arrays.toString(IDInformation.getIDCode())); 
      preparedStatement.setString(2, Arrays.toString(IDInformation.getIDDescription())); 
      preparedStatement.executeUpdate(); 
      } 
     } 

     catch (SQLException e){ 
      e.printStackTrace(); 
     } 
    } 

控制器:

  String action = request.getParameter("action"); 
      IDInfo idInfo = new IDInfo(); 
      String[] Code = request.getParameterValues("idCode"); 
      String [] Describe = request.getParameterValues("idDescription"); 


      idInfo.setidCode(Code); 
      idInfo.setidDescription(Describe); 

这是怎么引起的,我该如何解决?

PreparedStatement preparedStatement = connection 
    .prepareStatement("INSERT INTO id (id_code, id_description)" 
        + "VALUES (?, ?)"); 
preparedStatement.setString(1, Arrays.toString(IDInformation.getIDCode())); 
preparedStatement.setString(2, Arrays.toString(IDInformation.getIDDescription())); 
preparedStatement.executeUpdate(); 

阅读上面的代码。它有什么作用?它执行一个请求,创建一个包含两个值的单行。

你想要的是两行。

所以,你应该两次执行语句:

  • 一个与第一码和第一描述为参数
  • 一个与第二个代码和第二描述为参数

你通过不使用对象来使自己的任务变得困难。而不是有两个数组,一个包含代码,另一个包含描述,您应该有一个包含对象的数组(或集合)。并且每个对象都有一个代码和一个说明:

PreparedStatement preparedStatement = connection 
    .prepareStatement("INSERT INTO id (id_code, id_description)" 
        + "VALUES (?, ?)"); 
for (Product product : products) { 
    preparedStatement.setString(1, product.getCode()); 
    preparedStatement.setString(2, product.getDescription()); 
    preparedStatement.executeUpdate(); 
} 

JB Nizet指出的根本原因是您的addID函数。 以下几行是准备单个数据库更新语句并包含所有数组元素。

//Arrays.toString(IDInformation.getIDCode())-> Returns [PO1,P02] 
preparedStatement.setString(1, Arrays.toString(IDInformation.getIDCode())); 
//Arrays.toString(IDInformation.getIDDescription())-> Returns [hello, hi] 
preparedStatement.setString(2, Arrays.toString(IDInformation.getIDDescription())); 
preparedStatement.executeUpdate(); 

你应该做的,而不是理想的是循环遍历所有IDINFO的和准备的语句为每个即

public void addID(List<IDInfo> IDInfos) { 
    try { 
    for(IDInfo idInfo : IDInfos) { 
     //Loop over all IdInfo & update db 
     PreparedStatement preparedStatement = connection 
       .prepareStatement("INSERT INTO id (id_code, id_description)" 
         + "VALUES (?, ?)"); 
     preparedStatement.setString(1, idInfo.getIDCode()); 
     preparedStatement.setString(2, idInfo.getIDDescription()); 
     preparedStatement.executeUpdate(); 
    } 
    } 
    catch (SQLException e){ 
     e.printStackTrace(); 
    } 
} 

的IDINFO这里可以进行一个简单的POJO:

public class IDInfo { 

private String IDCode; 

private String IDDescription; 

public String getIDCode() { 
    return IDCode; 
} 

public void setIDCode(String IDCode) { 
    this.IDCode = IDCode; 
} 

public String getIDDescription() { 
    return IDDescription; 
} 

public void setIDDescription(String IDDescription) { 
    this.IDDescription = IDDescription; 
} 
} 

希望这帮助。