Mysql 将数据库查询中的值返回到LinkedHashMap中

Mysql 将数据库查询中的值返回到LinkedHashMap中,mysql,jdbc,linkedhashmap,rowcount,Mysql,Jdbc,Linkedhashmap,Rowcount,我一直从这个LinkedHashMap中得到一个值,要么是第一个[if(resultset.next())],要么是最后一个[while(resultset.next())],只返回一个结果,但我想要完整的映射。如何返回表中符合条件的所有行?任何帮助都将不胜感激 /** A method to list all previous statuses for a certain user given the userID */ public StringBuilder showStatusHist

我一直从这个LinkedHashMap中得到一个值,要么是第一个[if(resultset.next())],要么是最后一个[while(resultset.next())],只返回一个结果,但我想要完整的映射。如何返回表中符合条件的所有行?任何帮助都将不胜感激

/** A method to list all previous statuses for a certain user given the userID */

public StringBuilder showStatusHistory(int userID) {

    try {

        Date date;
        String status;
        preparedStatement = createStatement("select statusDate,statusText from statusTable where userID = ?");
        preparedStatement.setInt(1,userID);
        resultSet = preparedStatement.executeQuery();

        StringBuilder allStatus = new StringBuilder("");
        statusesList = new LinkedHashMap<>();

        while (resultSet.next()){
            date = resultSet.getDate(1);
            status = resultSet.getString(2);
            statusesList.put(date,status);
        }

        Set<Date> statusTime = statusesList.keySet();
        for(Date k:statusTime){
            allStatus.append(k+" "+statusesList.get(k));
        }
        return allStatus;
    }
    catch (SQLException sqlE){
        sqlE.printStackTrace();
    }
    return null;

下面的示例代码中有一些建议

public StringBuilder showStatusHistory(int userID) {
    // define at top and return "" if no results
    StringBuilder allStatus = new StringBuilder("");

    try {

        Date date;
        String status;
        // define local in the method
        PreparedStatement preparedStatement = createStatement("select statusDate,statusText from statusTable where userID = ?");
        preparedStatement.setInt(1, userID);
        // define local in the method
        ResultSet resultSet = preparedStatement.executeQuery();

        // append to the all Status here and skip the other loop
        // if you need a distinct list (like you had fro the Set) you could use a DISTINCT on the SQL statement.
        while (resultSet.next()) {
            date = resultSet.getDate(1);
            status = resultSet.getString(2);
            // append to the allStatus here 
            allStatus.append(date).append(" ").append(status);  // add a newline to the end? append(System.lineSeparator()) 
        }
    } catch (SQLException sqlE) {
        sqlE.printStackTrace();
        // do you want to throw this if something bad happened? 
    } finally {
       // close up your jdbc resources
    }

    // will contain "" if no records or concatenated statuses from the resultset
    return allStatus;
}
public StringBuilder showStatusHistory(int userID) {
    // define at top and return "" if no results
    StringBuilder allStatus = new StringBuilder("");

    try {

        Date date;
        String status;
        // define local in the method
        PreparedStatement preparedStatement = createStatement("select statusDate,statusText from statusTable where userID = ?");
        preparedStatement.setInt(1, userID);
        // define local in the method
        ResultSet resultSet = preparedStatement.executeQuery();

        // append to the all Status here and skip the other loop
        // if you need a distinct list (like you had fro the Set) you could use a DISTINCT on the SQL statement.
        while (resultSet.next()) {
            date = resultSet.getDate(1);
            status = resultSet.getString(2);
            // append to the allStatus here 
            allStatus.append(date).append(" ").append(status);  // add a newline to the end? append(System.lineSeparator()) 
        }
    } catch (SQLException sqlE) {
        sqlE.printStackTrace();
        // do you want to throw this if something bad happened? 
    } finally {
       // close up your jdbc resources
    }

    // will contain "" if no records or concatenated statuses from the resultset
    return allStatus;
}