我正在开发帐户管理系统。目标是将试用期设置到数据库中。我正在将 Timestamp "2015-10-30 22:55:48"
从 jsonObject
获取到 REST 服务中,并使用准备好的语句插入到数据库中。但是每次我得到 effected rows 0
并且意味着没有执行更新操作!
我尝试了以下一段代码,包括解析和转换:
JSONParser parser = new JSONParser();
Object obj = parser.parse(requestBody);
JSONObject jsonObject = (JSONObject) obj;
String accountExpiryStr = jsonObject.get("accountExpiry").toString();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date parsedDate = dateFormat.parse(accountExpiryStr);
Timestamp accountExpiry = new Timestamp(parsedDate.getTime()); // accountExpiry output is 2015-10-30 22:55:48.0
String requestQuery = "UPDATE table SET Account_Expiry = ?"
+ "WHERE Account_Id = ? "
+ "AND User_Id = ? ";
preparedStatement = con.prepareStatement(requestQuery);
preparedStatement.setTimestamp(1, accountExpiry);
preparedStatement.setInt(2, Integer.parseInt((String) jsonObject.get("selectedUserId")));
preparedStatement.setInt(3, Integer.parseInt((String) jsonObject.get("accountId")));
rowCount = preparedStatement.executeUpdate();
System.out.println("Effected Rows: " + rowCount);
最佳答案
Affected rows = 0
表示 UPDATE 的 where 子句与表中的任何记录都不匹配。所以你应该首先检查 where 子句是否正确。
你好像换了用户id和账号id...
而是写:
preparedStatement.setInt(2, Integer.parseInt((String) jsonObject.get("accountId")));
preparedStatement.setInt(3, Integer.parseInt((String) jsonObject.get("selectedUserId")));
关于java - PreparedStatement.setTimestamp 没有将时间戳插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33253643/