只更新表单中的单个字段,在Struts 2中保持其余信息不变

时间:2015-06-21 12:53:02

标签: java jsp jdbc

package org.entity;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import com.opensymphony.xwork2.ActionSupport;

public class BloodBankEditAction extends ActionSupport {
    BloodBankEdit bupdate;

    public BloodBankEditAction() {
        // TODO Auto-generated constructor stub
    }

    public BloodBankEdit getBupdate() {
        return bupdate;
    }

    public void setBupdate(BloodBankEdit bupdate) {
        this.bupdate = bupdate;
    }

    @Override
    public String execute() throws Exception {
        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/foryou", "root", "siddheshkk");
            System.out.println("Driver Loaded");
            PreparedStatement st = con
                    .prepareStatement("Update bbinfo SET name=?,address=?,city=?,district=?,contactno=?,password=?,aname=?,email=? where code=?");
            st.setString(9, bupdate.getCode());
            st.setString(1, bupdate.getName());
            st.setString(2, bupdate.getAddress());
            st.setString(3, bupdate.getCity());
            st.setString(4, bupdate.getDistrict());
            st.setString(5, bupdate.getNumber());
            st.setString(6, bupdate.getPassword());
            st.setString(7, bupdate.getAname());
            st.setString(8, bupdate.getEmail());
            System.out.println("Executed");
            st.executeUpdate();
            PreparedStatement st1 = con
                    .prepareStatement("Update stockinfo SET name=?,city=?,address=?,contact=?,email=? where code=?");
            st1.setString(1, bupdate.getName());
            st1.setString(2, bupdate.getCity());
            st1.setString(3, bupdate.getAddress());
            st1.setString(4, bupdate.getNumber());
            st1.setString(5, bupdate.getEmail());
            st1.setString(6, bupdate.getCode());
            st1.executeUpdate();
            System.out.println("Done");
        } catch (Exception e) {
            System.out.println(e);
        }
        return "success";
    }

    @Override
    public void validate() {
        // TODO Auto-generated method stub
        super.validate();
        if (!(bupdate.getConfirm().equals(bupdate.getPassword()))) {
            addFieldError("bupdate.confirm", "Password doesnt match");
        }
    }

}

这里我想更新我现有的信息,但是每当我更新数据时,会发生与特定代码相关的所有数据的更新。但我不想那样,假设我想只更新密码,所以当我输入密码时,只需要更新密码,其他信息应保持不变。但这不会发生,如果我只更新一个字段,其他字段将变为空白。那么解决方案是什么?

2 个答案:

答案 0 :(得分:0)

您需要扩展ParameterNameAware并实施acceptableParameterName。在此方法中,您将获得输入参数的名称,您可以通过返回true或拒绝返回false来接受它。

可以在Does the ModelDriven interface poses a security explot in struts2?

找到样本

答案 1 :(得分:0)

我得到了你想要的,你想要改变只返回指定电子邮件的一条记录的查询。电子邮件在表格中应该是唯一的。

PreparedStatement st = con
.prepareStatement("Update bbinfo SET name=?,address=?,city=?,district=?,contactno=?,password=?,aname=?,code=? where email=?");
st.setString(1, bupdate.getName());
st.setString(2, bupdate.getAddress());
st.setString(3, bupdate.getCity());
st.setString(4, bupdate.getDistrict());
st.setString(5, bupdate.getNumber());
st.setString(6, bupdate.getPassword());
st.setString(7, bupdate.getAname());
st.setString(8, bupdate.getCode());
st.setString(9, bupdate.getEmail());
System.out.println("Executed");
st.executeUpdate();