2012-12-19 6 views
-2

저는 사용자 프로필 클래스가 있으며,로드하는 가장 좋은 방법은 무엇인지 궁금합니다. 나는 다음과 같은 코드를 가지고 그것을 할 수있는 적절한 방법이 될 것인지 알고 싶었어요. 데이터베이스에서 프로필 가져 오기 - Java

UserProfile userProfile = null; 
char[] password = {'a','b','c'}; 

for(UserProfile profile : UserProfiles){ 
    if(compareUserNameAndPassword("userName", password)){ 
     userProfile = profile; 
    } 

} 

그리고 내 프로필 클래스 :

package jlibfprint; 

public class UserProfile extends Profile { 

    /** 
    * constructor 
    */ 
    public UserProfile(String userName, int id, char[] password){ 
     this.name = userName; 
     this.id = id; 
     this.password = password; 
    } 


    /** 
    * Users password 
    */ 
    private char[] password; 

    /** 
    * Set User password 
    * @param password 
    */ 
    public void setPassword(char[] password){ 

     this.password = password; 

    } 

    /** 
    * compare passwords 
    */ 
    public boolean compareUserNameAndPassword(String userName,char[] password) { 

     if(this.name.equals(userName) && this.password.equals(password)){ 

      return true; 

     } 
     return false; 

    } 
} 
+1

로드로 무엇을 의미합니까? – unholysampler

+0

당신은 무엇을 가장 잘 의미합니까? –

+0

코드가 컴파일되지 않습니다. –

답변

1

이, classloading되지는 단일 클래스의 인스턴스 인 객체를 확인합니다. 그리고 profile.compareUserNameAndPassword(userName,password)이어야합니다.

현재 진행중인 방법은 모든 UserProfile이 메모리에 있음을 의미합니다. 일반적으로 그들은 데이터베이스에있을 것이고 쿼리에서 사용자 이름과 비밀번호 비교를 한 다음 일치하는 경우에만 사용자 이름과 비밀번호 비교를 가져올 것입니다.

어느 시점에서 암호를 해시해야하는지 여부도 고려해야합니다.

아마 바퀴를 다시 발명하지 말고 도움이 될 몇 가지 프레임 워크 도구를 빌려야한다고 생각해야합니다. Hibernate은 데이터베이스에서 Java 오브젝트를 간단하게 검색하도록 설계된 오브젝트 관계 관리 도구입니다. Spring은 올바른 설계 기술을 향상시키고 MVC 접근 방식뿐만 아니라 인증 및 인증을 관리하는 데 도움이되는 프레임 워크입니다.

/* 
    * Retrieves a UserProfile from the database based on a username and password 
    * Needs Apache Commons Codec package otherwise you have to use MessageDigest 
    * which gives a binary SHA-1 
    * @param username The username to fetch 
    * @param password The unhashed password 
    * @return The UserProfile or null if the user was not found in the DB 
    */ 
private static UserProfile retrieveUserProfile(String username, char[] password) 
    throws SQLException { 
    password = DigestUtils.sha1Hex(password); 
    //Assuming a pre-setup JDBC Connection object - `con` 
    final String updateString = "SELECT userName, password FROM userProfiles" 
     + "WHERE username = ? AND password = ? LIMIT 1"; 
    PreparedStatement retrieveUserProfile = con.prepareStatement(updateString) 
    retrieveUserProfile.setString(1,"username"); 
    retrieveUserProfile.setString(2,"password"); 
    ResultSet rs = retrieveUserProfile.execute(); 
    if(rs.next()) { 
     return new UserProfile(username,password); 
    } 
    else { 
     //User Not found 
     return null; 
    } 
} 
+0

그래서 클래스 로딩은 내가 조사해야하는 것입니까 ?? 나는 런타임 중에로드 할 수있는 프로파일 객체의 데이터베이스를 가지려고합니다. – TomSelleck

+1

아니요, 사용하는 것은 완전히 잘못된 용어였습니다. 내 회신을 데이터베이스로드 예제로 업데이트 할 것이다. (Hibernate와 같은 것이 이것을 훨씬 쉽게 만들어 주지만). –

+1

UserProfile ** 객체 **의 검색 및 반환이 어떻게 완료 될지에 대해 '내 머리 꼭대기에서 벗어나'시도를 추가했습니다. –

관련 문제