在更新 JSP 记录时设置数值为 0。

huangapple go评论96阅读模式
英文:

set value = 0 while updating record in jsp

问题

  1. im trying to update a column using jsp
  2. here i want my count column value to be updated as zero
  3. count = 0
  4. how can i do that. through this code value is not updating
  5. String code = request.getParameter("code");
  6. Connection conn = null;
  7. Statement st=null;
  8. ResultSet rs = null;
  9. PreparedStatement ps = null;
  10. int count = 0;
  11. try{
  12. conn = DataBaseConnection.initializeDatabase();
  13. String query1 = null;
  14. conn.setAutoCommit(false);
  15. query1 = "update employee set count = ? where code= '" +code+ "' ";
  16. ps = conn.prepareStatement(query1);
  17. ps.setInt(1, count);
  18. ps.executeUpdate();
  19. }
  20. catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. here is my record in db
  24. employee table
  25. CODE VARCHAR2(12)
  26. COUNT NUMBER(3)
英文:

im trying to update a column using jsp
here i want my count column value to be updated as zero
count = 0
how can i do that. through this code value is not updating

  1. String code = request.getParameter("code");
  2. Connection conn = null;
  3. Statement st=null;
  4. ResultSet rs = null;
  5. PreparedStatement ps = null;
  6. int count = 0;
  7. try{
  8. conn = DataBaseConnection.initializeDatabase();
  9. String query1 = null;
  10. conn.setAutoCommit(false);
  11. query1 = "update employee set count = ? where code= ' +code+' ";
  12. ps = conn.prepareStatement(query1);
  13. ps.setInt(1, count);
  14. ps.executeUpdate();
  15. }
  16. catch (Exception e) {
  17. e.printStackTrace();
  18. }

here is my record in db
employee table
CODE VARCHAR2(12)
COUNT NUMBER(3)

答案1

得分: 1

你可以将

  1. "update employee set count = ? where code= ' + code + ' ";

替换为

  1. "update employee set count = ? where code= '" + code + "' ";

例如,如果code的值为xyz,则在这种更改后,查询将变为:

  1. "update employee set count = ? where code= 'xyz' ";

然而,我建议您按照以下方式进行修改,以避免SQL注入

  1. query1 = "update employee set count = ? where code= ?";
  2. ps = conn.prepareStatement(query1);
  3. ps.setInt(1, count);
  4. ps.setString(2, code);
英文:

You can replace

  1. "update employee set count = ? where code= ' +code+' ";

with

  1. "update employee set count = ? where code= '" + code + "'";

e.g. if the value of code is xyz then after this change, the query will become:

  1. "update employee set count = ? where code= 'xyz'";

However, I recommend you do it as follows to avoid the SQL Injection:

  1. query1 = "update employee set count = ? where code= ?";
  2. ps = conn.prepareStatement(query1);
  3. ps.setInt(1, count);
  4. ps.setString(2, code);

huangapple
  • 本文由 发表于 2020年10月6日 13:56:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64220092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定