JdbcTemplate更新方法

时间:2017-07-31 12:34:40

标签: spring jdbctemplate

我向Rest Controller发送了一个POST请求。

  @RequestMapping(value = "/addUser", method = {RequestMethod.POST})
public String addThisUser(@RequestBody String json)
  {
    String result = "";
    try
    {
      JSONObject jsonObject = new JSONObject(json);
      result = addUser.addTheUser(jsonObject.getString("firstName"),
              jsonObject.getString("lastName"), jsonObject.getInt("age"),
              jsonObject.getString("email"), jsonObject.getLong("phoneNumber"),
              jsonObject.getString("username"), jsonObject.getString("password"));
    }
    catch(JSONException exception)
    {
      result = "Something went wrong. Try again later";
    }
    return result;
  }

然后我尝试使用JdbcTemplate将值插入表中。但它不起作用,我无法弄清楚为什么。这是代码(我得到一个DataAccessException):

dbConnecter.update("INSERT INTO userInformation (firstName, lastName, age, email, phoneNumber, username, password) "
            +"VALUES (?, ?, ?, ?, ?, ?, ?)",
            new Object[] {thisFirstName, thisLastName, thisAge,  thisEmail, thisPhoneNumber, thisUsername, thisPassword});
    catch(DataAccessException exception)
    {
        output = "Cannot access database";
        exception.printStackTrace();
    }

    catch(Exception exception)
    {
        output = "Unknown error";
        exception.printStackTrace();
    }

我还要注意select语句工作正常:

  String queryString = "SELECT * FROM userInformation";


  List<Map<String, Object>> listOfUsers = dbConnecter.queryForList(queryString);

老实说,我不知道为什么会这样。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

明确的错误消息有助于提供更好的答案。但如果需要,请尝试这个:

Statement statement = dbConnection.createStatement();
statement.executeUpdate(updateTableSQL);