当我只能访问一个字段时,如何返回一个对象?

时间:2017-03-23 20:33:05

标签: java sql jdbc

对不起,如果这没有意义,我无法想出一个更好的方式来表达它。希望我的代码能更好地说明我的问题。

所以在这个方法中我想返回一个Myuser对象。 Myuser对象有大约8个字符串字段,其中一个是userId。

正如您所看到的,当调用此方法时,将传入一个字符串。如果此字符串与Myuser对象中的userId字段具有相同的值,那么我希望它返回完整的Myuser对象。否则返回null。

public Myuser getRecord(String userId) {
    Connection cnnct = null;
    PreparedStatement pStmnt = null;
    Myuser myusr = null;
    boolean result = false;

    try {
        cnnct = getConnection();
        String preQueryStatement = "SELECT * FROM MYUSER WHERE USERID = ?";
        pStmnt = cnnct.prepareStatement(preQueryStatement);

        pStmnt.setString(1, userId);

        ResultSet rslt = pStmnt.executeQuery();

        result = rslt.next();

        if (result) {
            //return myusr in its entirety
        }

     } catch (SQLException ex) {
        while (ex != null) {
            ex.printStackTrace();
            ex = ex.getNextException();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (pStmnt != null) {
            try {
                pStmnt.close();
            } catch (SQLException e) {
            }
        }
        if (cnnct != null) {
            try {
                cnnct.close();
            } catch (SQLException sqlEx) {
            }
        }
    }

    return myusr;
}

编辑:我想到了它,我也会发布Myuser类。

public class Myuser {

    private String userid;
    private String name;
    private String password;
    private String email;
    private String phone;
    private String address;
    private String secQn;
    private String secAns;

    public Myuser(String userid, String name, String password, String email, String phone, String address, String secQn, String secAns) {
        this.userid = userid;
        this.name = name;
        this.password = password;
        this.email = email;
        this.phone = phone;
        this.address = address;
        this.secQn = secQn;
        this.secAns = secAns;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getSecQn() {
        return secQn;
    }

    public void setSecQn(String secQn) {
        this.secQn = secQn;
    }

    public String getSecAns() {
        return secAns;
    }

    public void setSecAns(String secAns) {
        this.secAns = secAns;
    }
}

1 个答案:

答案 0 :(得分:2)

您可以轻松使用result.getXXX(field_name)

if (rslt.next()) {
   myusr = new Myuser(result.getString("userid"), result.getString("name"), 
                      result.getString("password"), result.getString("phone"),  
                      ...);
}

注意不需要在result = rslt.next();之前使用if(result),您必须直接使用

if (rslt.next()) {
   ...
}