2017-04-06 23 views
1

我是Java新手。我現在使用HashMap來存儲MySQL數據庫中的數據,我將使用JSon POST請求來獲取用戶的輸入並在HashMap中搜索相關數據並從HashMap中檢索。我需要來自用戶的三個輸入,但在HashMap中只能輸入1個鍵。所以,我試圖將鍵作爲對象輸入,但它不起作用。以下是我的代碼,用於存儲來自MySQL數據庫的數據。將3個屬性輸入到HashMap密鑰

public class AppDataService { 
HashMap<AppDataRequest, AppData> appdatas = new HashMap<AppDataRequest, AppData>(); 

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
static final String DB_URL = "jdbc:mysql://****:3306/****_demo"; 

static final String USER = "****"; 
static final String PASS = "****"; 

public AppDataService(){ 
    Connection conn = null; 
    Statement stat = null; 
    try{ 
     Class.forName("com.mysql.jdbc.Driver"); 
     conn = DriverManager.getConnection(DB_URL, USER, PASS); 
     stat = conn.createStatement(); 
     String sql = "SELECT * FROM testdata"; 
     ResultSet resu = stat.executeQuery(sql); 
     while(resu.next()){ 
      int id = resu.getInt("app_id"); 
      String email = resu.getString("email"); 
      String password = resu.getString("password"); 
      String status = resu.getString("status"); 
      String message = resu.getString("message"); 
      String token = resu.getString("token"); 
      appdatas.put(new AppDataRequest(id, email, password), new AppData(status, message, token)); 
     } 
     resu.close(); 
     stat.close(); 
     conn.close(); 
    } 
    catch(SQLException se){ 
     se.printStackTrace(); 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
    } 
    finally{ 
     try{ 
      if(stat!=null){ 
       stat.close(); 
      } 
     } 
     catch (SQLException se2){ 

     } 
     try{ 
      if(conn!=null){ 
       conn.close(); 
      } 
     } 
     catch(SQLException se3){ 
      se3.printStackTrace(); 
     } 
    }  
} 

    public List<AppData> getAllAppData(){ 
     return new ArrayList<AppData>(appdatas.values()); 
    } 

    public AppData getAppData(int id){ 
     return appdatas.get(id); 
    } 

    public AppData getSAppData(int id, String email, String password){ 
     return appdatas.get(new AppDataRequest (id, email, password)); 
    } 
} 

我的JSON POST代碼

@Path("/appdata") 
@Consumes(MediaType.APPLICATION_JSON) 
@Produces(MediaType.APPLICATION_JSON) 

AppDataService ads = new AppDataService(); 

@POST 
@Path("/appdatas") 
public AppData getSAppData(AppDataRequest adr){ 
    return ads.getSAppData(adr.getId(), adr.getEmail(),adr.getPassword()); 
} 

AppData的類

public class AppData { 
    public String status; 
    public String message; 
    public String token; 

    public AppData(){ 

    } 

    public AppData(String status, String message, String token) { 
     this.status = status; 
     this.message = message; 
     this.token = token; 
    } 

    public String getStatus() { 
     return status; 
    } 

    public void setStatus(String status) { 
     this.status = status; 
    } 

    public String getMessage() { 
     return message; 
    } 

    public void setMessage(String message) { 
     this.message = message; 
    } 

    public String getToken() { 
     return token; 
    } 

    public void setToken(String token) { 
     this.token = token; 
    } 
} 

AppDataRequest類

public class AppDataRequest { 
    public int id; 
    public String email; 
    public String password; 

    public AppDataRequest(){ 

    } 

    public AppDataRequest(int id, String email, String password) { 
     this.id = id; 
     this.email = email; 
     this.password = password; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getEmail() { 
     return email; 
    } 

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

    public String getPassword() { 
     return password; 
    } 

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

回答

0

您可以在此改變的hashCode和類equals

@Override 
public int hashCode() { 
    int hash = 3; 
    hash = 83 * hash + this.id; 
    hash = 83 * hash + Objects.hashCode(this.email); 
    hash = 83 * hash + Objects.hashCode(this.password); 
    return hash; 
} 

@Override 
public boolean equals(Object obj) { 
    if (this == obj) { 
     return true; 
    } 
    if (obj == null) { 
     return false; 
    } 
    if (getClass() != obj.getClass()) { 
     return false; 
    } 
    final AppDataRequest other = (AppDataRequest) obj; 
    if (this.id != other.id) { 
     return false; 
    } 
    if (!Objects.equals(this.email, other.email)) { 
     return false; 
    } 
    if (!Objects.equals(this.password, other.password)) { 
     return false; 
    } 
    return true; 
} 

你爲什麼不作id就象是一把鑰匙,如果它是表中唯一的,你的其他信息,地圖的價值:

Map<Integer, AppData> appdatas; 
appdatas.put(id, new AppData(status, message, token, email, password)); 
+0

嗨。 @YCF_L。感謝您的回覆。我將嘗試覆蓋HashCode和equals的代碼。我們必須有83號或任何號碼才能做到嗎?我們嘗試使用ID,電子郵件和密碼,因爲電子郵件和密碼也是唯一的,因爲它們只會在我們的桌面上有一個條目。 –

+0

嗨。 @YCF_L。我已經對83進行了一些研究。這是奇怪的素數? –

0

HashMap的內部利用的HashCode和Equals方法從集合中插入和檢索Object。

您需要重寫Java中的equals()和Hashcode()方法,以在HashMap中使用Custom Object作爲Key。此外,您最好還是將不可變類作爲關鍵字,這也是您必須考慮的問題。

看一看下面

What issues should be considered when overriding equals and hashCode in Java?

如果沒有,你可以隨時使用標準字符串/包裝類作爲重點,因爲它們在默認情況下提供必要的使用方法和設計。

作爲進一步閱讀,瞭解HashMap是如何工作的,你會理解爲什麼。

希望這會有所幫助。

+0

感謝您的回覆。我將在我的程序上嘗試使用HashCode和Equals方法。我會以鏈接的形式查看鏈接。 –