Sorry if this doesn't make sense, I can't think of a better way to phrase it. Hopefully my code illustrates my problem better.
So in this method I want to return a Myuser object. A Myuser object has about 8 string fields, one of which is userId.
As you can see, when this method is called, a string is passed in. If this string has the same value as the userId field in the Myuser object, then I want it to return the full Myuser object. Otherwise it returns 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;
}
Edit: I thought for the heck of it I would post the Myuser class as well.
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;
}
}
You can easily use result.getXXX(field_name)
:
if (rslt.next()) {
myusr = new Myuser(result.getString("userid"), result.getString("name"),
result.getString("password"), result.getString("phone"),
...);
}
Note don't need to use result = rslt.next();
before if(result)
, you have to use directly
if (rslt.next()) {
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.