java - Input 3 Attributes Into HashMap Key -
i new java. using hashmap purpose store data mysql database , use json post request input user , search related data in hashmap , retrieve hashmap. need 3 inputs user in hashmap able input 1 key. so, tried way input key object not working. below code storing data mysql database.
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 * 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)); } }
my json post code
@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 class
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 class
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; } }
you can overide hashcode , equals
in class :
@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; }
why don't make id
key if unique in table, , other information value of map :
map<integer, appdata> appdatas; appdatas.put(id, new appdata(status, message, token, email, password));
Comments
Post a Comment