2017-03-23 1 views
0

죄송합니다. 이것이 이해가되지 않는다면, 더 좋은 방법으로 생각할 수 없습니다. 바라건대 내 코드가 내 문제를 잘 나타내기를 바랍니다.오브젝트의 한 필드에만 액세스 할 때 오브젝트를 리턴하는 방법은 무엇입니까?

그래서이 메서드에서 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; 
    } 
} 
+0

을 사용해야합니다 jdbc 커넥터를 사용하면 결과에서 정보를 가져와야합니다. 새 Myuser Object를 만들면됩니다. wi 그 특정 정보 –

답변

2

쉽게 result.getXXX(field_name) 사용할 수 있습니다 if(result) 전에 result = rslt.next();를 사용할 필요가 없습니다

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

를 그냥 평범한를 사용하는 경우, 직접

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

환호하는 친구, 잘 작동했습니다. – JEvansPritchard

+0

행운을 빌어 요 @JEvansPritchard –

관련 문제