2016-04-30 139 views
1

我有一組數組值,我想插入到數據庫中,但它出來的結果是我不想要的。將數組值插入數據庫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); 

這是怎麼引起的,我該如何解決?

回答

1
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(); 
} 
0

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; 
} 
} 

希望這幫助。