Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在HashMap键中输入3个属性_Java_Mysql_Json_Hashmap - Fatal编程技术网

Java 在HashMap键中输入3个属性

Java 在HashMap键中输入3个属性,java,mysql,json,hashmap,Java,Mysql,Json,Hashmap,我是Java新手。我现在使用HashMap来存储MySQL数据库中的数据,我将使用JSon POST请求从用户那里获取输入,并在HashMap中搜索相关数据,然后从HashMap中检索。我需要来自用户的三个输入,但在HashMap中只能输入一个键。因此,我尝试了将键作为对象输入的方法,但它不起作用。下面是我从MySQL数据库存储数据的代码 public class AppDataService { HashMap<AppDataRequest, AppData> appdatas =

我是Java新手。我现在使用HashMap来存储MySQL数据库中的数据,我将使用JSon POST请求从用户那里获取输入,并在HashMap中搜索相关数据,然后从HashMap中检索。我需要来自用户的三个输入,但在HashMap中只能输入一个键。因此,我尝试了将键作为对象输入的方法,但它不起作用。下面是我从MySQL数据库存储数据的代码

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));
    }
}
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;
    }
}

您可以在类中跳过hashCode和
等于

@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的值:

Map<Integer, AppData> appdatas;
appdatas.put(id, new AppData(status, message, token, email, password));
地图应用数据;
put(id,新的AppData(状态、消息、令牌、电子邮件、密码));

您可以在您的类中跳过hashCode和
等于

@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的值:

Map<Integer, AppData> appdatas;
appdatas.put(id, new AppData(status, message, token, email, password));
地图应用数据;
put(id,新的AppData(状态、消息、令牌、电子邮件、密码));

HashMap在内部使用HashCode和Equals方法从集合中插入和检索对象

您需要重写Java中的equals()和Hashcode()方法,以将自定义对象用作HashMap中的键。此外,最好将一个不可变类作为一个密钥,这是你必须考虑的一个键。 看看下面

如果没有,您可以始终使用标准字符串/包装器类作为键,因为它们默认提供使用所需的方法和设计

在进一步阅读时,了解HashMap是如何工作的,您将理解其中的原因


希望这有帮助。

HashMap内部使用HashCode和Equals方法从集合中插入和检索对象

您需要重写Java中的equals()和Hashcode()方法,以将自定义对象用作HashMap中的键。此外,最好将一个不可变类作为一个密钥,这是你必须考虑的一个键。 看看下面

如果没有,您可以始终使用标准字符串/包装器类作为键,因为它们默认提供使用所需的方法和设计

在进一步阅读时,了解HashMap是如何工作的,您将理解其中的原因


希望这有帮助。

谢谢您的回复。我将在我的程序中尝试HashCode和Equals方法。我会把这个链接作为参考。谢谢你的回复。我将在我的程序中尝试HashCode和Equals方法。我会把这个链接作为参考@谢谢你的回复。我将尝试覆盖HashCode和equals的代码。我们必须要83号还是任何号码都可以?我们尝试使用id、电子邮件、密码,因为电子邮件和密码也是唯一的,因为它们在我们的表中只有一个条目。嗨@我对83做了一些研究。这是奇数素数?嗨@谢谢你的回复。我将尝试覆盖HashCode和equals的代码。我们必须要83号还是任何号码都可以?我们尝试使用id、电子邮件、密码,因为电子邮件和密码也是唯一的,因为它们在我们的表中只有一个条目。嗨@我对83做了一些研究。这是奇数素数吗?