使用PUT进行CRUD更新

时间:2016-04-18 22:03:08

标签: java crud hsqldb

我正在尝试更新HSQLDB表中的变量。在调用以下更新时,它似乎没有比这行代码更进一步:

    CloseableHttpResponse response = httpClient.execute(httpPost);

以下是完整的更新代码:

TeamClient.java

    public static void update(String id, String id1, String player1, String player2, String player3, String player4, String player5, String name) throws Exception
{
    URI uri = new URIBuilder().setScheme("http")
            .setHost("localhost")
            .setPort(8080)
            .setPath("/A00196363DonnachaOhA/rest/teams")
            .build();
    System.out.println(uri.toString());
    HttpPut httpPost = new HttpPut(uri);
    httpPost.setHeader("Accept", "text/html");

    CloseableHttpClient httpClient = HttpClients.createDefault();

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("id", id));
    nameValuePairs.add(new BasicNameValuePair("id1", id1));
    nameValuePairs.add(new BasicNameValuePair("player1", player1));
    nameValuePairs.add(new BasicNameValuePair("player2", player2));
    nameValuePairs.add(new BasicNameValuePair("player3", player3));
    nameValuePairs.add(new BasicNameValuePair("player4", player4));
    nameValuePairs.add(new BasicNameValuePair("player5", player5));
    nameValuePairs.add(new BasicNameValuePair("name", name));

    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)) ;
    System.out.println("Sending request");
    CloseableHttpResponse response = httpClient.execute(httpPost);

}

TeamDAO.java

    public void update(int id, int id2, String player1, String player2, String player3, String player4, String player5, String name) {
  Connection connection = getConnection();
  System.out.println("TRYING");  
  try
  {
      System.out.println("TRYING");
      PreparedStatement psmt = connection.prepareStatement("UPDATE TEAMS SET id=?, player1=?, player2=?, player3=?, player4=?, player5=?, name=? WHERE id =?");
      psmt.setInt(1, id);
      psmt.setInt(8, id2);
      psmt.setString(2, player1);
      psmt.setString(3, player2);
      psmt.setString(4, player3);
      psmt.setString(5, player4);
      psmt.setString(6, player5);
      psmt.setString(7, name);
      int rs = psmt.executeUpdate(); 
  }catch (SQLException e) 
  {
       e.printStackTrace();
  }  

} }

TeamResource.java

    @PUT
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("{teamId}")
public void putTeam(@FormParam("id") int id,
        @FormParam("id2") int id2,
        @FormParam("name") String name, 
        @FormParam("player1") String player1,
        @FormParam("player2") String player2, 
        @FormParam("player3") String player3, 
        @FormParam("player4") String player4, 
        @FormParam("player5") String player5,
        @Context HttpServletResponse servletResponse) throws IOException {
    System.out.println("PUT id = " + id);
    TeamDAO.instance.update(id, id2, name, player1, player2, player3, player4, player5);
}

1 个答案:

答案 0 :(得分:0)

您似乎已使用两个具有相同名称的表单参数实现了put方法。

public void putTeam(@FormParam("id") int id,
        @FormParam("id") int id2,

下一个问题:

UPDATE TEAMS SET id=?, player1=?, player2=?, player3=?, 
                 player4=?, player5=?, name=? WHERE id =?

如果第一个id和第二个id应该是相同的,那么你应该这样做

psmt.setInt(1, id);
psmt.setInt(8, id);

并摆脱id2。另一方面,如果它们应该是不同的,那么表单参数错误(见上文)会破坏查询......因为它会导致其中一个参数被忽略。

然后有这个:

nameValuePairs.add(new BasicNameValuePair("id", id));
nameValuePairs.add(new BasicNameValuePair("id1", id1));

第二个参数的名称应该是“id1”......还是“id2”?

最后,我怀疑从语义角度来看,此更新是声音。如果id字段是某个其他表中的外键的主键,那么您的更新将破坏与其他行的基于外键的关联。至少,这看起来很奇怪。